2016-12-06 59 views
1

如何显示和隐藏scrolltotop的潜水。 条件: 1.当用户scrool到80 px时,它会显示 2.如果用户点击它,它会将用户置顶。 3.如果用户在某个位置停留2秒或更长时间(可能超过200px或更多),scroolbar也会隐藏。如果他向上或向下滚动,则scroolbar可见。ScroolToTop基于用户的滚动显示和隐藏

$(document).ready(function() { 
    $("#scrollup").hide('slow') 

    $(window).scroll(function (e) { 
     e.preventDefault(); 
     if ($(window).scrollTop() > 80) { 
      $("#scrollup").show('slow'); 
     } 
     if ($(window).scrollTop() < 80) { 
      $("#scrollup").hide('slow'); 
     } 
    }); 

    $(".scrollup").click(function() { 
     $("html, body").animate({ scrollTop: 0 }, 500); 
     return false; 
    }); 
}); 

我已经做了1和2的条件,但我怎么能实现3?

回答

1

添加setInterval(function(){ $("#scrollup").hide('slow'); }, 2000);,并清除它的滚动

var idleInterval=null; 
$(document).ready(function() { 
    $("#scrollup").hide('slow'); 

    $(window).scroll(function (e) { 
     if(idleInterval != null) 
     clearTimeout(idleInterval); 
     idleInterval = setInterval(function(){ $("#scrollup").hide('slow'); }, 2000); 
     idleTime = 0; 
     e.preventDefault(); 
     if ($(window).scrollTop() > 80) { 
      $("#scrollup").show('slow'); 
     } 
     if ($(window).scrollTop() < 80) { 
      $("#scrollup").hide('slow'); 
     } 
    }); 

    $(".scrollup").click(function() { 
     $("html, body").animate({ scrollTop: 0 }, 500); 
     return false; 
    }); 
}); 

演示: -

var idleInterval=null; 
 
$(document).ready(function() { 
 
    $("#scrollup").hide('slow'); 
 

 
    $(window).scroll(function (e) { 
 
     if(idleInterval != null) 
 
     clearTimeout(idleInterval); 
 
     idleInterval = setInterval(function(){ $("#scrollup").hide('slow'); }, 2000); 
 
     idleTime = 0; 
 
     e.preventDefault(); 
 
     if ($(window).scrollTop() > 80) { 
 
      $("#scrollup").show('slow'); 
 
     } 
 
     if ($(window).scrollTop() < 80) { 
 
      $("#scrollup").hide('slow'); 
 
     } 
 
    }); 
 

 
    $(".scrollup").click(function() { 
 
     $("html, body").animate({ scrollTop: 0 }, 500); 
 
     return false; 
 
    }); 
 
});
#pagewrap{ 
 
    height:1000px; 
 
} 
 
#scrollup { 
 
    position: fixed; 
 
    color: white; 
 
    padding: 10px 30px; 
 
    background: red; 
 
    bottom: 30px; 
 
    right: 30px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="pagewrap"> 
 
\t <h1>Demo</h1> 
 
\t <h2><a href="http://webdesignerwall.com/tutorials/animated-scroll-to-top">Animated Scroll to Top</a></h2> 
 
<div id="scrollup"> 
 
    scroll to top 
 
</div> 
 

 
</div>