溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaScript中怎么實現(xiàn)拖動緩動效果

發(fā)布時間:2021-08-09 17:22:25 來源:億速云 閱讀:123 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹JavaScript中怎么實現(xiàn)拖動緩動效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

首先,綁定鼠標(biāo)按下事件,來獲取到鼠標(biāo)基于瀏覽器窗口左上角的xy平面二維坐標(biāo)。

然后,綁定move事件,在move事件回調(diào)內(nèi)獲取到鼠標(biāo)拖拽的坐標(biāo),和按下坐標(biāo)相減,求出拖拽的距離。

然后,我們需要通過一定比例,將拖拽的像素轉(zhuǎn)換為旋轉(zhuǎn)角度我這里設(shè)置的比例是,鼠標(biāo)橫向拖拽10像素,那模型沿3d的Y軸坐標(biāo)就旋轉(zhuǎn)5度,鼠標(biāo)縱向拖拽10像素,模型沿3d世界的X軸坐標(biāo)旋轉(zhuǎn)1度,并且還設(shè)置了范圍,即沿x軸旋轉(zhuǎn)再-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); //拖拽后的目標(biāo)位置  }

上面獲取到目標(biāo)角度,重點來了,如何實現(xiàn)惰性旋轉(zhuǎn)呢?

通過上面思路,我們知道了目標(biāo)角度,那么直接設(shè)置目標(biāo)角度,肯定就沒有這種想要的效果了,那么如何實現(xiàn)這種惰性效果呢?

接下來,我們需要一個專門實現(xiàn)動畫的requestAnimationFrame方法,這個方法是閑時運行,最大根據(jù)性能能夠達(dá)到60幀每秒,有好多小伙伴感覺一直遞歸運行會不會卡頓,或者影響性能。那是你多慮了,這個方法會根據(jù)當(dāng)前頁面性能進(jìn)行減幀,保證頁面流暢運行。

我們有了這個以后,然后做什么呢,就是用來實現(xiàn)緩動,在每一幀里面,獲取到目標(biāo)角度和當(dāng)前角度的角度差,然后每一次只選擇總進(jìn)度的百分之10 ,然后你會發(fā)現(xiàn)選擇離目標(biāo)角度越近,越慢,體驗效果也是非常的棒。

而且在運行中,角度也會無限制的接近目標(biāo)角度,當(dāng)前demo是通過css3d來實現(xiàn)的:

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翻轉(zhuǎn)</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 可以觸發(fā)翻轉(zhuǎn)的區(qū)域-->  <p class="item">    <!--box 可以翻轉(zhuǎn)的容器-->    <p class="box">      <!--font 默認(rèn)顯示的正面-->      <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); //拖拽后的目標(biāo)位置  }  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); //拖拽后的目標(biāo)位置    }  }  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>

關(guān)于JavaScript中怎么實現(xiàn)拖動緩動效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI