这段js脚本在chrome上不能实现,在火狐和IE下已经实现,如何修改能使其支持chrome?
#gotopbtn{width:53px;height:33px;background:#fff;position:fixed;bottom:14px;right:27p...
#gotopbtn {
width:53px;
height:33px;
background:#fff;
position:fixed;
bottom:14px;
right:27px;
display:none;
cursor:pointer;
font-size:14px;
line-height:30px;
border:1px solid #aaa;
background-image: url(../images/backtotop.gif);
}
<script type="text/javascript">
backTop=function (btnId){
var btn=document.getElementById(btnId);
var d=document.documentElement;
window.onscroll=set;
btn.onclick=function (){
btn.style.display="none";
window.onscroll=null;
this.timer=setInterval(function(){
d.scrollTop-=Math.ceil(d.scrollTop*0.1);
if(d.scrollTop==0) clearInterval(btn.timer,window.onscroll=set);
},10);
};
function set(){btn.style.display=d.scrollTop?'block':"none"}
};
backTop('gotopbtn');
</script>
</div> 展开
width:53px;
height:33px;
background:#fff;
position:fixed;
bottom:14px;
right:27px;
display:none;
cursor:pointer;
font-size:14px;
line-height:30px;
border:1px solid #aaa;
background-image: url(../images/backtotop.gif);
}
<script type="text/javascript">
backTop=function (btnId){
var btn=document.getElementById(btnId);
var d=document.documentElement;
window.onscroll=set;
btn.onclick=function (){
btn.style.display="none";
window.onscroll=null;
this.timer=setInterval(function(){
d.scrollTop-=Math.ceil(d.scrollTop*0.1);
if(d.scrollTop==0) clearInterval(btn.timer,window.onscroll=set);
},10);
};
function set(){btn.style.display=d.scrollTop?'block':"none"}
};
backTop('gotopbtn');
</script>
</div> 展开
展开全部
backTop = function(btnId) {
var btn = document.getElementById(btnId);
var d = document.documentElement;
var webkit = /webkit/i.test(navigator.userAgent);
window.onscroll = set;
btn.onclick = function () {
btn.style.display = "none";
window.onscroll = null;
this.timer = setInterval(function() {
webkit
? document.body.scrollTop
-= Math.ceil(document.body.scrollTop * 0.1)
: d.scrollTop -= Math.ceil(d.scrollTop * 0.1);
if(pageYOffset == 0)
clearInterval(btn.timer, window.onscroll = set);
}, 10);
};
function set(){
btn.style.display = (d.scrollTop || pageYOffset) ? 'block' : "none"
}
};
追问
感谢回答,但是还是没有实现,连火狐下也不能实现了
追答
Javascript 代码是没有错误的,我在 IE, Firefox, Chrome, Opera, Safari 下面全测试了,正常。
应该是你现在的 CSS 或 Javascript 有冲突或错误。你复制下面的代码,新建 html,不需要图片,直接测试。如果对了,再自己看看把代码移到你的页面中。
#gotopbtn {
width:53px;
height:33px;
background:#fff;
position:fixed;
bottom:14px;
right:27px;
display:none;
cursor:pointer;
font-size:14px;
line-height:30px;
border:1px solid #aaa;
background-image: url(../images/backtotop.gif);
}
backTop = function(btnId) {
var btn = document.getElementById(btnId);
var d = document.documentElement;
var webkit = /webkit/i.test(navigator.userAgent);
window.onscroll = set;
btn.onclick = function () {
btn.style.display = "none";
window.onscroll = null;
this.timer = setInterval(function() {
webkit
? document.body.scrollTop
-= Math.ceil(document.body.scrollTop * 0.1)
: d.scrollTop -= Math.ceil(d.scrollTop * 0.1);
if(pageYOffset == 0)
clearInterval(btn.timer, window.onscroll = set);
}, 10);
};
function set(){
btn.style.display = (d.scrollTop || pageYOffset) ? 'block' : "none"
}
};
window.onload = function() {
backTop("gotopbtn");
}
go top
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
p
展开全部
js脚本可以参考W3C标准 http://www.w3schools.com/js/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询