$(function () { $(".sticky").hide(); var top = $(window).scrollTop(); if (top >= 100) { $(".sticky").show(); } else { $(".sticky").hide(); } $(window).scroll(function () { var scrollTop = $(this).scrollTop(); if (scrollTop >= 100) { $(".sticky").show(); } else { $(".sticky").hide(); } }); }); function backTop() { $('body,html').animate({ scrollTop: 0 }, 1000); }
javascript onscroll函数
window.onload = function () { var topbtn = document.getElementById("btn"); var timer = null; var pagelookheight = document.documentElement.clientHeight; window.onscroll = function(){ var backtop = document.body.scrollTop; if(backtop >= pagelookheight){ topbtn.style.display = "block"; }else{ topbtn.style.display = "none"; } } topbtn.onclick = function () { timer = setInterval(function () { var backtop = document.body.scrollTop; var speedtop = backtop/5; document.body.scrollTop = backtop -speedtop; if(backtop ==0){ clearInterval(timer); } }, 30); } }