css3可拖动的魔方3d
程序员文章站
2022-05-18 11:09:03
css3可拖动的魔方3d 主要用到知识点: css3 3d转换 原生js鼠标拖动事件 display:grid 布局 实现的功能 3d魔方 可点击,可拖动 直接看效果 html: css: js: 参考自:腾讯课堂渡一教育 ......
css3可拖动的魔方3d
主要用到知识点:
- css3 3d转换
- 原生js鼠标拖动事件
-
display:grid 布局
实现的功能
- 3d魔方 可点击,可拖动
直接看效果
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>点击下面的图片按钮切换</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); } }) //鼠标拖动效果 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); } }) })()
参考自:腾讯课堂渡一教育