javascript - Use JSjQuery to scroll a div's content that has overflow: scroll applied - Stack Overflow

So I have a div, with a bunch of content inside it that is wider than the div. I have set overflow-x: s

So I have a div, with a bunch of content inside it that is wider than the div. I have set overflow-x: scroll and all is well. However I'd like to have two links on my page that allow the user to scroll the content within the div left and right (mimicking the standard scrollbar arrow functionality). Is that possible?

So I have a div, with a bunch of content inside it that is wider than the div. I have set overflow-x: scroll and all is well. However I'd like to have two links on my page that allow the user to scroll the content within the div left and right (mimicking the standard scrollbar arrow functionality). Is that possible?

Share Improve this question asked Aug 3, 2013 at 7:45 KevKev 4152 gold badges8 silver badges17 bronze badges 1
  • I mean two links that make the content scroll left/right when clicked. – Kev Commented Aug 3, 2013 at 8:09
Add a ment  | 

2 Answers 2

Reset to default 5

With jQuery the scrolling can be handled like in this example:

$(function(){
    var iv;
    var div = $('#content');
    $('#left').mousedown(function(){
        iv = setInterval(function(){
            div.scrollLeft( div.scrollLeft() - 4);
        },20);
    });
    $('#right').mousedown(function(){
        iv = setInterval(function(){
            div.scrollLeft( div.scrollLeft() + 4);
        },20);
    });
    $('#left,#right').on('mouseup mouseleave', function(){
        clearInterval(iv);
    });
});

JSFiddle:
http://jsfiddle/jdNa9/1/ (basic example)
http://jsfiddle/jdNa9/3/ (with a bit updated CSS)

Use jQuery .scrollLeft(). For example if you want to scroll 300 pixels:

$("a.your_link").click(function(){
  $("#your_container").scrollLeft(300);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744040347a4548194.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信