acctman
acctman

Reputation: 4349

jQuery auto scroll vertically in a div

Can anyone suggest a good simple jQuery vertical autoscroller script? one that's not bloat, I just need for it to auto start and scroll 6 or more liin a div. i tried the jquery.autoscroll.js but couldn't get it to auto start.

$.fn.autoscroll.defaults = { 
   start: { 
      step: 50, 
      scroll: true, 
      direction: "down", 
      pauseOnHover: true 
   }, 
   delay: 5000, 
   ffrw: { 
      speed: "fast", 
      step: 100 
   } 
};

Upvotes: 4

Views: 63756

Answers (4)

Shawn
Shawn

Reputation: 3149

The autoscroll.js download at github has the wrong files in the download. If you search there, you can find them.

I've made a working demo at the link below, which you can copy from if you like.

autoscroll.js at github

my working autoscroll demo

Upvotes: 1

Josh Burson
Josh Burson

Reputation: 559

Robin's answer didn't quite do the trick for me, for several reasons, so here's a modified and extended version of his approach:

var div = $('.scrollbit');

$('.scrollbit').bind('scroll mousedown wheel DOMMouseScroll mousewheel keyup', function(evt) {
    if (evt.type === 'DOMMouseScroll' || evt.type === 'keyup' || evt.type === 'mousewheel') {

    }
    if (evt.originalEvent.detail < 0 || (evt.originalEvent.wheelDelta && evt.originalEvent.wheelDelta > 0)) { 
        clearInterval(scrollbit);
    }
    if (evt.originalEvent.detail > 0 || (evt.originalEvent.wheelDelta && evt.originalEvent.wheelDelta < 0)) { 
        clearInterval(scrollbit);
    }
});

var scrollbit = setInterval(function(){
    var pos = div.scrollTop();
    if ((div.scrollTop() + div.innerHeight()) >= div[0].scrollHeight) {
        clearInterval(scrollbit);
    }
    div.scrollTop(pos + 1);
}, 100);

with help from anonymous user1248475 here:

Detect whether scroll event was created by user

and this answer:

https://stackoverflow.com/a/9392134

Hope that's helpful for anyone looking to solve the issue of how to auto scroll a div with jquery and stop when user scrolls manually.

Upvotes: 1

Robin Maben
Robin Maben

Reputation: 23044

var div = $('div.autoscrolling');
var scroller = setInterval(function(){
    var pos = div.scrollTop();
    div.scrollTop(++pos);
}, 100)​

Working Demo.

EDIT:

To stop scrolling when the div has scrolled to the bottom add the following check at the end of the above function() {} -

if($(this).scrollTop() + $(this).innerHeight() >= this.scrollHeight)
   clearInterval(scroller);
}

Upvotes: 17

Nerdroid
Nerdroid

Reputation: 13966

simplyScroll is a cool plugin http://logicbox.net/jquery/simplyscroll/

Upvotes: 3

Related Questions