简体   繁体   English

在滚动顶部按钮上应用时,jQuery animate()在我的情况下不起作用

[英]jQuery animate() not working in my case when applied on scroll top button

When scroll top button is clicked, the animate function doesn't work. 单击顶部滚动按钮时,动画功能不起作用。 It just follows the link of the anchor. 它仅遵循锚点的链接。 I have used this code in other project and it works perfectly there. 我已经在其他项目中使用了此代码,并且在这里可以完美地工作。

Edit: I also tried: By removing the href="#" 编辑:我也尝试过:通过删除href =“#”

By giving e.preventDefault in scrollUp click 通过在scrollUp中提供e.preventDefault单击

 var scrollUp = $(".scrollTop"); $(window).scroll(function() { var topPos = $(this).scrollTop(); if (topPos > 100) { $(scrollUp).css("opacity", "1"); } else { $(scrollUp).css("opacity", "0"); } }); $($(scrollUp)).click(function() { $('html, body').animate({ scrollTop: 0 }, 800); return false; }); 
 html, body { font-size: 95%; color: #1a2439; background-color: #fafafa; line-height: 1.7em; font-family: 'Raleway', sans-serif; letter-spacing: 0.035em; margin: 0; padding: 0; } 
 <div class="scrollTop"> <a href="#"><i class="fa fa-chevron-circle-up scrollTopBtn"></i></a> </div> 

Finally got my silly mistake. 终于犯了我愚蠢的错误。 I was using jquery slim build version in the current project and it doesn't support all features. 我在当前项目中使用的是jQuery slim版本,它不支持所有功能。 I switched to jquery full version (compressed production version) and this code runs perfectly now! 我切换到jQuery完整版(压缩生产版),此代码现在可以完美运行!

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

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