Skip to content

Instantly share code, notes, and snippets.

@Tomoli75
Created September 25, 2019 19:57
Show Gist options
  • Save Tomoli75/dee988cd6768feb419379f6131346041 to your computer and use it in GitHub Desktop.
Save Tomoli75/dee988cd6768feb419379f6131346041 to your computer and use it in GitHub Desktop.
AutoScroll Plugin and Implementation

AutoScroll Plugin and Implementation

In the ad tech scene, it's common that certain types of companies include legal text in their creative. I got tired of hand-coding it so I made my own plugin for it. It takes in the element as the first parameter and the options in the second parameter. You can see the options in the constructor.

A Pen by Phil Flanagan on CodePen.

License.

<h1>pf-autoscroll</h1>
<h2>Element 1</h2>
<div class="container pf" id="element1">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. </p><p>Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. </p><p>Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. </p><p>Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer lacinia sollicitudin massa. </p><p>Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo. Sed pretium blandit orci. </p>
</div>
<h2>Element 2</h2>
<div class="container pf" id="element2">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam repudiandae tempore quasi dolore voluptate quis temporibus placeat adipisci at pariatur, quia culpa? Mollitia quam expedita dicta corporis sit reprehenderit odio ullam a sapiente quibusdam. Eos eum similique excepturi perferendis sed placeat. Distinctio numquam ab impedit. Maiores iste libero dolores debitis? Illo ea delectus debitis praesentium voluptates corporis error doloremque quis, harum et est vero quidem quod rem similique vitae ad aliquid, soluta maxime dolores repellat veritatis id porro hic! Accusantium iure nam dolorem totam aliquid nostrum, tempora mollitia corporis rem. Eveniet possimus porro sequi deserunt doloribus repellendus ratione itaque quae adipisci soluta dicta voluptatem eaque, laboriosam corrupti, incidunt nemo animi nostrum velit? Saepe beatae inventore quam eius eveniet odit dicta amet repellat maxime iusto soluta, tempora voluptatum eum obcaecati suscipit molestiae corrupti ducimus fugiat nemo alias debitis laboriosam sequi. Officiis eos distinctio voluptates ipsam, nisi deserunt in maiores. Vitae doloribus eum iure eos itaque! Eius molestiae commodi aut in sequi eos explicabo nihil! Quibusdam minus error perferendis, totam similique fugit suscipit sed, quas quos veniam dicta nam tempora minima blanditiis esse ab animi, pariatur corporis? Quas perspiciatis accusamus, laborum fuga, quae libero quidem id ea facilis aperiam assumenda ipsa, sapiente voluptas vitae nihil ipsum reprehenderit repellendus mollitia suscipit neque. Accusantium minus quidem aspernatur nobis ad quibusdam quod asperiores vel animi ea dolor illo voluptatem, vero voluptates. Nulla consectetur repellat ad blanditiis. Veritatis aperiam a natus repudiandae. Illum officiis totam atque iure veritatis et asperiores, deleniti suscipit vitae ad deserunt praesentium necessitatibus numquam quasi, nihil distinctio. Nesciunt, deleniti autem aspernatur dolorem totam natus error placeat animi non sequi praesentium veritatis libero tenetur, impedit sint illo in alias quam, ipsum id sit unde at quod quisquam? Debitis ipsam veritatis soluta provident eveniet inventore incidunt, asperiores, consectetur dolorem in doloribus ex nihil expedita.</p>
</div>
'use strict';
(function() {
window.AutoScroll = function(el, options) {
// In case they forgot 'new'
if (!(this instanceof AutoScroll)) {
return new AutoScroll(el, options);
}
this.el = el;
this.speed = null;
this.isBeingThrown = false;
this.isMouseOver = false;
this.isRunning = false;
this.thrownInterval = null;
this.timeout = null;
this.previousScrollTop = null;
var defaults = {
speed: 10,
pauseBottom: 500,
pauseStart: 500,
requestAnimationFrame: true,
timeoutRate: 30
};
if (options && typeof options === 'object') {
this.options = extendDefaults(defaults, options);
} else {
this.options = defaults;
}
_init.call(this);
}
AutoScroll.prototype.autoScroll = function() {
if (this.isRunning && !this.isBeingThrown && !this.isMouseOver) {
if (this.el.scrollTop < this.el.scrollHeight - this.el.offsetHeight) {
if(this.options.requestAnimationFrame) {
this.el.scrollTop += this.speed;
window.requestAnimationFrame(this.autoScroll.bind(this));
} else {
this.el.scrollTop += this.speed;
if (this.timeout) clearTimeout(this.timeout);
this.timeout = setTimeout(this.autoScroll.bind(this), this.options.timeoutRate)
}
} else {
this.isRunning = false;
setTimeout(this.resetScroll.bind(this), this.options.pauseBottom);
}
} else {
return;
}
}
AutoScroll.prototype.startScroll = function() {
this.isRunning = true;
this.autoScroll.call(this);
}
AutoScroll.prototype.pauseScroll = function() {
this.isRunning = false;
}
AutoScroll.prototype.resetScroll = function() {
this.el.scrollTop = 0;
setTimeout(this.startScroll.bind(this), this.options.pauseStart);
}
AutoScroll.prototype.mouseEnter = function(e) {
this.isMouseOver = true;
this.isRunning = false;
}
AutoScroll.prototype.mouseLeave = function(e) {
this.isMouseOver = false;
this.isRunning = true;
this.startScroll.call(this);
}
AutoScroll.prototype.mobileTouchStart = function(e) {
this.isBeingThrown = true;
}
AutoScroll.prototype.mobileTouchEnd = function(e) {
this.thrownInterval = setInterval(this.wasThrown.bind(this), 10);
}
AutoScroll.prototype.wasThrown = function() {
if (this.previousScrollTop !== this.el.scrollTop) this.previousScrollTop = this.el.scrollTop;
else this.thrownEnd.call(this);
}
AutoScroll.prototype.thrownEnd = function() {
clearInterval(this.thrownInterval);
this.isBeingThrown = false;
this.startScroll.call(this);
}
// Private Methods
function _init() {
this.speed = _setSpeed(this.options.speed);
_initEvents.call(this);
setTimeout(this.startScroll.bind(this), this.options.pauseStart);
}
function _initEvents() {
this.el.addEventListener('mouseenter', this.mouseEnter.bind(this));
this.el.addEventListener('mouseleave', this.mouseLeave.bind(this));
this.el.addEventListener('touchstart', this.mobileTouchStart.bind(this));
this.el.addEventListener('touchend', this.mobileTouchEnd.bind(this));
}
function _setSpeed(scrollDistance) {
return Math.ceil(scrollDistance / 60);
}
// Utility Methods
function extendDefaults(source, properties) {
var property;
for (property in properties) {
if (properties.hasOwnProperty(property)) {
source[property] = properties[property];
}
}
return source;
}
})();
var element1 = document.getElementById('element1');
var element2 = document.getElementById('element2');
var Scroller1 = AutoScroll(element1, {
speed: 5,
requestAnimationFrame: false
});
var Scroller2 = new AutoScroll(element2, {
speed: 120,
});
body {
margin: 2em;
background-color: #333;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
text-align: center;
}
h1, h2 {
color: skyblue;
font-weight: lighter;
margin-bottom: 0.2em;
letter-spacing: 0.3em;
text-transform: lowercase;
}
h1 {color: white}
.container {
width: 300px;
height: 120px;
overflow: scroll;
margin: 0 auto;
text-align: left;
}
.pf {
padding: 1em;
background-color: #eee;
color: #555;
border: 5px solid #555;
}
.pf:hover {
border-color: skyblue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment