页面滚动视觉效果实现

来源:互联网 发布:nginx不支持pathinfo 编辑:程序博客网 时间:2024/06/06 03:57
<body style="background-repeat: no-repeat;margin: 0 auto; height: 5000px;">         
<div style="width:100%;position:relative;height: 800px;background-color: #ff5684;">
<h1>感谢观看</h1>
<h1>感谢观看</h1>
<h1>感谢观看</h1>
<h1>感谢观看</h1>
<h1>感谢观看</h1>
</div>
<div style="width:100%;position:relative;height: 800px;background-color: #B2DBA1;margin-top: 700px;">
<h1>感谢观看</h1>
<h1>感谢观看</h1>
<h1>感谢观看</h1>
<h1>感谢观看</h1>
<h1>感谢观看</h1>
</div>
<div style="width:100%;position:relative;height: 800px;background-color: skyblue;margin-top: 700px;">
<h1>感谢观看</h1>
<h1>感谢观看</h1>
<h1>感谢观看</h1>
<h1>感谢观看</h1>
<h1>感谢观看</h1>
</div>

</body>



<script type="text/javascript">
//检查浏览器 是否支持
    function scroll() {
        //判断当前的浏览器是否是IE9+ 或者其他浏览器
        if (window.pageYOffset != null) {
            return {
                left: window.pageXOffset,
                top: window.pageYOffset
            };
        }
        //检测是否是怪异模式浏览器,就是没有声明<!DOCTYPE html>
        else if(document.compatMode == "CSS1Compat"){
            //正常模式
            return {
                left: document.documentElement.scrollLeft,
                top: document.documentElement.scrollTop
            };
        }
        //怪异浏览器
        return {
            left:document.body.scrollLeft,
            top:document.body.scrollTop
        };
    }




//核心滚动代码
    window.onscroll = function () {
    //backgroundAttachment 属性设置背景图像是否固定或者随着页面的其余部分滚动。
        var dis = scroll().top;
        if(dis <= 1500){
            document.body.style.backgroundImage = "url(img/IMG_3441.JPG)";
            document.body.style.backgroundAttachment = "fixed";


            
        }
        else if(dis > 1500&& dis<3000){
            document.body.style.backgroundImage = "url(img/IMG_3443.JPG)";
            document.body.style.backgroundAttachment = "fixed";
        }else{
            document.body.style.backgroundImage = "url(img/IMG_3442.JPG)";
            document.body.style.backgroundAttachment = "fixed";
        }
    }
</script>