css3实现可拖动的魔方3d效果
网站建设 2023-01-28 21:39www.1681989.com免费网站
主要用到知识点
•css3 3d转换 •原生js鼠标拖动事件
•display:grid 布局
实现的功能
•3d魔方 可点击,可拖动
•直接看效果
html
<div class="contaer"> <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>点击狼蚁网站SEO优化的图片按钮切换</h1> <div class="btn"> <put type="image" class="1" src="./img/cat.jpg"> <put type="image" class="2" src="./img/dog.jpg"> <put type="image" class="3" src="./img/elephant.jpg"> <put type="image" class="4" src="./img/lion.jpg"> <put type="image" class="5" src="./img/rabbit.jpg"> <put type="image" class="6" src="./img/monkey.jpg"> </div>
css
{ marg: 0; paddg: 0; } html, body { width: 100%; height: 100%; background: #66677c; text-align: center; } .contaer { width: 300px; height: 300px; marg: 50px auto 150px; perspective: 1200px; } .contaer .box { width: 300px; height: 300px; position: relative; transform-style: preserve-3d; transition: transform 0.5s; } .contaer .box .pic { position: absolute; left: 0; : 0; width: 300px; height: 300px; box-shadow: 0px 0px 5px #fff; } .contaer .box .pic img { width: 100%; height: 100%; cursor: poter; } .contaer .box .pic:nth-child(1) { transform: translateZ(150px); } .contaer .box .pic:nth-child(2) { transform: rotateY(-180deg) translateZ(150px); } .contaer .box .pic:nth-child(3) { transform: rotateY(90deg) translateZ(150px); } .contaer .box .pic:nth-child(4) { transform: rotateY(-90deg) translateZ(150px); } .contaer .box .pic:nth-child(5) { transform: rotateX(90deg) translateZ(150px); } .contaer .box .pic:nth-child(6) { transform: rotateX(-90deg) translateZ(150px); } h1 { color: #fff; font-size: 30px; marg-bottom: 30px; } .btn { display: grid; justify-content: center; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px; grid-gap: 15px; } .btn put { width: 100px; height: 100px; outle: none; border: 2px solid #fff; } .btn put: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.sPropagation(); var x = e.clientX; var y = e.clientY; document.addEventListener('mousemove',move); document.addEventListener('mouseup', up); function move(e){ e.preventDefault(); e.sPropagation(); 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); } }) })()
以上所述是长沙网络推广给大家介绍的基于css3实现可拖动的魔方3d效果 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!如果你觉得本文对你有帮助,欢迎网络推广网站推广转载,烦请注明出处,谢谢!