實現(xiàn)監(jiān)聽 頁面在豎直方向的滾動,并觸發(fā)顯示回到頂部按鈕, 點擊按鈕頁面滾動回頁面頂部
====JQ====
var offset = 200; // 顯示back-to-top
var offset2 = 430; // 顯示contact-us
var duration = 500;
$(window).scroll(function() {
if ($(this).scrollTop() > offset) {
$('.back-to-top').fadeIn(400);
} else {
$('.back-to-top').fadeOut(400);
}
if ($(this).scrollTop() > offset2) {
$('.contact-us-right').fadeIn(400);
} else {
$('.contact-us-right').fadeOut(400);
}
});
// 點擊回到頂部
$('.back-to-top').on('click',
function(event) {
event.preventDefault();
$('html, body').animate({
scrollTop: 0
},
600);
return false;
});
====html====
<html>
<body>
<!-- 返回頂部 -->
<a href="#" class="back-to-top"> <img src="/img/contact-us-right/back-to-up.png" alt="" /> </a>
</body>
</html>