Teach you a trick to achieve 3D picture demonstration

醉折花枝作酒筹
Release: 2021-04-15 11:59:43
Original
2060 people have browsed it

This article will give you a detailed introduction to the method of making 3D pictures. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Teach you a trick to achieve 3D picture demonstration

1. Create a parent container and stack all the photos together.

The code is as follows (html):

  <div id="darg-container" class="darg">
      <!-- 父容器,相当于把所有图片都放在一起 -->
    <div id="spin-container">
      <img src="1.jpg" alt="">
      <img src="2.jpg" alt="">
      <img src="3.jpg" alt="">
      <img src="4.jpg" alt="">
      <img src="5.jpg" alt="">
      <img src="6.jpg" alt="">
      <img src="8.jpg" alt="">

      <a target="_blank" href="7.jpg">
        <img src="7.jpg" alt="">
      </a>
  

      <!-- <video controls autoplay="autoplay" loop>
        <source src="8.jpg" type="video/mp4">
      </video> -->

      <p>3D Tiktok Carousel</p>
    </div>
    <div id="ground"></div>
  </div>
Copy after login

2. Add rotation animation to all photos

The code is as follows (js):

function init(delayTime) {
  // 给所有的图片加动画
  for (var i = 0; i < aEle.length; i++) {
    aEle[i].style.transform = "rotateY(" + (i * (360 / aEle.length)) + "deg) translateZ(" + radius + "px)"
    aEle[i].style.transition = "transform 1s"
    aEle[i].style.transitionDelay = delayTime || (aEle.length - i) / 4 + &#39;s&#39;
  }
}
setTimeout(init, 1000)
Copy after login

3. Monitor mouse events

The code is as follows (js):

// 滚轮滚动
// 监听鼠标滚轮事件,该函数不用调用直接生效
document.onmousewheel = function(e){
    // console.log(e)
    e = e || window.event
    var d  = e.wheelDelta / 10 || -e.detail
    radius += d
    init(1)

} 
var sX,sY,nX,nY,desX = 0 , desY = 0, tX = 0,tY = 0;
// 鼠标拖动页面
document.onpointerdown = function(e){
    // console.log(e);
    e = e || window.event//防止出错,如果e不存在,则让window.event为e
    var sX = e.clientX,
    sY = e.clientY
    //监听鼠标移动函数
    this.onpointermove = function(e){
        console.log(e);
        e = e || window.event//防止出错,如果e不存在,则让window.event为e
        var nX = e.clientX,
            nY = e.clientY;
        desX = nX - sX;//在x轴上滑动的距离
        desY = nY - sY;
        tX += desX * 0.1
        tY += desY * 0.1
        // 让页面跟着鼠标动起来
        applyTransform(oDarg)
    }
    this.onpointerup = function(e){
        //每个多久实现一次setInterval
        oDarg.timer = setInterval(function(){
            desX *= 0.95
            desY *= 0.95
            tX += desX * 0.1
            tY += desY * 0.1
            applyTransform(oDarg)
            playSpin(false)
            if(Math.abs(desX) < 0.5 && Math.abs(desY) < 0.5){
                clearInterval(oDarg.timer)
                playSpin(true)
            }
        },17) 
        this.onpointermove = this.onpointerup = null 
    }
    return false
}
function applyTransform(obj){
    if(tY > 180)tY = 180
    if(tY < 0)tY = 0
    obj.style.transform = `rotateX(${-tY}deg) rotateY(${tX}deg)`
}

function playSpin(yes){
    oSpin.style.animationPlayState = (yes ? 'running' : 'paused')
}
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of Teach you a trick to achieve 3D picture demonstration. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
3d
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template