使用js實(shí)現(xiàn)的簡(jiǎn)單拖拽效果
前端開(kāi)發(fā)的時(shí)候,有好多地方用到拖拽效果,當(dāng)然 http://jqueryui.com/draggable/ 是個(gè)不錯(cuò)的選擇,but 我是個(gè)打破砂鍋問(wèn)到底的人,抽點(diǎn)時(shí)間用js小小的實(shí)現(xiàn)了類似的插件,話不多說(shuō)。
first: html和css
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
#div1 {
position: absolute;
width: 100px;
height: 100px;
cursor: move;
background-color: red;
}
</style>
</head>
<body>
<div id="div1">
</div>
</body>
</html>
now,先把主要算法實(shí)現(xiàn)一下:
<script>
window.onload = function () {
//獲取需要拖拽的div
var div1 = document.getElementById("div1");
//添加鼠標(biāo)按下事件
div1.onmousedown = function (evt) {
var oEvent = evt || event;
//獲取按下鼠標(biāo)到div left top的距離
var distanceX = oEvent.clientX - div1.offsetLeft;
var distanceY = oEvent.clientX - div1.offsetTop;
//添加doc滑動(dòng)時(shí)間
document.onmousemove = function (evt) {
var oEvent = evt || event;
//重新計(jì)算div的left top值
var left = oEvent.clientX - distanceX;
var top = oEvent.clientY - distanceY;
//left 當(dāng)小于等于零時(shí),設(shè)置為零 防止div拖出document之外
if (left <= 0) {
left = 0;
}
//當(dāng)left 超過(guò)文檔右邊界 設(shè)置為右邊界
else if (left >= document.documentElement.clientWidth - div1.offsetWidth) {
left = document.documentElement.clientWidth - div1.offsetWidth;
}
if (top <= 0) {
top = 0;
}
else if (top >= document.documentElement.clientHeight - div1.offsetHeight) {
top = document.documentElement.clientHeight - div1.offsetHeight;
}
//重新給div賦值
div1.style.top = top + "px";
div1.style.left = left + "px";
}
//添加鼠標(biāo)抬起事件
div1.onmouseup = function () {
//清空事件
document.onmousemove = null;
div1.onmouseup = null;
}
}
}
</script>
yeah,使用面向?qū)ο髮?shí)現(xiàn)一下
<style>
* {
margin:0;
padding:0;
}
#div1 {
width: 100px;
height: 100px;
background-color: red;
}
#div2 {
background-color:yellow;
width:100px;
height:100px;
}
</style>
<div id="div1"></div>
<div id="div2"></div>
js Draggle class:
function Drag(id) {
this.div = document.getElementById(id);
if (this.div) {
this.div.style.cursor = "move";
this.div.style.position = "absolute";
}
this.disX = 0;
this.disY = 0;
var _this = this;
this.div.onmousedown = function (evt) {
_this.getDistance(evt);
document.onmousemove = function (evt) {
_this.setPosition(evt);
}
_this.div.onmouseup = function () {
_this.clearEvent();
}
}
}
Drag.prototype.getDistance = function (evt) {
var oEvent = evt || event;
this.disX = oEvent.clientX - this.div.offsetLeft;
this.disY = oEvent.clientY - this.div.offsetTop;
}
Drag.prototype.setPosition = function (evt) {
var oEvent = evt || event;
var l = oEvent.clientX - this.disX;
var t = oEvent.clientY - this.disY;
if (l <= 0) {
l = 0;
}
else if (l >= document.documentElement.clientWidth - this.div.offsetWidth) {
l = document.documentElement.clientWidth - this.div.offsetWidth;
}
if (t <= 0) {
t = 0;
}
else if (t >= document.documentElement.clientHeight - this.div.offsetHeight) {
t = document.documentElement.clientHeight - this.div.offsetHeight;
}
this.div.style.left = l + "px";
this.div.style.top = t + "px";
}
Drag.prototype.clearEvent = function () {
this.div.onmouseup = null;
document.onmousemove = null;
}
at last:最終實(shí)現(xiàn):
window.onload = function () {
new Drag("div1");
new Drag("div2");
}
效果如下:
以上所述就是本文的全部?jī)?nèi)容了,希望能對(duì)大家更加熟練的掌握javascript有所幫助。
相關(guān)文章
js prototype 格式化數(shù)字 By shawl.qiu
js prototype 格式化數(shù)字 By shawl.qiu...2007-04-04js實(shí)現(xiàn)時(shí)間軸自動(dòng)排列效果
本文主要介紹了js實(shí)現(xiàn)新增加事件:時(shí)間軸自動(dòng)排列效果的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03微信小程序使用this.setData()遇到的問(wèn)題及解決方案詳解
this.setData估計(jì)是小程序中最經(jīng)常用到的一個(gè)方法,但是要注意其實(shí)他是有限制的,忽略這些限制的話,會(huì)導(dǎo)致數(shù)據(jù)無(wú)法更新,下面這篇文章主要給大家介紹了關(guān)于微信小程序使用this.setData()遇到的問(wèn)題及解決方案,需要的朋友可以參考下2022-08-08uni-app彈出層uni-popup使用及修改默認(rèn)樣式的方法實(shí)例
我們?cè)谑褂胾niapp開(kāi)發(fā)的時(shí)候,有時(shí)可以使用uniapp自有的樣式模板,這樣可以提高開(kāi)發(fā)效率,下面這篇文章主要給大家介紹了關(guān)于uni-app彈出層uni-popup使用及修改默認(rèn)樣式的相關(guān)資料,需要的朋友可以參考下2022-11-11javascript 用函數(shù)實(shí)現(xiàn)繼承詳解
下面小編就為大家?guī)?lái)一篇javascript 用函數(shù)實(shí)現(xiàn)繼承詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05使用JavaScript獲取掃碼槍掃描得到的條形碼的思路代碼詳解
這篇文章主要介紹了使用JavaScript獲取掃碼槍掃描得到的條形碼的思路代碼詳解,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06JavaScript鼠標(biāo)拖動(dòng)事件監(jiān)聽(tīng)使用方法以及實(shí)例效果演示
最近工作中遇到了鼠標(biāo)拖動(dòng)事件監(jiān)聽(tīng)的相關(guān)需求,所以下面這篇文章主要給大家介紹了關(guān)于JavaScript鼠標(biāo)拖動(dòng)事件監(jiān)聽(tīng)使用方法以及實(shí)例效果演示的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05JS Generator 函數(shù)的含義與用法實(shí)例總結(jié)
這篇文章主要介紹了JS Generator 函數(shù)的含義與用法,結(jié)合實(shí)例形式總結(jié)分析了JS Generator 函數(shù)基本含義、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04