js實現3D切換效果


今天分享一個3d翻轉動畫效果,js+css3+h5實現,沒有框架。

先看下html部分:

 1 <div class="box">
 2     <ul>
 3         <li></li>
 4         <li></li>
 5         <li></li>
 6         <li></li>
 7     </ul>
 8     <span class="arrow-left">上一頁</span>
 9     <span class="arrow-right">下一頁</span>
10 </div>
View Code

一個父容器,里面包裹一個ul,然后用li存放5張自己喜歡的圖片

下面是css3部分:

<style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .box{
            width: 300px;
            height: 300px;
            margin: 100px auto;
            position: relative;
        }
        ul{
            width: 100%;
            height: 100%;
            box-sizing: border-box;
            position: relative;
            transform-style: preserve-3d;
            transition: all 1s;
        }
        ul li{
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
            background-size: 100% 100%;
        }

        li:nth-child(1){
            background-image: url("3.jpg");
            transform: translateZ(150px);
        }
        li:nth-child(2){
            background-image: url("4.jpg");
            transform: rotateX(90deg) translateZ(150px);
        }
        li:nth-child(3){
            background-image: url("5.jpg");
            transform: rotateX(180deg) translateZ(150px);
        }
        li:nth-child(4){
            background-image: url("6.jpg");
            transform: rotateX(270deg) translateZ(150px);
        }
        .arrow-left,.arrow-right{
            width: 50px;
            height: 50px;
            background-color: #ff254a;
            border-radius: 5px;
            text-align: center;
            cursor: pointer;
        }
        .arrow-left{
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            left: -50px;
            line-height: 50px;
        }
        .arrow-right{
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            right: -50px;
            line-height: 50px;
        }
    </style>

里面主要用到css3的 transform3d旋轉 和 transition的過度動畫。“transform-style: preserve-3d;”這句話一定要寫在動畫的父容器里面,否則3d效果看不出來。

下面貼出js部分:

 1 <script>
 2     var btnleft = document.querySelector(".arrow-left");
 3     var btnright = document.querySelector(".arrow-right");
 4     var ul = document.querySelector("ul");
 5 
 6     var index = 0;
 7     btnleft.addEventListener("click",function () {
 8         index++;
 9         ul.style.transform = "rotateX("+(index * 90)+"deg)";
10     })
11     btnright.addEventListener("click",function () {
12         index--;
13         ul.style.transform = "rotateX("+(index * 90)+"deg)";
14     })
15 </script>
View Code

里面主要就是操作點擊事件和動態控制照片旋轉效果。

最后,大家可以新建一個html文件,把上面3個部分直接拷貝,可以直接在瀏覽器運行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM