返回顶部功能,js在ie6点击没反应,求助
<script type="text/javascript"><!--
var w = 150;
var h = 100;
var str = "";
var obj = document.getElementById("divStayTopLeft");
if (obj)str = obj.innerHTML;
if( typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat'){
document.writeln('<DIV style="z-index:9;right:0;bottom:0;height:'+h+'px;width:'+w+'px;overflow:hidden;POSITION:fixed;_position:absolute; _margin-top:expression(document.documentElement.clientHeight-this.style.pixelHeight+document.documentElement.scrollTop);">');
}
else {
document.writeln('<DIV style="z-index:9;right:0;bottom:0;height:'+h+'px;width:'+w+'px;overflow:hidden;POSITION:fixed;*position:absolute; *top:expression(eval(document.body.scrollTop)+eval(document.body.clientHeight)-this.style.pixelHeight);">');
}
document.writeln('<div style="clear:both;margin:auto;height:50px;font-size:50px;overflow:hidden;font-weight:bold;text-align:left;"><a href="javascript:scroll(0,0)" hidefocus="true"><img src="img/return_top.gif" alt="回到顶部" style="border: 0px;" /></a></div> ');
document.write('<div style="clear:both;margin:auto;overflow:hidden;text-align:left;">'+str+'</div>');
document.writeln('</DIV>');
-->
</script>
<script type="text/javascript"><!--
var w = 150;
var h = 100;
var str = "";
var obj = document.getElementById("divStayTopLeft");
if (obj)str = obj.innerHTML;
if( typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat'){
document.writeln('<DIV style="z-index:9;right:0;bottom:0;height:'+h+'px;width:'+w+'px;overflow:hidden;POSITION:fixed;_position:absolute; _margin-top:expression(document.documentElement.clientHeight-this.style.pixelHeight+document.documentElement.scrollTop);">');
}
else {
document.writeln('<DIV style="z-index:9;right:0;bottom:0;height:'+h+'px;width:'+w+'px;overflow:hidden;POSITION:fixed;*position:absolute; *top:expression(eval(document.body.scrollTop)+eval(document.body.clientHeight)-this.style.pixelHeight);">');
}
document.writeln('<div style="clear:both;margin:auto;height:50px;font-size:50px;overflow:hidden;font-weight:bold;text-align:left;"><a href="javascript:scroll(0,0)" hidefocus="true"><img src="img/return_top.gif" alt="回到顶部" style="border: 0px;" /></a></div> ');
document.write('<div style="clear:both;margin:auto;overflow:hidden;text-align:left;">'+str+'</div>');
document.writeln('</DIV>');
-->
</script>
var top = document.body.scrollTop | document.documentElement.scrollTop;
$("#return_top").click(function(){
var bh = $("body").height();
var time = 0;
//var time = bh / 3; 这里的time是返回的速度
var bs = $("html,body").scrollTop();
$("html,body").animate({scrollTop: "0px"}, time)
})
$(window).scroll( function() {
var rt = $("#return_top");
if(rt.length > 0){
var top = document.body.scrollTop | document.documentElement.scrollTop;
if(top>0){
rt.show();
}else{
rt.hide();
}
}
} );
请先加载jquery的代码,然后再添加下方代码:(注:jquery.js可到官方自行下载.)gotop样式显示:#goToTop {position:fixed;right: 0;z-index:9000;bottom:0; width:50px; height:50px;_position: absolute;/*IE6 用absolute模拟fixed*/_top: expression(documentElement.scrollTop + 400 + "px"); /*IE6 动态设置top位置*/}
#goToTop a {width:50px;height:50px;display:block;overflow:hidden;background:url(http://tuan.admin5.com/app/Tpl/purple_new/css/back-home.gif) no-repeat 0px 0px; text-indent:500px;}
#goToTop a:hover {background:url(http://tuan.admin5.com/app/Tpl/purple_new/css/back-home.gif) no-repeat -50px 0px}
jquery代码:(function($){
var topDistance = 300;
var showDistance = 1;
var goToTopButton = "
";
var thisTop = $(window).scrollTop() + topDistance;
$("body").append(goToTopButton);
//$("#goToTop").css("top",thisTop); if($(window).scrollTop() < showDistance) {
$("#goToTop").hide();
}
$(window).scroll(function(){
// thisTop = $(this).scrollTop() + topDistance;
// $("#goToTop").css("top",thisTop);
if($(this).scrollTop() < showDistance) {
$("#goToTop").fadeOut("fast");
} else {
$("#goToTop").fadeIn("fast");
}
}); $("#goToTop a").click(function(){
$("html,body").animate({scrollTop:0},"slow");
return false;
});
})(jQuery);
要看你的代码放在什么地方了,scroll(0,0)不能放在table里面,<body>....<table>....</table><script>...</script>...</body>,IE6就是有这个毛病,如果你的代码都是div,想放哪都可以
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>弹性返回顶部JS代码_懒人图库</title>
<style>
#goTopBtn {
position:fixed;text-align:center; width:30px;height:30px;background:#ccc; bottom:10px;right:0px; cursor:pointer;
}
</style><script type=text/javascript >
function goTopEx(){
var obj=document.getElementById("goTopBtn");
function getScrollTop(){
return document.documentElement.scrollTop;
}
function setScrollTop(value){
document.documentElement.scrollTop=value;
}
window.onscroll=function(){getScrollTop()>0?obj.style.display="":obj.style.display="none";}
obj.onclick=function(){
var goTop=setInterval(scrollMove,10);
function scrollMove(){
setScrollTop(getScrollTop()/1.1);
if(getScrollTop()<1)clearInterval(goTop);
}
}
}
</script>
</head><body>
<p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p>
<p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p><p>pr<p>><div style="display: none" id="goTopBtn">Top</div>
<script type=text/javascript>goTopEx();</script></body>
</html>