JavaScript實現(xiàn)拖拽簡單效果
本文實例為大家分享了JavaScript實現(xiàn)拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下
1.1 拖拽的基本效果
思路:
鼠標在盒子上按下時,準備移動 (事件加給物體)
鼠標移動時,盒子跟隨鼠標移動 (事件添加給頁面)
鼠標抬起時,盒子停止移動 (事件加給頁面)
var o = document.querySelector('div');
//鼠標按下
o.onmousedown = function (e) {
//鼠標相對于盒子的位置
var offsetX = e.clientX - o.offsetLeft;
var offsetY = e.clientY - o.offsetTop;
//鼠標移動
document.onmousemove = function (e) {
o.style.left = e.clientX - offsetX + "px";
o.style.top = e.clientY - offsetY + "px";
}
//鼠標抬起
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
}
}
1.2 拖拽的問題
若盒子中出現(xiàn)了文字,或盒子自身為圖片,由于瀏覽器的默認行為(文字和圖片本身就可以拖拽),通過設(shè)置return false即可以。但是,攔截默認行為在IE低版本中,不適用;可以使用全局捕獲來解決IE的問題。
1.2.1 全局捕獲
全局捕獲僅適用于IE低版本瀏覽器
<button>btn1</button>
<button>btn2</button>
<script>
var bts = document.querySelectorAll('button')
bts[0].onclick = function () {
console.log(1);
}
bts[1].onclick = function () {
console.log(2);
}
// bts[0].setCapture() //添加全局捕獲
// bts[0].releaseCapture() ;//釋放全局捕獲
</script>
一旦為指定節(jié)點添加全局捕獲,則頁面中其它元素就不會觸發(fā)同類型事件
1.2.2 完整版的拖拽
var o = document.querySelector('div');
//鼠標按下
o.onmousedown = function (e) {
if (o.setCapture) { //IE低版本
o.setCapture()
}
e = e || window.event
//鼠標相對于盒子的位置
var offsetX = e.clientX - o.offsetLeft;
var offsetY = e.clientY - o.offsetTop;
//鼠標移動
document.onmousemove = function (e) {
e = e || window.event
o.style.left = e.clientX - offsetX + "px";
o.style.top = e.clientY - offsetY + "px";
}
//鼠標抬起
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
if (o.releaseCapture) {
o.releaseCapture();//釋放全局捕獲
}
}
return false;//標準瀏覽器的默認行為
}
1.3 拖拽邊界
可視區(qū)域?qū)挾龋?/p>
可視區(qū)域高度:
//屏幕的高度 // var h=document.documentElement.clientHeight // var w=document.documentElement.clientWidth; // console.log(h,w);
分析:
最大left:可視區(qū)域?qū)挾?盒子寬度
最小left:0
最小top: 0
最大top: 可視區(qū)域的高度-盒子的高度
1.4 碰撞
碰撞的重點在于尋找臨界值。
分別命名兩個物體的四邊為:L1,T1,R1,B1和L2,T2,R2,B2
若L1 > R2 || T1 > B2 || R1 < L2 || B1 < T2,則不碰撞
<div class="one">
</div>
<div class="two"></div>
<script>
var o = document.querySelector('.one');
var ox = document.querySelector('.two');
//鼠標按下
o.onmousedown = function (e) {
if (o.setCapture) { //IE低版本
o.setCapture()
}
e = e || window.event
//鼠標相對于盒子的位置
var offsetX = e.clientX - o.offsetLeft;
var offsetY = e.clientY - o.offsetTop;
//計算最大左邊距和上邊距(邊界)
var maxLeft = document.documentElement.clientWidth - this.offsetWidth;
var maxTop = document.documentElement.clientHeight - this.offsetHeight;
//碰撞
var L2 = ox.offsetLeft;
var T2 = ox.offsetTop;
var R2 = L2 + ox.offsetWidth;
var B2 = T2 + ox.offsetHeight
//鼠標移動
document.onmousemove = function (e) {
e = e || window.event
var x = e.clientX - offsetX;
var y = e.clientY - offsetY;
//計算邊界
if (x <= 0) x = 0
if (y <= 0) y = 0
if (x >= maxLeft) x = maxLeft;
if (y >= maxTop) y = maxTop;
o.style.left = x + "px";
o.style.top = y + "px";
//計算碰撞
var L1 = o.offsetLeft;
var T1 = o.offsetTop;
var R1 = L1 + o.offsetWidth;
var B1 = T1 + o.offsetHeight;
if (L1 > R2 || T1 > B2 || R1 < L2 || B1 < T2) { //不碰撞
ox.style.backgroundColor = "blue"
} else {
ox.style.backgroundColor = "orange"
}
}
//鼠標抬起
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
if (o.releaseCapture) {
o.releaseCapture();//釋放全局捕獲
}
}
return false;//標準瀏覽器的默認行為
}
</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript獲取地址欄參數(shù)的方法實現(xiàn)
這篇文章主要給大家介紹了關(guān)于JavaScript獲取地址欄參數(shù)的方法實現(xiàn),項目中經(jīng)常遇到獲取上個頁面跳轉(zhuǎn)過來獲取當前的參數(shù),文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-07-07
JavaScript?數(shù)據(jù)結(jié)構(gòu)之集合創(chuàng)建(2)
這篇文章主要介紹了JavaScript?數(shù)據(jù)結(jié)構(gòu)之集合創(chuàng)建,上一篇我們介紹了什么是集合,并且手動實現(xiàn)了一個集合的類,本篇基于上篇內(nèi)容繼續(xù)深入介紹需要的小伙伴可以參考一下2022-04-04

