JavaScript實(shí)現(xiàn)拖拽簡單效果
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下
1.1 拖拽的基本效果
思路:
鼠標(biāo)在盒子上按下時(shí),準(zhǔn)備移動(dòng) (事件加給物體)
鼠標(biāo)移動(dòng)時(shí),盒子跟隨鼠標(biāo)移動(dòng) (事件添加給頁面)
鼠標(biāo)抬起時(shí),盒子停止移動(dòng) (事件加給頁面)
var o = document.querySelector('div');
//鼠標(biāo)按下
o.onmousedown = function (e) {
//鼠標(biāo)相對于盒子的位置
var offsetX = e.clientX - o.offsetLeft;
var offsetY = e.clientY - o.offsetTop;
//鼠標(biāo)移動(dòng)
document.onmousemove = function (e) {
o.style.left = e.clientX - offsetX + "px";
o.style.top = e.clientY - offsetY + "px";
}
//鼠標(biāo)抬起
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
}
}
1.2 拖拽的問題
若盒子中出現(xiàn)了文字,或盒子自身為圖片,由于瀏覽器的默認(rèn)行為(文字和圖片本身就可以拖拽),通過設(shè)置return false即可以。但是,攔截默認(rèn)行為在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é)點(diǎn)添加全局捕獲,則頁面中其它元素就不會觸發(fā)同類型事件
1.2.2 完整版的拖拽
var o = document.querySelector('div');
//鼠標(biāo)按下
o.onmousedown = function (e) {
if (o.setCapture) { //IE低版本
o.setCapture()
}
e = e || window.event
//鼠標(biāo)相對于盒子的位置
var offsetX = e.clientX - o.offsetLeft;
var offsetY = e.clientY - o.offsetTop;
//鼠標(biāo)移動(dòng)
document.onmousemove = function (e) {
e = e || window.event
o.style.left = e.clientX - offsetX + "px";
o.style.top = e.clientY - offsetY + "px";
}
//鼠標(biāo)抬起
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
if (o.releaseCapture) {
o.releaseCapture();//釋放全局捕獲
}
}
return false;//標(biāo)準(zhǔn)瀏覽器的默認(rèn)行為
}
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 碰撞
碰撞的重點(diǎn)在于尋找臨界值。
分別命名兩個(gè)物體的四邊為: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');
//鼠標(biāo)按下
o.onmousedown = function (e) {
if (o.setCapture) { //IE低版本
o.setCapture()
}
e = e || window.event
//鼠標(biāo)相對于盒子的位置
var offsetX = e.clientX - o.offsetLeft;
var offsetY = e.clientY - o.offsetTop;
//計(jì)算最大左邊距和上邊距(邊界)
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
//鼠標(biāo)移動(dòng)
document.onmousemove = function (e) {
e = e || window.event
var x = e.clientX - offsetX;
var y = e.clientY - offsetY;
//計(jì)算邊界
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";
//計(jì)算碰撞
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"
}
}
//鼠標(biāo)抬起
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
if (o.releaseCapture) {
o.releaseCapture();//釋放全局捕獲
}
}
return false;//標(biāo)準(zhǔn)瀏覽器的默認(rèn)行為
}
</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js中關(guān)于String對象的replace使用詳解
關(guān)于String對象的replace使用詳解,需要的朋友可以參考下。2011-05-05
bootstrap可編輯下拉框jquery.editable-select
這篇文章主要介紹了bootstrap可編輯下拉框jquery.editable-select的相關(guān)資料,需要的朋友可以參考下2017-10-10
將字符串轉(zhuǎn)換成gb2312或者utf-8編碼的參數(shù)(js版)
直接在url中傳遞中文參數(shù)時(shí),讀到的中文都是亂碼,那么我們應(yīng)該怎么將這些參數(shù)轉(zhuǎn)換呢,接下來與大家分享下將字符串轉(zhuǎn)換成utf-8或者gb2312編碼的參數(shù)的技巧2013-04-04

