利用perspective 和 transform 里面的幾個(gè)參數(shù)來實(shí)現(xiàn)旋轉(zhuǎn)照片墻 旋轉(zhuǎn)照片墻

首先,來看下,是什么效果吧,上效果圖 ↓

image

其實(shí)這個(gè)東西,很容易制作,先說下思路, 把照片都給疊在一起,然后 rotateY 旋轉(zhuǎn),給每張圖片 旋轉(zhuǎn)不一樣的角度能構(gòu)成一圈, 然后translateZ 出去就好了,最后一步,父級(jí)轉(zhuǎn)起來。

搭建好基本的 html 和 css ↓ 已經(jīng)寫好注釋啦。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        :root,body{    /* root在html也就是文檔,文檔是有高度的,而body 是沒有高度的,所以繼承下來,給 body的子元素使用*/
            height: 100%;
        }
        .wra{
            position: absolute;    /* 把父級(jí)作為容器,定位到屏幕的中間去 */
            width: 200px;
            height: 100px;
            left: calc(50% - 100px);
            top: calc(50% - 50px);
        }
        .img{
            position: absolute;   /*設(shè)置定位屬性,然后所與的圖片就會(huì) 疊在一起。*/
            width: 200px;
            height: 100px;
        }
        .img:nth-of-type(1){
            background-image: url('./img/rotatePic/u=1036157886,1414018506&fm=26&gp=0.jpg');
            background-size: cover; /*這個(gè)參數(shù),是為了更好的顯示完整的圖片,既是他有可能顯示不全。*/
        }
        .img:nth-of-type(2){
            background-image: url('./img/rotatePic/u=1252635897,4048351284&fm=26&gp=0.jpg');
            background-size: cover;
        }
        .img:nth-of-type(3){
            background-image: url('./img/rotatePic/u=1369094069,1969348532&fm=26&gp=0.jpg');
            background-size: cover;
        }
        .img:nth-of-type(4){
            background-image: url('./img/rotatePic/u=1891680577,1135327170&fm=26&gp=0.jpg');
            background-size: cover;
        }
        .img:nth-of-type(5){
            background-image: url('./img/rotatePic/u=215760299,4224999617&fm=26&gp=0.jpg');
            background-size: cover;
        }
        .img:nth-of-type(6){
            background-image: url('./img/rotatePic/u=4188003421,389717180&fm=26&gp=0.jpg');
            background-size: cover;
        }
        .img:nth-of-type(7){
            background-image: url('./img/rotatePic/下載 (1).jfif');
            background-size: cover;
        }
        .img:nth-of-type(8){
            background-image: url('./img/rotatePic/下載 (2).jfif');
            background-size: cover;
        }
    </style>
</head>
<body>
    <div class="wra">
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
    </div>
</body>
</html>

效果如下:

image

基本架子搭建好后,給每張圖片,旋轉(zhuǎn)不同的位置。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        :root,body{   
            height: 100%;
        }
        .wra{
            position: absolute;    
            width: 200px;
            height: 100px;
            left: calc(50% - 100px);
            top: calc(50% - 50px);
        }
        .img{
            position: absolute;   
            width: 200px;
            height: 100px;
        }
        .img:nth-of-type(1){
            background-image: url('./img/rotatePic/u=1036157886,1414018506&fm=26&gp=0.jpg');
            background-size: cover; 
            /*第一張圖片,就不用旋轉(zhuǎn)了,讓他在原位置就好。*/
        }
        .img:nth-of-type(2){
            background-image: url('./img/rotatePic/u=1252635897,4048351284&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(45deg);
        }
        .img:nth-of-type(3){
            background-image: url('./img/rotatePic/u=1369094069,1969348532&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(90deg);
        }
        .img:nth-of-type(4){
            background-image: url('./img/rotatePic/u=1891680577,1135327170&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(135deg);
        }
        .img:nth-of-type(5){
            background-image: url('./img/rotatePic/u=215760299,4224999617&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(180deg);
        }
        .img:nth-of-type(6){
            background-image: url('./img/rotatePic/u=4188003421,389717180&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(225deg);
        }
        .img:nth-of-type(7){
            background-image: url('./img/rotatePic/下載 (1).jfif');
            background-size: cover;
            transform: rotateY(270deg);
        }
        .img:nth-of-type(8){
            background-image: url('./img/rotatePic/下載 (2).jfif');
            background-size: cover;
            transform: rotateY(315deg);
        }
    </style>
</head>
<body>
    <div class="wra">
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
    </div>
</body>
</html>

旋轉(zhuǎn)好位置后,添加 translateZ() 參數(shù),分開圖片。因?yàn)?,translateZ 也是 transform 的參數(shù)之一,所以要添加在rotate后面接上。最后,記得在15行添加上 perspective 屬性。不懂什么是 perspective 屬性的話,請(qǐng)點(diǎn)擊→ css3系列之詳解perspective

<!DOCTYPE html>
<!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <meta http-equiv="X-UA-Compatible" content="ie=edge">
     <title>Document</title>
     <style>
         *{
             padding: 0;
             margin: 0;
         }
         :root,body{   
             height: 100%;
             perspective: 1000px;
         }
         .wra{
             position: absolute;    
             width: 200px;
             height: 100px;
             left: calc(50% - 100px);
             top: calc(50% - 50px);
             transform-style: preserve-3d; /*這個(gè)屬性的作用呢,是為了讓瀏覽器,以3d的方式來渲染,
                                             這個(gè)屬性要添在父級(jí)身上,那么他的子元素,就能以3d的方式展示。
                                             瀏覽器默認(rèn)的渲染方式是2d的,我們這種3d結(jié)構(gòu),他顯示不出來。 */
         }
         .img{
             position: absolute;   
             width: 200px;
             height: 100px;
         }
         .img:nth-of-type(1){
             background-image: url('./img/rotatePic/u=1036157886,1414018506&fm=26&gp=0.jpg');
             background-size: cover; 
             transform: translateZ(350px);
         }
         .img:nth-of-type(2){
             background-image: url('./img/rotatePic/u=1252635897,4048351284&fm=26&gp=0.jpg');
             background-size: cover;
             transform: rotateY(45deg) translateZ(350px);
         }
         .img:nth-of-type(3){
             background-image: url('./img/rotatePic/u=1369094069,1969348532&fm=26&gp=0.jpg');
             background-size: cover;
             transform: rotateY(90deg) translateZ(350px);
         }
         .img:nth-of-type(4){
             background-image: url('./img/rotatePic/u=1891680577,1135327170&fm=26&gp=0.jpg');
             background-size: cover;
             transform: rotateY(135deg) translateZ(350px);
         }
         .img:nth-of-type(5){
             background-image: url('./img/rotatePic/u=215760299,4224999617&fm=26&gp=0.jpg');
             background-size: cover;
             transform: rotateY(180deg) translateZ(350px);
         }
         .img:nth-of-type(6){
             background-image: url('./img/rotatePic/u=4188003421,389717180&fm=26&gp=0.jpg');
             background-size: cover;
             transform: rotateY(225deg) translateZ(350px);
         }
         .img:nth-of-type(7){
             background-image: url('./img/rotatePic/下載 (1).jfif');
             background-size: cover;
             transform: rotateY(270deg) translateZ(350px);
         }
         .img:nth-of-type(8){
             background-image: url('./img/rotatePic/下載 (2).jfif');
             background-size: cover;
             transform: rotateY(315deg) translateZ(350px);
         }
     </style>
 </head>
 <body>
     <div class="wra">
         <div class="img"></div>
         <div class="img"></div>
         <div class="img"></div>
         <div class="img"></div>
         <div class="img"></div>
         <div class="img"></div>
         <div class="img"></div>
         <div class="img"></div>
     </div>
 </body>
 </html>

然后你就能看見這種效果啦。

image

然后,我們給父級(jí) 加上旋轉(zhuǎn)的功能就OK了。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        :root,body{   
            height: 100%;
            perspective: 1000px;
        }
        @keyframes run{   /*這里我們要他旋轉(zhuǎn)360度,那么就不用那么麻煩寫關(guān)鍵幀了。開始0  結(jié)束360,然后循環(huán)即可*/
            0%{
                transform: rotateY(0deg);
            }
            100%{
                transform: rotateY(360deg);
            }
        }
        .wra{
            position: absolute;    
            width: 200px;
            height: 100px;
            left: calc(50% - 100px);
            top: calc(50% - 50px);
            transform-style: preserve-3d;
            animation: run 20s linear infinite; /*animation linear是勻速運(yùn)動(dòng),infinite是無限循環(huán)*/
        }
        .img{
            position: absolute;   
            width: 200px;
            height: 100px;
        }
        .img:nth-of-type(1){
            background-image: url('./img/rotatePic/u=1036157886,1414018506&fm=26&gp=0.jpg');
            background-size: cover; 
            transform: translateZ(350px);
        }
        .img:nth-of-type(2){
            background-image: url('./img/rotatePic/u=1252635897,4048351284&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(45deg) translateZ(350px);
        }
        .img:nth-of-type(3){
            background-image: url('./img/rotatePic/u=1369094069,1969348532&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(90deg) translateZ(350px);
        }
        .img:nth-of-type(4){
            background-image: url('./img/rotatePic/u=1891680577,1135327170&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(135deg) translateZ(350px);
        }
        .img:nth-of-type(5){
            background-image: url('./img/rotatePic/u=215760299,4224999617&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(180deg) translateZ(350px);
        }
        .img:nth-of-type(6){
            background-image: url('./img/rotatePic/u=4188003421,389717180&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(225deg) translateZ(350px);
        }
        .img:nth-of-type(7){
            background-image: url('./img/rotatePic/下載 (1).jfif');
            background-size: cover;
            transform: rotateY(270deg) translateZ(350px);
        }
        .img:nth-of-type(8){
            background-image: url('./img/rotatePic/下載 (2).jfif');
            background-size: cover;
            transform: rotateY(315deg) translateZ(350px);
        }
    </style>
</head>
<body>
    <div class="wra">
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
    </div>
</body>
</html>

最后,再加上一個(gè)小功能。像你想看哪里,鼠標(biāo)就移動(dòng)到 那里 即可,代碼在93行開始。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        :root,body{
            height: 100%;
            perspective: 1000px;
        }
        @keyframes run{
            0%{
                transform: rotateY(0deg);
            }
            100%{
                transform: rotateY(360deg);
            }
        }
        .wra{
            position: absolute;
            width: 200px;
            height: 100px;
            left: calc(50% - 100px);
            top: calc(50% - 50px);
            transform-style: preserve-3d;
            animation: run 20s linear infinite;
        }
        .img{
            position: absolute;
            width: 200px;
            height: 100px;
        }
        .img:nth-of-type(1){
            background-image: url('./img/rotatePic/u=1036157886,1414018506&fm=26&gp=0.jpg');
            background-size: cover;
            transform: translateZ(350px);
        }
        .img:nth-of-type(2){
            background-image: url('./img/rotatePic/u=1252635897,4048351284&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(45deg) translateZ(350px);
        }
        .img:nth-of-type(3){
            background-image: url('./img/rotatePic/u=1369094069,1969348532&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(90deg) translateZ(350px);
        }
        .img:nth-of-type(4){
            background-image: url('./img/rotatePic/u=1891680577,1135327170&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(135deg) translateZ(350px);
        }
        .img:nth-of-type(5){
            background-image: url('./img/rotatePic/u=215760299,4224999617&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(180deg) translateZ(350px);
        }
        .img:nth-of-type(6){
            background-image: url('./img/rotatePic/u=4188003421,389717180&fm=26&gp=0.jpg');
            background-size: cover;
            transform: rotateY(225deg) translateZ(350px);
        }
        .img:nth-of-type(7){
            background-image: url('./img/rotatePic/下載 (1).jfif');
            background-size: cover;
            transform: rotateY(270deg) translateZ(350px);
        }
        .img:nth-of-type(8){
            background-image: url('./img/rotatePic/下載 (2).jfif');
            background-size: cover;
            transform: rotateY(315deg) translateZ(350px);
        }
    </style>
</head>
<body>
    <div class="wra">
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
        <div class="img"></div>
    </div>
    <script>
        document.body.onmousemove = function(e){
            this.style.perspectiveOrigin = e.pageX + 'px ' + e.pageY +'px';
        }
    </script>
</body>
</html>

看一下效果好吧。

image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容