/* jQuery返回顶部 使用方法:initTopHoverTree("tophovertree",500,30,20); } tophovertree是返回顶部按钮的ID,500是返回顶部时间(毫秒),30是距离右边距离,20是距离底部距离 by 何问起 hovertree.com */ function initTopHoverTree(hvtid, times, right, bottom) { jQuery("#" + hvtid).css("right", right).css("bottom", bottom); jQuery("#" + hvtid).on("click", function () { goTopHovetree(times); }) jQuery(window).scroll(function () { if (jQuery(window).scrollTop() > 268) { jQuery("#" + hvtid).fadeIn(100); } else { jQuery("#" + hvtid).fadeOut(100); } }); } //返回顶部动画 //goTop(500);//500ms内滚回顶部 function goTopHovetree(times) { if (!!!times) { jQuery(window).scrollTop(0); return; } var sh = jQuery('body').scrollTop();//移动总距离 var inter = 13.333;//ms,每次移动间隔时间 var forCount = Math.ceil(times / inter);//移动次数 var stepL = Math.ceil(sh / forCount);//移动步长 var timeId = null; function aniHovertree() { !!timeId && clearTimeout(timeId); timeId = null; //console.log(jQuery('body').scrollTop()); if (jQuery('body').scrollTop() <= 0 || forCount <= 0) {//移动端判断次数好些,因为移动端的scroll事件触发不频繁,有可能检测不到有<=0的情况 jQuery('body').scrollTop(0); jQuery('body,html').animate({ scrollTop: 0 }, times); return; } forCount--; sh -= stepL; jQuery('body').scrollTop(sh); timeId = setTimeout(function () { aniHovertree(); }, inter); } aniHovertree(); }