简体   繁体   中英

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="#"

By giving e.preventDefault in scrollUp click

 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. I switched to jquery full version (compressed production version) and this code runs perfectly now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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