vue+ts實(shí)現(xiàn)元素鼠標(biāo)拖動(dòng)效果
本文實(shí)例為大家分享了vue+ts實(shí)現(xiàn)元素鼠標(biāo)拖動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果
相關(guān)使用屬性
// clientX 鼠標(biāo)相對于瀏覽器左上角x軸的坐標(biāo); 不隨滾動(dòng)條滾動(dòng)而改變; // clientY 鼠標(biāo)相對于瀏覽器左上角y軸的坐標(biāo); 不隨滾動(dòng)條滾動(dòng)而改變; // element.offsetTop 指 element距離上方或上層控件的位置,整型,單位像素。 // element.offsetLeft 指 element距離左方或上層控件的位置,整型,單位像素。 // element.offsetWidth 指 element控件自身的寬度,整型,單位像素。 // element.offsetHeight 指 element控件自身的高度,整型,單位像素。 // clientHeigh = height + 上下padding // clientWidth = width+左右padding
實(shí)現(xiàn)完整代碼
<template> <div class="to-do-list" ref="parentBox"> <div class="search-title"> <h1 class="add-bold left-box" style="margin-left:35px"> <a-icon type="unordered-list" style="margin-right: 10px;" />元素拖動(dòng) </h1> </div> <a-button ref="moveBtn" style="width: 100px;height: 40px;transition:none" class="move-btn" type="primary" >拖動(dòng)按鈕</a-button > </div> </template> <script lang="ts"> import { Component, Emit, Inject, Prop, Ref, Vue, Watch } from 'vue-property-decorator'; @Component({ components: {}, }) export default class BriberyInformation extends Vue { @Ref() readonly moveBtn; @Ref() readonly parentBox; btnDown() { let box = this.moveBtn.$el; //獲取button的盒子dom元素 let parentBox = this.parentBox; //獲取button父級元素的dom元素 let parentH = parentBox.clientHeight; //獲取button父級元素的height let parentW = parentBox.clientWidth; //獲取button父級元素的width let x, y; let moveX, moveY; //移動(dòng)距離 let maxX, maxY; //最大移動(dòng)距離 let isDrop = false; box.onmousedown = e => { x = e.clientX - box.offsetLeft; // e.clientX鼠標(biāo)相對于瀏覽器左上角x軸的坐標(biāo)-button上層控件的位置 y = e.clientY - box.offsetTop; isDrop = true; }; document.onmousemove = e => { if (isDrop) { e.preventDefault(); moveX = e.clientX - x; //得到距離左邊移動(dòng)距離 moveY = e.clientY - y; //得到距離上邊移動(dòng)距離 //可移動(dòng)最大距離 maxX = parentW - box.offsetWidth; maxY = parentH - box.offsetHeight; //移動(dòng)的有效距離計(jì)算 //console.log(Math.min(-1, 4, 6, 12));//輸出-1-----多個(gè)參數(shù),返回最小值 moveX = Math.min(maxX, Math.max(0, moveX)); moveY = Math.min(maxY, Math.max(0, moveY)); box.style.left = moveX + 'px'; box.style.top = moveY + 'px'; } else { return; } }; document.onmouseup = e => { e.preventDefault(); isDrop = false; }; } mounted() { this.btnDown(); } } </script> <style scoped lang="less"> .to-do-list { position: relative; min-height: 600px; max-height: 600px; width: 600px; overflow: hidden; border: 2px solid black; .move-btn { position: absolute; } } </style>
參考來源:用JavaScript實(shí)現(xiàn)div的鼠標(biāo)拖拽效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中el-menu-item實(shí)現(xiàn)路由跳轉(zhuǎn)的完整步驟
這篇文章主要給大家介紹了關(guān)于Vue中el-menu-item實(shí)現(xiàn)路由跳轉(zhuǎn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-09-09解決element-ui中Popconfirm氣泡確認(rèn)框的事件不生效問題
這篇文章主要介紹了解決element-ui中Popconfirm氣泡確認(rèn)框的事件不生效問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue之elementUi的el-select同時(shí)獲取value和label的三種方式
這篇文章主要介紹了vue之elementUi的el-select同時(shí)獲取value和label的三種方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02詳解vuejs中執(zhí)行npm run dev出現(xiàn)頁面cannot GET/問題
這篇文章主要介紹了詳解vuejs中執(zhí)行npm run dev出現(xiàn)頁面cannot GET/問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04使用VueRouter的addRoutes方法實(shí)現(xiàn)動(dòng)態(tài)添加用戶的權(quán)限路由
這篇文章主要介紹了使用VueRouter的addRoutes方法實(shí)現(xiàn)動(dòng)態(tài)添加用戶的權(quán)限路由,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06vue3監(jiān)聽resize窗口事件(離開頁面要銷毀窗口事件)
這篇文章主要給大家介紹了關(guān)于vue3監(jiān)聽resize窗口事件(離開頁面要銷毀窗口事件)的相關(guān)資料,vue是單頁面應(yīng)用,路由切換后,定時(shí)器并不會(huì)自動(dòng)關(guān)閉,需要手動(dòng)清除,當(dāng)頁面被銷毀時(shí),清除定時(shí)器即可,需要的朋友可以參考下2023-11-11