js實(shí)現(xiàn)限定區(qū)域范圍拖拉拽效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)限定區(qū)域范圍拖拉拽的具體代碼,供大家參考,具體內(nèi)容如下
需要在范圍內(nèi)拖拉拽,之前看來許多資料覺得都不是特別滿足要求,今天自己寫了一個(gè),通過監(jiān)聽鼠標(biāo)按下、鼠標(biāo)抬起、鼠標(biāo)移動(dòng)事件來控制
代碼如下
<!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>這是一個(gè)測(cè)試</div></div>
</section>
<script type="text/javascript">
//自執(zhí)行函數(shù),需要拖拽的元素需要設(shè)置position:absolute,父元素position:relative
//有傳父親節(jié)點(diǎn)、若無則默認(rèn)body為父節(jié)點(diǎn)
((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';
};
// 鼠標(biāo)移動(dòng)
window.onmousemove = function (e) {
if (!isDown) {
return;
}
//獲取移動(dòng)后的x和y坐標(biāo)
let nx = e.clientX;
let ny = e.clientY;
//獲取父元素的寬高
let parentWidth = parentDiv.clientWidth;
let parentHeight = parentDiv.clientHeight;
//獲取子元素的寬高
let childrenWidth = childrenDiv.clientWidth;
let childrenHight = childrenDiv.clientHeight;
// 計(jì)算移動(dòng)后的左偏移量和頂部偏移量
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';
};
// 鼠標(biāo)抬起事件
document.onmouseup = function (e) {
//鼠標(biāo)抬起
isDown = false;
childrenDiv.style.cursor = 'default';
};
})('#parent', '#drag', 'move');
</script>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何在JavaScript中實(shí)現(xiàn)私有屬性的寫類方式(一)
這篇文章主要介紹了如何在JavaScript中實(shí)現(xiàn)私有屬性的寫類方式。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12
javascript解析json格式的數(shù)據(jù)方法詳解
這篇文章主要介紹了javascript解析json格式的數(shù)據(jù)方法詳解,文章通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
ECharts調(diào)用接口獲取后端數(shù)據(jù)的四種方法總結(jié)
echarts是我們經(jīng)常用到的數(shù)據(jù)可視化圖形,但是后端反饋給我們的數(shù)據(jù)經(jīng)常是數(shù)組包對(duì)象的集合類型,下面這篇文章主要給大家介紹了關(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ù)緩存實(shí)現(xiàn)方法詳解
這篇文章主要介紹了微信小程序 數(shù)據(jù)緩存實(shí)現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
JavaScript進(jìn)階知識(shí)點(diǎn)作用域詳解
這篇文章主要介紹了JavaScript進(jìn)階講解一作用域,主要包括作用域、函數(shù)、閉包、面向?qū)ο蟆S新特性、事件循環(huán)、微任務(wù)、宏任務(wù)、內(nèi)存管理、Promise、await、?asnyc、防抖、節(jié)流等等知識(shí)點(diǎn),需要的朋友可以參考下2022-05-05

