js+css实现点击回到顶部的效果(最低兼容至ie7)

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>js+css实现置顶的效果</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        #top_div{
            position:fixed;
            bottom:190px;
            right:0;
            display:none;
            z-index: 5;
            width: 86px;
            height: 20px;
        }
    </style>
</head>
<body>
<div name="top" style="width: 100%;height: 1000px;background: papayawhip"></div>
<div id="top_div"><a href="#top"><img src="go-top.png"  alt=""/></a></div>
</body>
</html>
<script type="text/javascript">
    /*最低兼容到ie7*/
    window.onscroll = function(){
        var t = document.documentElement.scrollTop || document.body.scrollTop;
        var top_div = document.getElementById( "top_div" );
        if( t >= 300 ) {
            top_div.style.display = "none";
        }
        if( t <= 0 ) {
            top_div.style.display = "none";
        }
        else {
            top_div.style.display = "block";
        }
    }
</script>

效果图:

posted @ 2017-03-27 17:22  贺小鸣  阅读(466)  评论(0编辑  收藏  举报
为尊重他人劳动成果,转载/摘抄请标明来源!