css3實(shí)現(xiàn)可拖動(dòng)的魔方3d效果
發(fā)布時(shí)間:2019-05-07 10:09:02 作者:smile_or
我要評(píng)論
這篇文章主要介紹了css3實(shí)現(xiàn)可拖動(dòng)的魔方3d效果 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
主要用到知識(shí)點(diǎn):
•css3 3d轉(zhuǎn)換 •原生js鼠標(biāo)拖動(dòng)事件
•display:grid 布局
實(shí)現(xiàn)的功能
•3d魔方 可點(diǎn)擊,可拖動(dòng)
•直接看效果

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>
<h1>點(diǎn)擊下面的圖片按鈕切換</h1>
<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);
}
h1 {
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);
}
})
})()
總結(jié)
以上所述是小編給大家介紹的基于css3實(shí)現(xiàn)可拖動(dòng)的魔方3d效果 ,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章

CSS3 SVG實(shí)現(xiàn)的復(fù)古粽子魔方變換動(dòng)畫特效源碼
這是一款基于CSS3 SVG實(shí)現(xiàn)的復(fù)古粽子魔方變換動(dòng)畫特效源碼。畫面上由兩個(gè)立體多邊形拼接成復(fù)古棕子的魔方效果,且隨著立體圖形的旋轉(zhuǎn),上下兩個(gè)立方體間歇的分離再組合成新2018-02-09
CSS3實(shí)現(xiàn)3D視覺旋轉(zhuǎn)魔方動(dòng)畫特效源碼
使用css3 3D視覺魔方特效,旋轉(zhuǎn)魔方動(dòng)畫。本段代碼可以在各個(gè)網(wǎng)頁使用,有需要的朋友可以直接下載使用2018-01-03
純CSS3 3D魔方翻轉(zhuǎn)動(dòng)畫特效源碼
純CSS3 3D魔方翻轉(zhuǎn)動(dòng)畫特效源碼是一款使用簡單純CSS3代碼實(shí)現(xiàn)的魔方特效下載,效果非常棒,本段代碼適應(yīng)于所有網(wǎng)頁使用,有需要的朋友可以直接下載使用2016-08-10




