简体   繁体   中英

How to scroll to top using JQuery and Material-Design-Lite

I've been using Material-Design-Lite in my website and I'm also using JQuery to display a button that scrolls to top. But they are not working together. With Material-Design-Lite installed, the button does nothing on click.

<div class='back-to-top'>
  <a class='hvr-icon-spin' href='#'/>
</div>

<script>
  $(window).scroll(function() {
    if($(this).scrollTop() &gt; 200) {
      $(&#39;#back-to-top&#39;).fadeIn();
        } else {
        $(&#39;#back-to-top&#39;).fadeOut();
        }
        });
      $(&#39;#back-to-top&#39;).hide().click(function() {
        $(&#39;html, body&#39;).animate({scrollTop:0}, 600);
        return false;
        });
</script>

Is there any alternative way to solve it? I've tried not using JQyery, but it also didn't work. The website I'm trying to use it is here.

This might happen due to fixed position of your main container, header. And material design uses overflow in mdl container. So use this code to scroll top:

$(".mdl-layout__content").animate({scrollTop:0}, 500, "slow");

您可以尝试使用

$('.mdl-layout__content').animate({scrollTop:0}, 'slow');

You can check with the below link.

http://jsfiddle.net/6qrQF/88/

 jQuery(document).ready(function() {
  //set the link
  jQuery('#top-link').topLink({
    min: 500,
    fadeSpeed: 100
  });
  //smoothscroll
  jQuery('#top-link').click(function(r) {    
    jQuery("html, body").animate({ scrollTop: 0 }, 300);
  });
});

对我来说,它与:

$('.mdl-layout').animate({scrollTop:0}, 'slow');

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