2016-09-04 83 views
1

我正在尝试做一件简单的事情。我有一个大全宽页眉的网页。我需要它一直滚动到第一个鼠标滚轮上的内容。jQuery - 顺利滚动内容

我有这样的事情:

$(window).scroll(function() { 
     $('html, body').animate({ 
      scrollTop: ($('.site-content').first().offset().top) 
     },2000); 
    }); 

它的工作原理几乎正常,但 - 起初它使向下滚动短(原滚动)和滚动动画开始后。所以它看起来并不顺利。

如何使这个过程顺利进行,最好的方法是什么?

回答

0

这未经测试;我不是在计算机现在

但尝试改变

$(window).scroll(function(e) { 
    e.preventDefault() 
    $('html, body').animate({ 
     scrollTop: ($('.site-content').first().offset().top) 
    },500); 
}); 

但请记住,这将禁用用户滚动和你的功能将有充分的控制研究

+0

好,它好像它真的可以工作,但事实并非如此。鼠标滚动仍处于启用状态。 – Balu

-1

只有在第一卷轴?

var scrolled = false; 
 
$(window).scroll(function(e) { 
 
    if (scrolled == false) { 
 
    $('html, body').animate({ 
 
     scrollTop: ($('.site-content').first().offset().top) 
 
    }, 2000); 
 
    scrolled = true; 
 
    } 
 
});
div { 
 
    background-color: #000; 
 
    width: 100px; 
 
    height: 100px; 
 
} 
 

 
.site-content { 
 
    background-color: #F00; 
 
}
<body> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div></div> 
 
<div class="site-content"></div> 
 
</body>