js實現(xiàn)限定區(qū)域范圍拖拉拽效果
更新時間:2020年11月20日 14:13:41 作者:碼尚
這篇文章主要為大家詳細介紹了js實現(xiàn)限定區(qū)域范圍拖拉拽,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js實現(xiàn)限定區(qū)域范圍拖拉拽的具體代碼,供大家參考,具體內(nèi)容如下
需要在范圍內(nèi)拖拉拽,之前看來許多資料覺得都不是特別滿足要求,今天自己寫了一個,通過監(jiān)聽鼠標按下、鼠標抬起、鼠標移動事件來控制
代碼如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
#drag {
background: aqua;
width: 200px;
height: 200px;
position: absolute;
-moz-user-select: none;
-khtml-user-select: none;
user-select: none;
}
#parent {
position: relative;
background: #cde4dc;
height: 80vh;
overflow: hidden;
}
</style>
</head>
<body>
<section id="parent">
<div id="drag"><div>這是一個測試</div></div>
</section>
<script type="text/javascript">
//自執(zhí)行函數(shù),需要拖拽的元素需要設置position:absolute,父元素position:relative
//有傳父親節(jié)點、若無則默認body為父節(jié)點
((parent, children, mouseType) => {
if (!children) return;
let childrenDiv = document.querySelector(children);
childrenDiv.style.position = 'absolute';
let parentDiv = parent
? document.querySelector(parent)
: document.querySelector('body');
let isDown = false;
let x,
y,
l,
t = 0;
childrenDiv.onmousedown = function (e) {
x = e.clientX;
y = e.clientY;
// 獲取左部和底部的偏移量
l = childrenDiv.offsetLeft;
t = childrenDiv.offsetTop;
isDown = true;
childrenDiv.style.cursor = mouseType || 'move';
};
// 鼠標移動
window.onmousemove = function (e) {
if (!isDown) {
return;
}
//獲取移動后的x和y坐標
let nx = e.clientX;
let ny = e.clientY;
//獲取父元素的寬高
let parentWidth = parentDiv.clientWidth;
let parentHeight = parentDiv.clientHeight;
//獲取子元素的寬高
let childrenWidth = childrenDiv.clientWidth;
let childrenHight = childrenDiv.clientHeight;
// 計算移動后的左偏移量和頂部偏移量
let nLeft = nx - (x - l);
let nTop = ny - (y - t);
let nRight = nLeft + childrenWidth;
let nBottom = nTop + childrenHight;
nLeft = nLeft <= 0 ? 0 : nLeft; //判斷左邊距離是否越界
nTop = nTop <= 0 ? 0 : nTop; //判斷上邊距離是否越界
//判斷右邊距離大于父元素寬度
if (nRight >= parentWidth) {
nLeft = parentWidth - childrenHight;
}
// 判斷下邊界是否越界
if (nBottom >= parentHeight) nTop = parentHeight - childrenHight;
childrenDiv.style.left = nLeft + 'px';
childrenDiv.style.top = nTop + 'px';
};
// 鼠標抬起事件
document.onmouseup = function (e) {
//鼠標抬起
isDown = false;
childrenDiv.style.cursor = 'default';
};
})('#parent', '#drag', 'move');
</script>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何在JavaScript中實現(xiàn)私有屬性的寫類方式(一)
這篇文章主要介紹了如何在JavaScript中實現(xiàn)私有屬性的寫類方式。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
javascript解析json格式的數(shù)據(jù)方法詳解
這篇文章主要介紹了javascript解析json格式的數(shù)據(jù)方法詳解,文章通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
ECharts調(diào)用接口獲取后端數(shù)據(jù)的四種方法總結(jié)
echarts是我們經(jīng)常用到的數(shù)據(jù)可視化圖形,但是后端反饋給我們的數(shù)據(jù)經(jīng)常是數(shù)組包對象的集合類型,下面這篇文章主要給大家介紹了關(guān)于ECharts調(diào)用接口獲取后端數(shù)據(jù)的四種方法,需要的朋友可以參考下2022-11-11
JavaScript中windows.open()、windows.close()方法詳解
這篇文章主要介紹了JavaScript中windows.open()、windows.close()方法詳解 的相關(guān)資料,需要的朋友可以參考下2016-07-07
微信小程序 數(shù)據(jù)緩存實現(xiàn)方法詳解
這篇文章主要介紹了微信小程序 數(shù)據(jù)緩存實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08

