您好,登錄后才能下訂單哦!
使用css3怎么實(shí)現(xiàn)一個(gè)魔方3d效果 ?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
css是一種用來表現(xiàn)HTML或XML等文件樣式的計(jì)算機(jī)語言,主要是用來設(shè)計(jì)網(wǎng)頁的樣式,使網(wǎng)頁更加美化。它也是一種定義樣式結(jié)構(gòu)如字體、顏色、位置等的語言,并且css樣式可以直接存儲于HTML網(wǎng)頁或者單獨(dú)的樣式單文件中,而樣式規(guī)則的優(yōu)先級由css根據(jù)這個(gè)層次結(jié)構(gòu)決定,從而實(shí)現(xiàn)級聯(lián)效果,發(fā)展至今,css不僅能裝飾網(wǎng)頁,也可以配合各種腳本對于網(wǎng)頁進(jìn)行格式化。
html:
<div class="container"> <div class="box defaul"> <div class="pic"><img src="./img/cat.jpg" alt=""></div> <div class="pic"><img src="./img/dog.jpg" alt=""></div> <div class="pic"><img src="./img/elephant.jpg" alt=""></div> <div class="pic"><img src="./img/lion.jpg" alt=""></div> <div class="pic"><img src="./img/rabbit.jpg" alt=""></div> <div class="pic"><img src="./img/monkey.jpg" alt=""></div> </div> </div> <h2>點(diǎn)擊下面的圖片按鈕切換</h2> <div class="btn"> <input type="image" class="1" src="./img/cat.jpg"> <input type="image" class="2" src="./img/dog.jpg"> <input type="image" class="3" src="./img/elephant.jpg"> <input type="image" class="4" src="./img/lion.jpg"> <input type="image" class="5" src="./img/rabbit.jpg"> <input type="image" class="6" src="./img/monkey.jpg"> </div>
css:
* { margin: 0; padding: 0; } html, body { width: 100%; height: 100%; background: #66677c; text-align: center; } .container { width: 300px; height: 300px; margin: 50px auto 150px; perspective: 1200px; } .container .box { width: 300px; height: 300px; position: relative; transform-style: preserve-3d; transition: transform 0.5s; } .container .box .pic { position: absolute; left: 0; top: 0; width: 300px; height: 300px; box-shadow: 0px 0px 5px #fff; } .container .box .pic img { width: 100%; height: 100%; cursor: pointer; } .container .box .pic:nth-child(1) { transform: translateZ(150px); } .container .box .pic:nth-child(2) { transform: rotateY(-180deg) translateZ(150px); } .container .box .pic:nth-child(3) { transform: rotateY(90deg) translateZ(150px); } .container .box .pic:nth-child(4) { transform: rotateY(-90deg) translateZ(150px); } .container .box .pic:nth-child(5) { transform: rotateX(90deg) translateZ(150px); } .container .box .pic:nth-child(6) { transform: rotateX(-90deg) translateZ(150px); } h2 { color: #fff; font-size: 30px; margin-bottom: 30px; } .btn { display: grid; justify-content: center; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px; grid-gap: 15px; } .btn input { width: 100px; height: 100px; outline: none; border: 2px solid #fff; } .btn input:focus { border: 2px solid #e70; } .defaul { transform: translateZ(-150px) rotateX(-10deg) rotateY(15deg); } .image1 { transform: translateZ(-150px) rotateX(0deg) rotateY(0deg); } .image2 { transform: translateZ(-150px) rotateY(-180deg); } .image3 { transform: translateZ(-150px) rotateY(-90deg); } .image4 { transform: translateZ(-150px) rotateY(90deg); } .image5 { transform: translateZ(-150px) rotateX(-90deg); } .image6 { transform: translateZ(-150px) rotateX(90deg); }
js:
(function(){ var btn = document.getElementsByClassName('btn')[0]; var box = document.getElementsByClassName('box')[0]; btn.addEventListener('click',function(e){ var className = e.target.className; if(className !== 'btn'){ box.style = ''; box.classList.replace(box.classList[1],'image'+className); } }) //鼠標(biāo)拖動(dòng)效果 var xN = 10, yN = 15; document.addEventListener('mousedown',function(e){ e.preventDefault(); e.stopPropagation(); var x = e.clientX; var y = e.clientY; document.addEventListener('mousemove',move); document.addEventListener('mouseup', up); function move(e){ e.preventDefault(); e.stopPropagation(); var x1 = e.clientX; var y1 = e.clientY; xN += (x1 - x)*0.04; yN += (y1 - y)*0.04; box.style.transform = 'translateZ(-150px) rotateY(' + xN + 'deg) rotateX(' + -yN + 'deg)'; } function up(){ document.removeEventListener('mousemove', move); } }) })()
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。