简体   繁体   English

scrollTo函数在JSfiddle中正常运行,但在我的页面上不平滑

[英]scrollTo function working smoothly in JSfiddle but not smoothly on my page

So, scrollTo() scrolls to the correct part of my page. 因此,scrollTo()滚动到页面的正确部分。 That isn't the issue. 那不是问题。 It use appears there instantly. 它的使用立即出现在那。 I was going to make a fiddle to show you the issue, but found out that it is working there. 我本来是要摆弄给您看这个问题的,但是发现它在那里起作用。 There is no "gradual" scrolling on my page as you can see in the following JSFiddle. 在下面的JSFiddle中可以看到,我的页面上没有“渐进式”滚动。

http://jsfiddle.net/8xK8x/1/ http://jsfiddle.net/8xK8x/1/

$('.scrollto').click(function(e) {
   var elementClicked = $(this).attr("href");
   var destination = $(elementClicked).offset().top;
   $("#main").animate({ scrollTop: parseInt($("#main").scrollTop() + destination)} );
   e.preventDefault();
});

If you have any idea as to why this may be, feel free to point me in the right direction. 如果您对为什么会这样有任何想法,请随时向我指出正确的方向。 I have also included my head tag information in that fiddle. 我还在小提琴中包含了我的头部标签信息。

You can view the site I'm working on here: http://afrohorse.netau.net/test/ 您可以在这里查看我正在处理的网站: http : //afrohorse.netau.net/test/

In your fiddle, the script code is embedded so that it gets executed on page load – whereas in your site, you execute it just where it is right there and then, and the elements you are targetting do not even exist by then, since they are further down the DOM tree (and so no event handlers get actually bound to anything). 在您的小提琴中,嵌入了脚本代码,以便在页面加载时执行该脚本代码–而在您的站点中,则只在该脚本代码所在的位置执行该脚本代码,而那时定位的元素甚至不存在,因为它们在DOM树的下方(因此没有事件处理程序实际绑定到任何东西)。

Either make your code execute on page load as well (wrap it into $(function() { … }); ), 使您的代码也可以在页面加载时执行(将其包装到$(function() { … }); )中,
or move it further down after the elements in the DOM, 或将其向下移动到DOM中的元素之后,
or use .on in the “live” way (see jQuery docs for that). 或以“实时”方式使用.on (有关此信息,请参阅jQuery文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM