欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

top小火箭

程序员文章站 2023-01-18 22:30:22
// my.js function $(id){return document.getElementById(id)};function show(obj){obj.style.display = "block"};function hide(obj){obj.style.display = "no ......

// my.js

function $(id){return document.getElementById(id)};
function show(obj){obj.style.display = "block"};
function hide(obj){obj.style.display = "none"};
function scroll(){
if(window.pageYOffset != null){// ie9 和以上的版本浏览器
return {
left: window.pageXOffset,
top: window.pageYOffset
}
}
else if(document.compatMode == "CSS1Compat"){// 声明了DTD的都支持
return {
left: document.documentElement.scrollLeft,
top: document.documentElement.scrollTop
}
}
return {// 剩下的就是怪异模式
left: document.body.scrollLeft,
top: document.body.scrollTop
}
}

 

// 上面 为引用,下面写效果

var pic = $("pic");
window.onscroll = function(){
scroll().top>0 ? show(pic) : hide(pic);
leader = scroll().top;
}
var leader=0, target=0, timer = null;
pic.onclick = function(){
target = 0;
timer = setInterval(function(){
leader = leader + (target - leader) / 10;
window.scrollTo(0,leader);
if(leader == target){
clearInterval(timer);
}
},20);
}