簡單實現(xiàn)js拖拽效果
更新時間:2017年07月25日 14:58:18 作者:diasa
這篇文章主要教大家如何簡單實現(xiàn)js拖拽效果,很詳細(xì)的js拖拽效果實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js拖拽效果展示的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
margin:0;
padding:0;
}
html,body{
width:100%;
height:100%;
}
#box{
position:absolute;
top:50%;
left:50%;
width:200px;
height:200px;
background:#ff6600;
margin:-100px 0 0 -100px;
cursor:move;
/*
不知道寬高的情況下的居中
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
margin:auto;
*/
}
</style>
</head>
<body>
<div id='box'>
</div>
<script>
//JS實現(xiàn)讓當(dāng)前的元素在屏幕居中的位置
var box = document.getElementById('box');
// box.style.top = ((document.documentElement.clientHeight || document.body.clientHeight)-box.offsetHeight)/2 + "px";
// box.style.left = ((document.documentElement.clientWidth || document.body.clientWidth)-box.offsetWidth)/2 + "px";
//拖拽的原理
/*
當(dāng)鼠標(biāo)在盒子上按下的時候,我們開始拖拽(給盒子綁定onmousemove和onmouseup),當(dāng)鼠標(biāo)移動的時候,我們計算盒子的最新位置
當(dāng)鼠標(biāo)抬起的時候說明拖拽結(jié)束了,我們的move和up就沒用了,我們再把這兩個方法移除
*/
box.onmousedown = down;
function down(e){
e = e || window.event;
//記錄開始位置的信息
this["strX"] = e.clientX;
this["strY"] = e.clientY;
this["strL"] = parseFloat(this.style.left);
this["strT"] = parseFloat(this.style.top);
//給元素綁定移動和抬起的事件
if(this.setCapture){
this.setCapture()//把當(dāng)前的鼠標(biāo)和this綁定在一起
this.onmousemove = move;
this.onmouseup= up;
}else{
var _this = this;
document.onmousemove = function(e){
// move(e)//這個里面的this是window
move.call(_this,e);
}
;
document.onmouseup= function(e){
up.call(_this,e);
};
}
}
function move(e){
e = e || window.event;
var curL = (e.clientX-this["strX"])+this["strL"];
var curT = (e.clientY-this["strY"])+this["strT"];
//邊界判斷
var minL = 0,minT = 0,maxL = (document.documentElement.clientWidth || document.body.clientWidth) - this.offsetWidth,maxT = (document.documentElement.clientHeight || document.body.clientHeight) - this.offsetHeight;
curL = curL < minL ? minL :(curL > maxL ? maxL : curL);
curT = curT < minT ? minT :(curT > maxT ? maxT : curT)
this.style.left = curL + "px";
this.style.top = curT + "px";
}
function up(e){
if(this.releaseCapture){
this.releaseCapture();//把當(dāng)前的鼠標(biāo)和盒子解除綁定
this.onmousemove = null;
this.onmouseup= null;
}else{
document.onmousemove = null;
document.onmouseup= null;
//這樣綁定的話,move和up綁定的this都是document
}
}
//當(dāng)鼠標(biāo)移動過快的時候,我們的鼠標(biāo)會脫離盒子,導(dǎo)致盒子的mousemove和mouseup事件都移除不到->"鼠標(biāo)焦點丟失"
//在IE和火狐瀏覽器中,我們用一個方法把盒子和鼠標(biāo)綁定在一起即可。
//鼠標(biāo)再快也跑不出去文檔:我們把mousemove和mouseup綁定給document
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
uniapp中scroll-view基礎(chǔ)用法示例代碼
我們在項目中往往都能遇到實現(xiàn)左右滑動跟上下滑動的需求,下面這篇文章主要給大家介紹了關(guān)于uniapp中scroll-view基礎(chǔ)用法的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
JavaScript 產(chǎn)生不重復(fù)的隨機數(shù)三種實現(xiàn)思路
在 JavaScript 中,一般產(chǎn)生的隨機數(shù)會重復(fù),但是有時我們需要不重復(fù)的隨機數(shù),如何實現(xiàn)?本文給于解決方法,需要的朋友可以參考下2012-12-12
Bootstrap教程JS插件滾動監(jiān)聽學(xué)習(xí)筆記分享
這篇文章主要為大家分享了Bootstrap教程JS插件滾動監(jiān)聽學(xué)習(xí)筆記,內(nèi)容很詳細(xì),感興趣的小伙伴們可以參考一下2016-05-05
學(xué)習(xí)JavaScript設(shè)計模式(接口)
這篇文章主要帶領(lǐng)大家學(xué)習(xí)JavaScript設(shè)計模式,其中重點介紹接口,舉例說明什么是接口,對接口進行詳細(xì)剖析,感興趣的小伙伴們可以參考一下2015-11-11

