国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

??
? ?? ??? ?? ??
?? ??
??? ?? ? ??
定義三組坐標(biāo)
注冊 mousedown 事件
????PS: ??? ???? ?? ??? ? ???? Point 1? ? ?????. OriginalPosition ?? elementPosition, ??? 2? ???? ??? ?? ??? ?????. ??? ??? 1? ? ??? ??? 2? mousedownOffset;????mousedown ??? ?????????? ???? ??? ? ???? ??? mousedown ???? ???? ???? ???? ??? ?????. ??? ??? ??? ???? ??? ??? ???. ???????? ??? mousemove, mouseup? ???? ????? ???? ?? ??? ? ????. ???????? ??? ? ?? ???? ??? ?? ??? ????? mousedown ???? ?????. ?? ??? ???? ? mousedown ???? ?????. ??%%PRE_BLOCK_3%%

實現(xiàn)拖拽的核心" >?? ? ?? ?? ??? ?????. ?????? ?? ??? ?? ??(originalPosition), ???? ???? ? ???? ?? ??? ?????. ??? ??? ? ??? ??(mousedownOffset)? ??? ??? ? ????? ?????? ?? ??(elementPosition). ??????? ?? ?? ??? ?????. ??? ???? ?? ??? ????. ??? ? ??? ? ???? ??? ??? ?????? ? ?????. ?? %%PRE_BLOCK_3%%????? ??? ???? ?? ?? ????. ? ??? ??? ???? ??? ?? ??? ????, ??? ? ???? ??? ????. - ??? ?? ??? ?? ??: ??%%PRE_BLOCK_4%%????? ??? ? ????? ?????? ??, ??? ???? ?? ??? ????, ???? originalPosition? ???? ???. code>mousemove ???? ???? ???? ??? ?? - mousedownOffset? ?????: ??%%PRE_BLOCK_5%%??????PS: ??? ???? ?? ??? ? ???? Point 1? ? ?????. OriginalPosition ?? elementPosition, ??? 2? ???? ??? ?? ??? ?????. ??? ??? 1? ? ??? ??? 2? mousedownOffset;????mousedown ??? ?????????? ???? ??? ? ???? ??? mousedown ???? ???? ???? ???? ??? ?????. ??? ??? ??? ???? ??? ??? ???. ???????? ??? mousemove, mouseup? ???? ????? ???? ?? ??? ? ????. ???????? ??? ? ?? ???? ??? ?? ??? ????? mousedown ???? ?????. ?? ??? ???? ? mousedown ???? ?????. ??%%PRE_BLOCK_3%%

實現(xiàn)拖拽的核心

補(bǔ)充其它部分代碼和演示
總結(jié)
? ? ????? View.js Vue3? ???? ??? ?? ??? ??? ???? ??? ???? ??????.

Vue3? ???? ??? ?? ??? ??? ???? ??? ???? ??????.

Mar 30, 2023 pm 08:57 PM
??? ?? vue.js

?? ??? ??? ???? ??? ?????? ?? ?? Vue3? ???? ??? ?? ??? ??? ???? ??? ???? ???? ??? ?? ?? ?? ???? ??? ?? ??? ??? ????.

Vue3? ???? ??? ?? ??? ??? ???? ??? ???? ??????.

? ?? ??? ?? ??

?? ??

?? ???? JavaScript ???? ?? ?? ?? ??? ??? ???? ??? ?? ?? ?????. ?? Zhongcai? ? ???? ?? ???? Vue3? ?? ??? ????? ??? ??????? ??? ? ? ????? ???? ??????. [?? ?? ??: vuejs ??? ????, ? ??? ?? ??]

PS: Vue3 ??? ?? ???? centering ??? ??? ??? ??? ? ??? ?????! ! !

??? ?? ? ??

?? ???? ??? ? mouse ???? ?? ???? ??? ?? ? ????. ?? ???? ??? ? ??? ?????. ?? ??? MouseEvent? clientX ? clientY???. ??? ? ? ??? ?? ????? ??? ?????. mouse 事件,在 mouse 事件的回調(diào)函數(shù)中可以得到當(dāng)前事件發(fā)生時元素的位置,對應(yīng)的屬性是 MouseEvent 中的 clientXclientY,我們后續(xù)將通過讀取這兩個屬性來實時更新元素的位置。

元素的移動推薦優(yōu)先使用 transform 中的 translate 實現(xiàn),相比于修改元素的 top、left 屬性來說不會造成元素布局的改變,避免了回流和重繪造成的性能影響。

PS:在 MDN 有一份關(guān)于translate的使用和體驗,可以感受一下。

定義三組坐標(biāo)

分別定義用來記錄元素初始位置的一組坐標(biāo)(originalPosition)、元素被按下時指針在元素上的坐標(biāo)(mousedownOffset)和元素在移動時實時更新的一組坐標(biāo)(elementPosition)。

記錄元素初始位置的坐標(biāo),原點位于頁面左上角,用來在初始化和被拖拽結(jié)束后還原被拖拽元素的位置,固定值不發(fā)生變化:

const originalPosition = reactive({
  x: 10,
  y: 10,
})

元素被按下時指針在元素上的坐標(biāo),原點位于被拖拽元素的左上角,通過按下時指針的坐標(biāo) - 元素初始的偏移位置得到:

const mousedownOffset = reactive({
  x: 0,
  y: 0,
})

元素在移動時實時更新的坐標(biāo),原點位于頁面左上角,初始值應(yīng)該同 originalPosition ,在 mousemove 事件發(fā)生時,通過指針的實時坐標(biāo) - mousedownOffset 得到:

const elementPosition = reactive({
  x: 0,
  y: 0,
})

PS:當(dāng)原點是頁面左上角時在圖中的1號點表示 originalPositionelementPosition,2號點表示指針按下時的坐標(biāo),當(dāng)原點是1號點時在圖中的2號點表示 mousedownOffset

注冊 mousedown 事件

在實現(xiàn)元素拖拽時,僅需要給被拖拽的元素添加 mousedown 事件即可,監(jiān)聽事件使用完后記得要清楚掉,成對出現(xiàn)的習(xí)慣一定要養(yǎng)成。

如果你把 mousemovemouseup 都添加到被拖拽的元素上,你會發(fā)現(xiàn)有脫離控制的現(xiàn)象發(fā)生。

在頁面加載完成后首先要重置一下被拖拽元素的默認(rèn)位置,并增加 mousedown 事件,在組件卸載后刪除 mousedown

?? ? ??? ???? ?? ??? ?? ????? transform? translate ??? ???? ?? ????. code> ?? > ??? ?? ????? ???? ???? ???? ? ?? ???? ?? ?? ??? ?????. ????PS: MDN???? ??? ??? ??? ?? ??? ??? ???? ? ????. ??

?? ? ?? ?? ??? ?????. ?????? ?? ??? ?? ??(originalPosition), ???? ???? ? ???? ?? ??? ?????. ??? ??? ? ??? ??(mousedownOffset)? ??? ??? ? ????? ?????? ?? ??(elementPosition). ??????? ?? ?? ??? ?????. ??? ???? ?? ??? ????. ??? ? ??? ? ???? ??? ??? ?????? ? ?????. ??
const restore = () => {
  elementPosition.x = originalPosition.x;
  elementPosition.y = originalPosition.y;
}

onMounted(() => {
  restore();
  floatButton.value.addEventListener('mousedown', onMousedown, true);
})

onUnmounted(() => {
  floatButton.value.removeEventListener('mousedown', onMousedown, true);
})
????? ??? ???? ?? ?? ????. ? ??? ??? ???? ??? ?? ??? ????, ??? ? ???? ??? ????. - ??? ?? ??? ?? ??: ??
const onMousedown = (event: MouseEvent) => {
  event.stopPropagation();
  
  mousedownOffset.x = event.clientX - originalPosition.x;
  mousedownOffset.y = event.clientY - originalPosition.y;
  
  document.addEventListener('mousemove', onMousemove, true);
  document.addEventListener('mouseup', onMouseup, true);
}
????? ??? ? ????? ?????? ??, ??? ???? ?? ??? ????, ???? originalPosition? ???? ???. code>mousemove
???? ???? ???? ??? ?? - mousedownOffset? ?????: ??
const onMousemove = (event: MouseEvent) => {
  event.stopPropagation();
  
  elementPosition.x = event.clientX - mousedownOffset.x;
  elementPosition.y = event.clientY - mousedownOffset.y;
}
??????PS: ??? ???? ?? ??? ? ???? Point 1? ? ?????. OriginalPosition ?? elementPosition, ??? 2? ???? ??? ?? ??? ?????. ??? ??? 1? ? ??? ??? 2? mousedownOffset;????mousedown ??? ?????????? ???? ??? ? ???? ??? mousedown</code? ???? ???. > ???? ???? ???? ???? ??? ?????. ??? ??? ??? ???? ??? ??? ???. ???????? ??? <code>mousemove, mouseup? ???? ????? ???? ?? ??? ? ????. ???????? ??? ? ?? ???? ??? ?? ??? ????? mousedown ???? ?????. ?? ??? ???? ? mousedown ???? ?????. ??
const restore = () => {
  elementPosition.x = originalPosition.x;
  elementPosition.y = originalPosition.y;
}

onMounted(() => {
  restore();
  floatButton.value.addEventListener(&#39;mousedown&#39;, onMousedown, true);
})

onUnmounted(() => {
  floatButton.value.removeEventListener(&#39;mousedown&#39;, onMousedown, true);
})

實現(xiàn)拖拽的核心

選擇 Vuejs 的原因就是因為其是 MVVM 型框架,我們關(guān)注點在聲明上,內(nèi)部的運(yùn)轉(zhuǎn)機(jī)制有框架負(fù)責(zé),所以在下面的事件處理上就只需要在對應(yīng)的事件中去更新一開始聲明的三組坐標(biāo)就可以了。

onMousedown 時,通過指針?biāo)诘淖鴺?biāo) - 被拖拽元素初始位置的坐標(biāo)得到指針此時在被拖拽元素上的坐標(biāo),onMousedown 時要為 document 添加 mousemovemouseup 事件:

const onMousedown = (event: MouseEvent) => {
  event.stopPropagation();
  
  mousedownOffset.x = event.clientX - originalPosition.x;
  mousedownOffset.y = event.clientY - originalPosition.y;
  
  document.addEventListener(&#39;mousemove&#39;, onMousemove, true);
  document.addEventListener(&#39;mouseup&#39;, onMouseup, true);
}

onMousemove時,通過指針?biāo)诘淖鴺?biāo) - 指針在被拖拽元素上的位置得到被拖拽元素左上角距離頁面左上角的距離,并更新到 elementPosition

const onMousemove = (event: MouseEvent) => {
  event.stopPropagation();
  
  elementPosition.x = event.clientX - mousedownOffset.x;
  elementPosition.y = event.clientY - mousedownOffset.y;
}

onMouseup時,主要做的就是為 document 移除在 onMousemove 時注冊的兩個事件,要注意的是移除的事件要是同一個事件,也就是引用一致的事件,推薦將對應(yīng)的處理事件賦值給一個變量使用,最后可以在拖拽結(jié)束后還原被拖拽元素的位置:

const onMouseup = (event: MouseEvent) => {
  event.stopPropagation();
  document.removeEventListener(&#39;mousemove&#39;, onMousemove, true);
  document.removeEventListener(&#39;mouseup&#39;, onMouseup, true);
  restore();
}

補(bǔ)充其它部分代碼和演示

<div 
 ref="floatButton"
 class="float-button"
 :style="{
    &#39;transition-duration&#39;: &#39;0.1s&#39;,
    transform: `translate(${elementPosition.x}px, ${elementPosition.y}px)`
  }">
</div>
.float-button {
  position: absolute;
  width: 42px;
  height: 42px;
  background: red;
  border-radius: 5px;
  user-select: none;
  background-image: url(../assets/taobao.svg);
  background-size: cover;
}

總結(jié)

使用 mousemove、translate 在 Vue3 中實現(xiàn)可以隨意拖拽的 Icon 的案例就完成了,在本次案例中需要認(rèn)真思考對應(yīng)的幾個坐標(biāo)和移動時坐標(biāo)如何更新,事件的使用要成對出現(xiàn),如何在這個拖拽的 Icon 上增加點擊事件時還需要多做一些處理,有答案的朋友可以留下你的想法~

(學(xué)習(xí)視頻分享:vuejs入門教程、編程基礎(chǔ)視頻

? ??? Vue3? ???? ??? ?? ??? ??? ???? ??? ???? ??????.? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

??? ????
1601
29
PHP ????
1502
276
???
PHP? Vue: ????? ?? ??? ??? ?? PHP? Vue: ????? ?? ??? ??? ?? Mar 16, 2024 pm 12:09 PM

PHP? Vue: ????? ?? ??? ??? ?? ??? ???? ??? ???? ??? ????? ??? ?? ? ????? ????. ???? ? ??? ? ?????? ??? ?? ?? ??? ?? ? ???? ?? ??? ?? ???? ?? ????? ??? ??? ???? ??? ? ??? ?????? ???? ???. ????? ?? ??? ? ?? ??? ??? PHP? Vue.js? ?? ???? ??? ???? ? ? ????. ? ????? ??? ? ? ??? ? ? ???? ??? ? ??? PHP? Vue? ??? ??? ?? ??? ???????.

Go ?? ????? ?? ??: ????? ??? ?? ??? ?? Go ?? ????? ?? ??: ????? ??? ?? ??? ?? Mar 28, 2024 pm 01:06 PM

??? ???? ????? ??? Go ??? ??? ?? ???? ?? ?????. ??? Go ??? ????? ??? ????? ??? ?? ????. ??? ????? ??? Go ??? ???? ???? ??? ?? ??? ????? ??? ??? ??? ?? ????. ? ????? ????? ??? Go ??? ??? ? ?? ???? ???? ??? ? ??? ? ? ??? ? ??? ???? ?? ??? ?????. ???? ????? ????? ??? ?????? ???? ?? JavaScript, HTML, CSS? ???? ??? ????.

vue.js vs. React : ???? ? ?? ?? vue.js vs. React : ???? ? ?? ?? Apr 09, 2025 am 12:01 AM

vue.js? ??? ???? ? ?? ??? ??? ?? React? ?? ??? ?? ????? ?????. 1) vue.js? ???? ?? ?? ?????? ???? ??? ?? ??? ?????. 2) React? ? ??? ???? ??? ??? ??? ? ??? ??? ????? ????? ?????.

????? ???? ?? ?? ?? ????? ???? ?? ?? ?? Mar 19, 2024 pm 02:24 PM

????? ?? ????? ???? ??? HTML/CSS ??, JavaScript ??, ????? ? ?????, ???? ??, ???? ? ??? ??, ?? ???, ??? ??? ??, ????? ?????, ??? ??, ??? ?? ? ???. ??? ??? ???? ??? ??, ???? ??, ?? ??? ?? ??? ???? ?? ???????. ??? ???? ??? ??? ???? ??? ? ??? ?? ??? ?? ??? ??? ???? ???.

Golang? ????? ??? ??: Golang? ????? ???? ?? ??? ??? ?????. Golang? ????? ??? ??: Golang? ????? ???? ?? ??? ??? ?????. Mar 19, 2024 pm 06:15 PM

Golang? ????? ??? ??: Golang? ????? ???? ?? ??? ??? ????? ???? ?? ??? ?????. ???? ??? ??????? ??? ???? ?? ????? ??? ?? ? ????? ????. ? ????? ??? ??? ????? ??? Golang? ??? ??? ? ? ????. ? ????? Golang? ????? ??? ??? ????? ???? ?? ?? ??? ?? ????? ????? ???? ?????. ????? ???? Golang? ??? ????? ???? ??? ?? ????.

vue.js? ???? ?????? vue.js? ???? ?????? Apr 04, 2025 am 12:02 AM

vue.js? ?? JavaScript Foundation? ?? ?????? ???? ??? ????. 1) ??? ? ??? ?? ? ???? ?? ????? ??????. 2) ?? ?? ?? ??? ?? ????? ????? ????. 3) ?? ??? ?? ? ?? ??? ?????. 4) vuedevtools? ?? ???? ??? ??? ? ? ????. 5) V-IF/V- ? ? ?? ?? ??? ?? ?? ??? ? ?? ??? ?????? ???? ???? ? ????.

Vue? ??? ?? ?? ???? ?????? Vue? ??? ?? ?? ???? ?????? Apr 03, 2025 am 12:07 AM

vue.js? ?? ??? ?? ??? ?????. 1) ??? ????? ? ?? ??? ?? ???? ??? ??? ? ??? ??? JavaScript ??? ?????. 2) vue.js? ??? ?? ? ??? ?????, ???? ???? ?? ???? ???????. 3) ?? ?? ??? ???? UI? ????? ??? ??? ?? ??? ?? ? ? ????.

React? ?? ?? ?? : ??? ?? ?? React? ?? ?? ?? : ??? ?? ?? Apr 18, 2025 am 12:15 AM

React? ?? ???? ?? ??? ??, ?? ?? ? ?? DOM? ?????. 1) ?? ???? ?? ????? ?? UI? ??? ??? ???? ??? ?? ?? ???? ?? ?? ???? ??????. 2) ?? ??? ?? ? ??? ?? ?? ???? ???? ?? UI ????? ??????. 3) ?? DOM ??? ??, ????? DOM ??? ?? ??? ???? UI? ????????.

See all articles