回到顶部案例

2018-06-24 02:00:28来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

html:
<div class='btn-top' id='box'></div>
 
css:
<style>
.btn-top {
width: 50px;
height: 50px;
background: url("./components/top/icon_top.png") no-repeat;
background-size: contain;
cursor: pointer;
position: fixed;
right: 10px;
bottom: 20px;
display: none;
}

</style>
 
js:
<script>
var box = document.getElementById('box')
// 定时器
var timer = null;

// 兼容ie9
window.requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function (callback) {
// 为了使setTimteout的尽可能的接近每秒60帧的效果
window.setTimeout(callback, 1000 / 60);
}

window.cancelAnimationFrame = window.cancelAnimationFrame ||
Window.webkitCancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.msCancelAnimationFrame ||
window.oCancelAnimationFrame ||
function (id) {
window.clearTimeout(id);
}


window.onscroll = function () {
var oTop = document.documentElement.scrollTop || document.body.scrollTop;
if (oTop > 300) {
box.style.display = 'block';
} else {
box.style.display = 'none';
}
}


box.onclick = function () {
cancelAnimationFrame(timer);
timer = requestAnimationFrame(function fn() {
var oTop = document.documentElement.scrollTop || document.body.scrollTop;
if (oTop > 0) {
document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
timer = requestAnimationFrame(fn);
} else {
cancelAnimationFrame(timer);
}
});
}
</script>
 

 
 
 
 
 
 
 
 
 
 
 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:移动端屏幕自适应布局

下一篇:Echart--百度地图(散点图)