幻灯片/轮播

3D旋转木马特效JavaScript插件

面包多

3D旋转木马特效JavaScript插件-创客云
这是一款 js 3D 旋转木马特效插件。该插件通过 CSS3 和纯 js 来完成,可在中以 3D 旋转的方式来展示一组图片。

HTML 结构

  1. <div id="drag-container">
  2.   <div id="spin-container">
  3.   <img src="images/pexels-photo-206395.jpeg" alt="">
  4.   <img src="images/pexels-photo-1391498.jpeg" alt="">
  5.   <img src="images/pexels-photo-1382731.jpeg" alt="">
  6.   <img src="images/pexels-photo-1758144.jpeg" alt="">
  7.   <img src="images/pexels-photo-1382734.jpeg" alt="">
  8.   <img src="images/pexels-photo-1462636.jpeg" alt="">
  9.  
  10.   <a target="_blank" href="images/pexels-photo-139829.jpeg" rel="noopener noreferrer">
  11.     <img src="images/pexels-photo-139829.jpeg" alt="">
  12.   </a>
  13.  
  14.   <video controls autoplay="autoplay" loop>
  15.     <source src="" type="video/mp4">
  16.   </video>
  17.  
  18.   <p>3D Tiktok Carousel</p>
  19.   </div>
  20.   <div id="ground"></div>
  21. </div>
  22. <div id="music-container"></div>
也想出现在这里?联系我们
创客主机

CSS 样式

  1. * {
  2.   margin: 0;
  3.   padding: 0;
  4. }
  5.  
  6. html,
  7. body {
  8.   height: 100%;
  9. }
  10.  
  11. body {
  12.   overflow: hidden;
  13.   display: flex;
  14.   background: #111;
  15.   perspective: 1000px;
  16.   transform-style: preserve-3d;
  17. }
  18.  
  19. #drag-container, #spin-container {
  20.   position: relative;
  21.   display: flex;
  22.   margin: auto;
  23.   transform-style: preserve-3d;
  24.   transform: rotateX(-10deg);
  25. }
  26.  
  27. #drag-container img, #drag-container video {
  28.   transform-style: preserve-3d;
  29.   position: absolute;
  30.   left: 0;
  31.   top: 0;
  32.   width: 100%;
  33.   height: 100%;
  34.   line-height: 200px;
  35.   font-size: 50px;
  36.   text-align: center;
  37.   box-shadow: 0 0 8px #fff;
  38.   -webkit-box-reflect: below 10px linear-gradient(transparent, transparent, #0005);
  39. }
  40.  
  41. #drag-container img:hover, #drag-container video:hover {
  42.   box-shadow: 0 0 15px #fffd;
  43.   -webkit-box-reflect: below 10px linear-gradient(transparent, transparent, #0007);
  44. }
  45.  
  46. #drag-container p {
  47.   font-family: Serif;
  48.   position: absolute;
  49.   top: 100%;
  50.   left: 50%;
  51.   transform: translate(-50%,-50%) rotateX(90deg);
  52.   color: #fff;
  53. }
  54.  
  55. #ground {
  56.   width: 900px;
  57.   height: 900px;
  58.   position: absolute;
  59.   top: 100%;
  60.   left: 50%;
  61.   transform: translate(-50%,-50%) rotateX(90deg);
  62.   background: -webkit-radial-gradient(center center, farthest-side , #9993, transparent);
  63. }
  64.  
  65. #music-container {
  66.   position: absolute;
  67.   top: 0;
  68.   left: 0;
  69. }
  70.  
  71. #carousel-container {
  72.   width: 100%;
  73.   height: 100%;
  74. }
  75.  
  76. @keyframes spin {
  77.   from{
  78.     transform: rotateY(0deg);
  79.   } to{
  80.     transform: rotateY(360deg);
  81.   }
  82. }
  83. @keyframes spinRevert {
  84.   from{
  85.     transform: rotateY(360deg);
  86.   } to{
  87.     transform: rotateY(0deg);
  88.   }
  89. }

JavaScript

  1. var radius = 240; 
  2. var autoRotate = true; 
  3. var rotateSpeed = -60; 
  4. var imgWidth = 120; 
  5. var imgHeight = 170; 
  6.  
  7. var bgMusicURL = '';
  8. var bgMusicControls = true; 
  9.  
  10. // ===================== start =======================
  11. setTimeout(init, 100);
  12.  
  13. var obox = document.getElementById('drag-container');
  14. var ospin = document.getElementById('spin-container');
  15. var aImg = ospin.getElementsByTagName('img');
  16. var aVid = ospin.getElementsByTagName('video');
  17. var aEle = [...aImg, ...aVid]; 
  18.  
  19. ospin.style.width = imgWidth + "px";
  20. ospin.style.height = imgHeight + "px";
  21.  
  22. var ground = document.getElementById('ground');
  23. ground.style.width = radius * 3 + "px";
  24. ground.style.height = radius * 3 + "px";
  25.  
  26. function init(delayTime) {
  27.   for (var i = 0; i < aEle.length; i++) {
  28.     aEle[i].style.transform = "rotateY(" + (i * (360 / aEle.length)) + "deg) translateZ(" + radius + "px)";
  29.     aEle[i].style.transition = "transform 1s";
  30.     aEle[i].style.transitionDelay = delayTime || (aEle.length - i) / 4 + "s";
  31.   }
  32. }
  33.  
  34. function applyTranform(obj) {
  35.   if(tY > 180) tY = 180;
  36.   if(tY < 0) tY = 0;
  37.  
  38.   obj.style.transform = "rotateX(" + (-tY) + "deg) rotateY(" + (tX) + "deg)";
  39. }
  40.  
  41. function playSpin(yes) {
  42.   ospin.style.animationPlayState = (yes?'running':'paused');
  43. }
  44.  
  45. var sX, sY, nX, nY, desX = 0,
  46.     desY = 0,
  47.     tX = 0,
  48.     tY = 10;
  49.  
  50. if (autoRotate) {
  51.   var animationName = (rotateSpeed > 0 ? 'spin' : 'spinRevert');
  52.   ospin.style.animation = `${animationName} ${Math.abs(rotateSpeed)}s infinite linear`;
  53. }
  54.  
  55. if (bgMusicURL) {
  56.   document.getElementById('music-container').innerHTML += `
  57. <audio src="${bgMusicURL}" ${bgMusicControls? 'controls': ''} autoplay loop>    
  58. <p>If you are reading this, it is because your browser does not support the audio element.</p>
  59. </audio>
  60. `;
  61. }
  62.  
  63. if (mobilecheck()) {
  64.   // ==================== Touch Events ====================
  65.   document.ontouchstart = function(e) {
  66.     clearInterval(obox.timer);
  67.     e = e || window.event;
  68.     var sX = e.touches[0].clientX,
  69.         sY = e.touches[0].clientY;
  70.  
  71.     this.ontouchmove = function(e) {
  72.       e = e || window.event;
  73.       var nX = e.touches[0].clientX,
  74.           nY = e.touches[0].clientY;
  75.       desX = nX - sX;
  76.       desY = nY - sY;
  77.       tX += desX * 0.1;
  78.       tY += desY * 0.1;
  79.       applyTranform(obox);
  80.       sX = nX;
  81.       sY = nY;
  82.     }
  83.  
  84.     this.ontouchend = function(e) {
  85.       this.ontouchmove = this.ontouchend = null;
  86.       obox.timer = setInterval(function() {
  87.         desX *= 0.95;
  88.         desY *= 0.95;
  89.         tX += desX * 0.1;
  90.         tY += desY * 0.1;
  91.         applyTranform(obox);
  92.         playSpin(false);
  93.         if (Math.abs(desX) < 0.5 && Math.abs(desY) < 0.5) {
  94.           clearInterval(obox.timer);
  95.           playSpin(true);
  96.         }
  97.       }, 17);
  98.     }
  99.  
  100.     // return false;
  101.   }
  102. } else {
  103.   // ==================== Mouse Events ====================
  104.   document.onmousedown = function(e) {
  105.     clearInterval(obox.timer);
  106.     e = e || window.event;
  107.     var sX = e.clientX,
  108.         sY = e.clientY;
  109.  
  110.     this.onmousemove = function(e) {
  111.       e = e || window.event;
  112.       var nX = e.clientX,
  113.           nY = e.clientY;
  114.       desX = nX - sX;
  115.       desY = nY - sY;
  116.       tX += desX * 0.1;
  117.       tY += desY * 0.1;
  118.       applyTranform(obox);
  119.       sX = nX;
  120.       sY = nY;
  121.     }
  122.  
  123.     this.onmouseup = function(e) {
  124.       this.onmousemove = this.onmouseup = null;
  125.       obox.timer = setInterval(function() {
  126.         desX *= 0.95;
  127.         desY *= 0.95;
  128.         tX += desX * 0.1;
  129.         tY += desY * 0.1;
  130.         applyTranform(obox);
  131.         playSpin(false);
  132.         if (Math.abs(desX) < 0.5 && Math.abs(desY) < 0.5) {
  133.           clearInterval(obox.timer);
  134.           playSpin(true);
  135.         }
  136.       }, 13);
  137.     }
  138.  
  139.     return false;
  140.   }
  141.   document.onmousewheel = function(e) {
  142.     e = e || window.event;
  143.     var d = e.wheelDelta / 20 || -e.detail;
  144.     radius += d;
  145.     init(1);
  146.   };
  147. }

Codepen 网址:https://codepen.io/hoanghien0410/pen/MMPaqm

(1)

本文由 创客云 作者:PetitQ 发表,转载请注明来源!

阿里云

热评文章

发表评论

精彩推荐

Supro - 极简电商WordPress汉化主题

Envato Affiliates

本站承接 WordPress / DedeCMS / ThinkPHP 等
系统建站、仿站、开发、定制等业务!

Hi, 如果您有主题插件代购(30-600元)汉化等建站相关业务,可以 跟我联系 哦!
欢迎投稿
嘿,欢迎咨询!