码迷,mamicode.com
首页 > 其他好文 > 详细

回到顶部

时间:2017-11-14 11:25:52      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:timer   pointer   com   doc   point   img   height   isp   call   

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>


 
技术分享
 
 








回到顶部

标签:timer   pointer   com   doc   point   img   height   isp   call   

原文地址:http://www.cnblogs.com/minimissile/p/10b5a8c54e067d96044e189a81f40714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!