javascript - Scroll a div 30px when anchor clicked - Stack Overflow

What's the best way to scroll a div with overflow:auto by a certain pixels or certain percentage w

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 badges
Add a ment  | 

2 Answers 2

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

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

相关推荐

  • javascript - Scroll a div 30px when anchor clicked - Stack Overflow

    What's the best way to scroll a div with overflow:auto by a certain pixels or certain percentage w

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信