简体   繁体   English

js滚动平滑

[英]js Make a smooth scroll

I seriously have a question about the scroll event. 我真的对滚动事件有疑问。 I tried to solve it all night but I couldnt. 我试着彻夜解决它,但我不能。

I am trying stick a navigation on the top. 我正在尝试在顶部粘贴导航。 The stick effect will process when $(window).scrollTop() pass the point right before the navigation. $(window).scrollTop()在导航之前传递点时,将执行操纵杆效果。

The problem is, there will have a "blink" effect (its like delay process) on IE and Chrome but not on Firefox. 问题是,IE和Chrome上会出现“眨眼”效果(类似延迟过程)但Firefox上没有。

While on my research I knew that Firefox has "smooth scroll" by default. 在我的研究中,我知道Firefox默认情况下是“平滑滚动”。

However, please check this example on Chrome or IE 但是,请在Chrome或IE上查看此示例

http://www.backslash.gr/demos/jquery-sticky-navigation/ http://www.backslash.gr/demos/jquery-sticky-navigation/

It is so smooth like on Firefox, and the code is just that simple...... 就像在Firefox上一样流畅,代码就是那么简单......

The point is, I am doing the exactly same thing like this example but why I have the 'blink' effect?? 关键是,我正在做与此示例完全相同的事情,但为什么我有'眨眼'效果?

Is the trick on CSS ?? CSS上的技巧?

Is there any way that I can create a smooth scrool like what on firefox by js?? 有没有什么方法可以像js上的Firefox一样创建一个流畅的scrool?

Thank you very much for your help. 非常感谢您的帮助。

$(window).on('scroll', Sticky);

function Sticky(){
    $(this).scrollTop() > anchor.offset().top
    ? nav.css({ 'position': 'fixed', 
                      'z-index': z_index, 
                       top: y, 
                       left: x, })
    : nav.css({ 'position': 'static', });
};

Looking at the incomplete example code it's really hard to determine what's going on, so please either update your question with complete code, or better - upload a JSFiddle to serve us as an example and we can directly update it with necessary changes. 查看不完整的示例代码,很难确定发生了什么,所以请用完整的代码更新您的问题,或者更好 - 上传一个JSFiddle作为示例,我们可以通过必要的更改直接更新它。 So far (based on what I said before) it looks like you're getting a flickering effect due to typos in your example code : 到目前为止(基于我之前所说的),由于示例代码中的拼写错误,您看起来会出现flickering效果:

? nav.css({ 'position': 'fixed', 
                  'z-index': z_index, 
                   top: y, 
                   left: x, })
: nav.css({ 'position': 'static', });

where you're not terminating the array of CSS properties and values that needs to be applied (you're ending it with a comma , ), and you've not enclosed some CSS properties in a single quote ' . 在那里你不终止,需要加以应用(你用逗号结束它的CSS属性和值的阵列, ),你也没有在一个单引号括一些CSS属性' Your code should be : 你的代码应该是

? nav.css({ 'position': 'fixed', 
                  'z-index': z_index, 
                   'top': y, 
                   'left': x })
: nav.css({ 'position': 'static' });

That's of course provided you've already set variables z_index , y and x beforehand. 当然,如果你已经预先设置了变量z_indexyx

EDIT & DISCLAIMER: I've created a new JSFiddle with the original demo code. 编辑和免责声明:我用原始的演示代码创建了一个新的JSFiddle The demo you referred to is copyrighted, so please attribute your gratitude to the original author and not me, if that helps you out. 您提到的演示版权受版权保护,所以请将您的感谢归功于原作者,而不是我,如果这可以帮助您。 The code I've posted JSFiddle with is available as a free download, though. 我发布的JSFiddle代码可以免费下载。 So I guess it's OK to reuse it for demo purposes as well. 所以我想将它重用于演示目的也是可以的。 Change that code to comply with your requirements and update it to new version each step you update it. 更改该代码以符合您的要求,并在更新它时将其更新为新版本。 It will help you track where you're doing something wrong (if at all). 它可以帮助您跟踪您做错的地方(如果有的话)。 ;) ;)

I think you might be confusing two things here. 我想你可能会在这里混淆两件事。

  1. looking at the working code you have linked to. 查看您链接到的工作代码。 there is a blink in there if you scroll on chrome or IE or firefox using your mouse scroller. 如果你使用鼠标滚动滚动chrome或IE或firefox,那里就会闪烁。
  2. The blink is because you are changing position suddenly. 眨眼是因为你突然改变了位置。 Try to change the js so it does animate the position rather than suddenly changing its value. 尝试更改js,使其为位置设置动画,而不是突然改变其值。

As others have said linking to a working code and expecting an answer by showing a trick might not help all of us. 正如其他人所说的那样,链接到一个正常工作的代码并通过展示一个技巧来期待答案可能无助于我们所有人。 Try to add animate on position change line of js and see if that helps. 尝试在js的位置变化线上添加动画,看看是否有帮助。

There is no trick here. 这里没有诀窍。 Its all in code so read the source and enjoy. 它的全部代码所以阅读源和享受。

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

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