温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JavaScript中怎么实现拖动缓动效果

发布时间:2021-08-09 17:22:25 来源:亿速云 阅读:110 作者:Leah 栏目:开发技术

这篇文章给大家介绍JavaScript中怎么实现拖动缓动效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

首先,绑定鼠标按下事件,来获取到鼠标基于浏览器窗口左上角的xy平面二维坐标。

然后,绑定move事件,在move事件回调内获取到鼠标拖拽的坐标,和按下坐标相减,求出拖拽的距离。

然后,我们需要通过一定比例,将拖拽的像素转换为旋转角度我这里设置的比例是,鼠标横向拖拽10像素,那模型沿3d的Y轴坐标就旋转5度,鼠标纵向拖拽10像素,模型沿3d世界的X轴坐标旋转1度,并且还设置了范围,即沿x轴旋转再-45度到45度之间

function onDocumentMouseMove(event) {    mouseX = event.clientX;    mouseY = event.clientY;    targetRotationX = targetRotationOnMouseDownX + (mouseX - mouseXOnMouseDownX) * 0.5;    targetRotationY = Math.min(Math.max((targetRotationOnMouseDownY - (mouseY - mouseXOnMouseDownY) * 0.1), -45), 45); //拖拽后的目标位置  }

上面获取到目标角度,重点来了,如何实现惰性旋转呢?

通过上面思路,我们知道了目标角度,那么直接设置目标角度,肯定就没有这种想要的效果了,那么如何实现这种惰性效果呢?

接下来,我们需要一个专门实现动画的requestAnimationFrame方法,这个方法是闲时运行,最大根据性能能够达到60帧每秒,有好多小伙伴感觉一直递归运行会不会卡顿,或者影响性能。那是你多虑了,这个方法会根据当前页面性能进行减帧,保证页面流畅运行。

我们有了这个以后,然后做什么呢,就是用来实现缓动,在每一帧里面,获取到目标角度和当前角度的角度差,然后每一次只选择总进度的百分之10 ,然后你会发现选择离目标角度越近,越慢,体验效果也是非常的棒。

而且在运行中,角度也会无限制的接近目标角度,当前demo是通过css3d来实现的:

function animate() {    requestAnimationFrame(animate);    rotateY += (targetRotationX - rotateY) * 0.1;    rotateX += (targetRotationY - rotateX) * 0.1;    box.style.transform = 'rotateY(' + rotateY + 'deg)';    item.style.transform = 'rotateX(' + rotateX + 'deg)';  }

案例全部代码

<!DOCTYPE html><html lang="zh"><head>  <meta charset="UTF-8">  <title>css3d翻转</title>  <style>    * {      padding: 0;      margin: 0;    }    body {      display: flex;      justify-content: center;      align-items: center;      height: 100vh;      overflow: hidden;      perspective: 1000px;    }    .item {      width: 50vw;      height: 50vh;      transform: rotateX(-50deg);      perspective: 5000px;      transform-style: preserve-3d;    }    .box {      background: #abb9c5;      width: 100%;      height: 100%;      transform-style: preserve-3d;      position: relative;    }    .font,    .back {      position: absolute;      top: 0;      left: 0;      width: 100%;      height: 100%;      text-align: center;      line-height: 50vh;      background: #4cae4c;      backface-visibility: hidden;    }    .back {      background: #62ebff;      transform: rotateY(180deg);    }  </style></head><body>  <!--item 可以触发翻转的区域-->  <p class="item">    <!--box 可以翻转的容器-->    <p class="box">      <!--font 默认显示的正面-->      <p class="font">正面</p>      <!--back 背面-->      <p class="back">背面</p>    </p>  </p></body><script>  var targetRotationX = 0;  var targetRotationY = 0;  var targetRotationOnMouseDownX = 0;  var targetRotationOnMouseDownY = 0;  var mouseX = 0;  var mouseY = 0;  var mouseXOnMouseDownX = 0;  var mouseXOnMouseDownY = 0;  var box = document.querySelector('.box');  var item = document.querySelector('.item');  var rotateY = 0;  var rotateX = 0;  init();  animate();  function init() {    // EVENTS    document.addEventListener('mousedown', onDocumentMouseDown, false);    document.addEventListener('touchstart', onDocumentTouchStart, false);    document.addEventListener('touchmove', onDocumentTouchMove, false);  }  function onDocumentMouseDown(event) {    event.preventDefault();    document.addEventListener('mousemove', onDocumentMouseMove, false);    document.addEventListener('mouseup', onDocumentMouseUp, false);    mouseXOnMouseDownX = event.clientX;    mouseXOnMouseDownY = event.clientY;    targetRotationOnMouseDownX = targetRotationX;    targetRotationOnMouseDownY = targetRotationY;  }  function onDocumentMouseMove(event) {    mouseX = event.clientX;    mouseY = event.clientY;    targetRotationX = targetRotationOnMouseDownX + (mouseX - mouseXOnMouseDownX) * 0.5;    targetRotationY = Math.min(Math.max((targetRotationOnMouseDownY - (mouseY - mouseXOnMouseDownY) * 0.1), -45), 45); //拖拽后的目标位置  }  function onDocumentMouseUp() {    document.removeEventListener('mousemove', onDocumentMouseMove, false);    document.removeEventListener('mouseup', onDocumentMouseUp, false);  }  function onDocumentTouchStart(event) {    event.preventDefault();    if (event.touches.length === 1) {      mouseXOnMouseDownX = event.touches[0].pageX;      mouseXOnMouseDownY = event.touches[0].pageY;      targetRotationOnMouseDownX = targetRotationX;      targetRotationOnMouseDownY = targetRotationY;    }  }  function onDocumentTouchMove(event) {    event.preventDefault();    if (event.touches.length === 1) {      mouseX = event.touches[0].pageX;      mouseY = event.touches[0].pageY;      targetRotationX = targetRotationOnMouseDownX + (mouseX - mouseXOnMouseDownX) * 0.5;      targetRotationY = Math.min(Math.max((targetRotationOnMouseDownY - (mouseY - mouseXOnMouseDownY) * 0.1), -45), 45); //拖拽后的目标位置    }  }  function animate() {    requestAnimationFrame(animate);    rotateY += (targetRotationX - rotateY) * 0.1;    rotateX += (targetRotationY - rotateX) * 0.1;    box.style.transform = 'rotateY(' + rotateY + 'deg)';    item.style.transform = 'rotateX(' + rotateX + 'deg)';  }</script></html>

关于JavaScript中怎么实现拖动缓动效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI