admin管理员组文章数量:1404605
What's the best way to scroll a div with overflow:auto by a certain pixels or certain percentage when clicking an anchor? The HTML is very simple:
<style>
#container{
height:250px;
overflow:auto;
</style>
<div id="container">
<p>Lots of Content</p>
</div>
<a href="#" id="scrolldiv">Scroll Down</a>
When I click the anchor above, I want to scroll that div above a certain amount of pixes, say 30px. I'm hoping jQuery has something built in that makes this simple.
What's the best way to scroll a div with overflow:auto by a certain pixels or certain percentage when clicking an anchor? The HTML is very simple:
<style>
#container{
height:250px;
overflow:auto;
</style>
<div id="container">
<p>Lots of Content</p>
</div>
<a href="#" id="scrolldiv">Scroll Down</a>
When I click the anchor above, I want to scroll that div above a certain amount of pixes, say 30px. I'm hoping jQuery has something built in that makes this simple.
Share Improve this question asked Mar 23, 2012 at 14:55 Joel EckrothJoel Eckroth 2,5343 gold badges20 silver badges24 bronze badges2 Answers
Reset to default 6$('#scrolldiv').click(function(e){
var current = $('#container').scrollTop();
$('#container').scrollTop(current + 30);
e.preventDefault();
});
jsFiddle
For that purpose I would use jQuery's animate:
$('#scrolldiv').click(function(){
$('#container').animate({scrollTop: '+=30'});
});
I belive it's got the shortest syntax for this and it looks nice.
jsFiddle example
本文标签: javascriptScroll a div 30px when anchor clickedStack Overflow
版权声明:本文标题:javascript - Scroll a div 30px when anchor clicked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744837445a2627718.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论