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 拖拽的問題
若盒子中出現了文字,或盒子自身為圖片,由于瀏覽器的默認行為(文字和圖片本身就可以拖拽),通過設定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>
一旦為指定節點添加全域捕獲,則頁面中其它元素就不會觸發同型別事件
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 拖拽邊界
可視區域寬度:
可視區域高度:
//螢屏的高度
// var h=document.documentElement.clientHeight
// var w=document.documentElement.clientWidth;
?
// console.log(h,w);
分析:
最大left:可視區域寬度-盒子寬度
最小left:0
最小top: 0
最大top: 可視區域的高度-盒子的高度
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/303058.html
標籤:其他
