2013-11-26 112 views
1

我想制作一个网站,当用户开始滚动它自动滚动它们(不跳转)到设置的位置,以便可以看到内容。内容已经在页面上,我只是不希望他们滚动到它,所以一旦他们开始向下移动页面,它将通过滚动到设置点来帮助他们。检测滚动 - 滚动到div

这是我有这么远,但我迷路了:

<script type="text/javascript"> 
$(body).scroll(function() { 
    if ($this).scrollTop() > 1) { 

    } 
}); 
</script> 

回答

4

Here's a fiddle

var scrollFunction = function() { 
    $('html, body').not(':animated').animate({ 
     scrollTop: $("#layer-2").offset().top //gets the position of the next layer 
    }, 1000, function() { 
     $(document).off('scroll', scrollFunction) 
    }); 
} 

$(document).on('scroll', scrollFunction); 
+0

这是可爱的效果:p –

+0

这真的很好,可惜你不能后滚动。 – user2598957

+0

@ user2598957我已更新脚本。动画完成后,它会启用滚动。它由您决定,然后检查滚动位置是否为0,以重新连接侦听器。 –

2

您可以scrollstart你的元素绑定(也有一个scrollstop)事件:

jQuery('#yourElementId').bind("scrollstart",scrollFunc); 

var scrollFunc = function(){ 
    //if(jQuery('#yourElementId').scrollTop()>1) //unnecessary 
    jQuery('#yourElementId').scrollTop(300); // change 300 to any number you want 
} 
+0

你已经链接到“文档”的jQuery手机,我不认为OP的问题是移动只。 Scrollstart在jQuery中不存在,你需要这个插件:http://james.padolsey.com/javascript/special-scroll-events-for-jquery/ – jammykam

+0

谢谢,没有注意到,让我编辑那个链接。 –