admin管理员组文章数量:1355564
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?
- I mean two links that make the content scroll left/right when clicked. – Kev Commented Aug 3, 2013 at 8:09
2 Answers
Reset to default 5With 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);
});
本文标签: javascriptUse JSjQuery to scroll a div39s content that has overflow scroll appliedStack Overflow
版权声明:本文标题:javascript - Use JSjQuery to scroll a div's content that has overflow: scroll applied - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744040346a2580544.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论