HT■ML+CSS+JS完成层叠滚屏实际效果的实例编码

日期:2020-11-04 类型:科技新闻 

关键词:在线网页制作,建网页,个人简介网页制作,简单网页,建立网页

实际效果:
滚屏图在向一个方位移动的同时,要对其每个照片的尺寸,部位,全透明度及其等级开展更改
基本原理:
滚屏图向左移动时将第一身高原素裁切到结尾,滚屏图向右移动时,将结尾子原素裁切到最前边,为此完成无缝拼接滚屏实际效果,再由于li裁切后,下一个li会补好(比如第一身高原素被裁切到最终时,第二个字原素会补好变成第一身高原素),因此li下标不会改变,以次来改动每个部位的li的特性(尺寸,部位,全透明度,等级)
HTML编码:

 <body>
    <div class="smallBox">
    <div class="arrowLeft">←</div>
    <div class="smallPicBox">            //用一个div储放一个ul,并对ul里的每个li开展原始款式设定
    <ul>
                <li id="li1" style="position: absolute;top:calc(50% - 200px);left: 0px;z-index:1;background-color: aqua;transform: scale(0.5);transition: 0.3s;opacity: 0.5;background-image: url(./images/01.jpg);background-size: cover;"></li>
                <li id="li2" style="position: absolute;top:calc(50% - 200px);left: 150px; z-index:2;background-color: red;transform: scale(0.7); transition: 0.3s;opacity: 0.7;background-image: url(./images/02.jpg);background-size: cover;"></li>
                <li id="li3" style="position: absolute;top:calc(50% - 200px);left: 300px; z-index:3;background-color: blue;transform: scale(0.9); transition: 0.3s;opacity: 0.9;background-image: url(./images/03.jpg);background-size: cover;"></li>
                <li id="li4" style="position: absolute;top:calc(50% - 200px);left: 450px; z-index:4;background-color: green;transform: scale(1); transition: 0.3s;opacity: 1;background-image: url(./images/04.jpg);background-size: cover;"></li>
                <li id="li5" style="position: absolute;top:calc(50% - 200px);left: 600px; z-index:3;background-color: yellow;transform: scale(0.9); transition: 0.3s;opacity: 0.9;background-image: url(./images/05.jpg);background-size: cover;"></li>
                <li id="li6" style="position: absolute;top:calc(50% - 200px);left: 750px; z-index:2;background-color: gray;transform: scale(0.7); transition: 0.3s;opacity: 0.7;background-image: url(./images/06.jpg);background-size: cover;"></li>
                <li id="li7" style="position: absolute;top:calc(50% - 200px);left: 900px; z-index:1;background-color: deeppink;transform: scale(0.5); transition: 0.3s;opacity: 0.5;background-image: url(./images/07.jpg);background-size: cover;"></li>
            </ul>
        </div>
        <div class="arrowRight">→</div>
    </div>
</div>
</body>

CSS编码:

<style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .albumBox{
            width: 1200px;
            height: 400px;
            margin: 0 auto;
            border: 1px solid #000;
        }

        .smallBox{
            height: 400px;
            line-height: 400px;
            position: relative;
        }
        .smallPicBox{
            width: 1100px;
            height: 400px;
            float: left;
            position: relative;
        }
        .smallPicBox ul{
            width: 100%;
            height: 400px;
        }
        .smallPicBox li{
            width: 320px;
            height: 400px;
            float: left;
            border: 1px solid #000;
            box-sizing: border-box;
        }
        .smallBox .current::after{
            content: "\25b2";
            position: absolute;
            top: -31px;
            left: 70px;
            color: red;

        }
        .arrowLeft{
            width: 50px;
            height: 400px;
            position: absolute;
            left: 0;
            border: 1px solid #000;
            box-sizing: border-box;
            background-color: gray;
            z-index: 10;
        }
        .arrowRight{
            width: 50px;
            height: 400px;
            position: absolute;
            right: 0;
            border: 1px solid #000;
            box-sizing: border-box;
            background-color: gray;
            z-index: 10;
        }
    </style>

JS编码:

<script>
        var arrowLeft=document.querySelector(".arrowLeft")
        var arrowRight=document.querySelector(".arrowRight")
        var ul=document.querySelector(".smallPicBox ul")
        var li=document.querySelectorAll(".smallPicBox li")
        var timerId=0
        arrowLeft.onclick=function(){                      //左箭头符号点一下恶性事件
                li=document.getElementsByTagName('li')
                ul.appendChild(li[0])                         //将ul的第0身高原素裁切到结尾,完成无缝拼接滚屏
                 posi(li)                                 //改动每个li的特性
           }
        arrowRight.onclick=function(){                    //右箭头符号点一下恶性事件
            li=document.getElementsByTagName('li')        //再次获得li
                ul.insertBefore(li[6],li[0])                 //将ul的最终一身高原素裁切到最前边,完成无缝拼接滚屏
                 posi(li)                                 //改动每个li的特性
        }
        
        function posi(li){                                //改动li特性涵数
            var n1=0
                for(var x=0;x<li.length;x++){     //改动部位
                    li[x].style.left=n1+'px'
                    n1=n1+150
                }
                for(var i=0;i<li.length/2;i++){     //改动等级
                    li[i].style.zIndex=i+1
                    li[li.length-1-i].style.zIndex=i+1
                }
                li[3].style.zIndex='4'
                var n2=0.3
                for(var j=0;j<li.length/2;j++){        //放缩
                    n2=parseFloat(n2+0.2)
                    li[j].style.transform='scale('+n2+')'
                    li[li.length-1-j].style.transform='scale('+n2+')'
                }
                li[3].style.transform='scale(1)'
                var n3=0.3
                for(var k=0;k<li.length/2;k++){       //改动全透明度
                    n3=parseFloat(n3+0.2)
                    li[k].style.opacity=n3
                    li[li.length-1-k].style.opacity=n3
                }
                li[3].style.opacity='1'
        }
        //电脑鼠标移入移除
        temerId=setInterval(function(){
             arrowLeft.click()
        }, 1000);
        arrowLeft.onmouseover=function(){
            clearInterval(temerId)
        }
        arrowLeft.onmouseout=function(){
           temerId=setInterval(function(){
            arrowLeft.click()
         }, 1000);
        }
        arrowRight.onmouseover=function(){
            clearInterval(temerId)
        }
        arrowRight.onmouseout=function(){
           temerId=setInterval(function(){
            arrowLeft.click()
         }, 1000);
        }
    </script>

注:本例js是立即写在body里的,还可以独立写一个js文档,在引进到html页面

实际效果图:
 

到此这篇有关HTML+CSS+JS完成层叠滚屏实际效果的实例编码的文章内容就详细介绍到这了,大量有关HTML+CSS+JS完成滚屏內容请检索脚本制作之家之前的文章内容或再次访问下边的有关文章内容,期待大伙儿之后多多的适用脚本制作之家!