简体   繁体   中英

Vertical scroll on div text

Currently my div is scrolling using two buttons (Up and Down) but I would like it to jump to top and jump to bottom with those two buttons with a vertical auto scroll (downwards) going on instead. How do I accomplish this in jQuery? Here is my code so far: http://jsfiddle.net/NkY8J/ .

HTML

    <div id="scroll">
         Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here<br /><br />
    Content here and more content here
      </div>
              <input id="scroll-up" class="btn" type="button" value="Up"/>
              <input id="scroll-down" class="btn" type="button" value="Down"/>
​

JS

$(function() {
    var ele   = $('#scroll');
    var speed = 25, scroll = 5, scrolling;

    $('#scroll-up').mouseenter(function() {
        // Scroll the element up
        scrolling = window.setInterval(function() {
            ele.scrollTop( ele.scrollTop() - scroll );
        }, speed);
    });

    $('#scroll-down').mouseenter(function() {
        // Scroll the element down
        scrolling = window.setInterval(function() {
            ele.scrollTop( ele.scrollTop() + scroll );
        }, speed);
    });

    $('#scroll-up, #scroll-down').bind({
        click: function(e) {
            // Prevent the default click action
            e.preventDefault();
        },
        mouseleave: function() {
            if (scrolling) {
                window.clearInterval(scrolling);
                scrolling = false;
            }
        }
    });
});
​

CSS

#scroll {
    width: 200px;
    height: 100px;
    overflow: hidden;
    padding: 4px;
}​

Let's see Here. http://jsfiddle.net/NkY8J/2/

Just set ele.scrollTop(0); to jump to top.

And set ele.scrollTop(Math.pow(10,9)); to jump to bottom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM