jQuery拖動(dòng)div、移動(dòng)div、彈出層實(shí)現(xiàn)原理及示例
更新時(shí)間:2014年04月08日 11:26:41 作者:
正如標(biāo)題所言的實(shí)現(xiàn)原理是使div的position為絕對(duì)定位absolute,然后控制其top與left值,需要的朋友可以參考下
代碼演示:
http://www.imqing.com/demo/movediv.html
大概原理:
使div的position為絕對(duì)定位absolute,然后控制其top與left值,需要監(jiān)聽(tīng)鼠標(biāo)事件,主要用到mousedown, mousemove, mouseup。
在mousedown后,記錄mousedown時(shí)鼠標(biāo)與需要移動(dòng)的div的位置,然后取得兩者之差,得到在鼠標(biāo)移動(dòng)后,div的位置。即:
left = 當(dāng)前鼠標(biāo)位置.x - (鼠標(biāo)點(diǎn)擊時(shí)的.x值 - div的初始位置x值)
top = 當(dāng)前鼠標(biāo)位置.y - (鼠標(biāo)點(diǎn)擊時(shí)的.y值 - div的初始位置y值)
代碼:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Qing's Web</title>
<script src="./jquery-1.7.2.min.js" type="text/javascript"></script>
<style type="text/css">
.footer {
position: fixed;
bottom: 0;
width: 100%;
}
.moveBar {
position: absolute;
width: 250px;
height: 300px;
background: #666;
border: solid 1px #000;
}
#banner {
background: #52CCCC;
cursor: move;
}
</style>
</head>
<body style="padding-top: 50px;">
<div class="moveBar">
<div id="banner">按住此處移動(dòng)當(dāng)前div</div>
<div class="content">這里是其它內(nèi)容</div>
</div>
<div class="footer">
<p align="center" class="label">ALL Rights Reserved Qing 版權(quán)所有</p>
</div>
<script>
jQuery(document).ready(
function () {
$('#banner').mousedown(
function (event) {
var isMove = true;
var abs_x = event.pageX - $('div.moveBar').offset().left;
var abs_y = event.pageY - $('div.moveBar').offset().top;
$(document).mousemove(function (event) {
if (isMove) {
var obj = $('div.moveBar');
obj.css({'left':event.pageX - abs_x, 'top':event.pageY - abs_y});
}
}
).mouseup(
function () {
isMove = false;
}
);
}
);
}
);
</script>
</body>
</html>
其實(shí)代碼量也不多的,嘿嘿。要點(diǎn)就是需要移動(dòng)的div的position是絕對(duì)定位,然后檢測(cè)鼠標(biāo)事件就行了。嘿嘿。
http://www.imqing.com/demo/movediv.html
大概原理:
使div的position為絕對(duì)定位absolute,然后控制其top與left值,需要監(jiān)聽(tīng)鼠標(biāo)事件,主要用到mousedown, mousemove, mouseup。
在mousedown后,記錄mousedown時(shí)鼠標(biāo)與需要移動(dòng)的div的位置,然后取得兩者之差,得到在鼠標(biāo)移動(dòng)后,div的位置。即:
left = 當(dāng)前鼠標(biāo)位置.x - (鼠標(biāo)點(diǎn)擊時(shí)的.x值 - div的初始位置x值)
top = 當(dāng)前鼠標(biāo)位置.y - (鼠標(biāo)點(diǎn)擊時(shí)的.y值 - div的初始位置y值)
代碼:
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Qing's Web</title>
<script src="./jquery-1.7.2.min.js" type="text/javascript"></script>
<style type="text/css">
.footer {
position: fixed;
bottom: 0;
width: 100%;
}
.moveBar {
position: absolute;
width: 250px;
height: 300px;
background: #666;
border: solid 1px #000;
}
#banner {
background: #52CCCC;
cursor: move;
}
</style>
</head>
<body style="padding-top: 50px;">
<div class="moveBar">
<div id="banner">按住此處移動(dòng)當(dāng)前div</div>
<div class="content">這里是其它內(nèi)容</div>
</div>
<div class="footer">
<p align="center" class="label">ALL Rights Reserved Qing 版權(quán)所有</p>
</div>
<script>
jQuery(document).ready(
function () {
$('#banner').mousedown(
function (event) {
var isMove = true;
var abs_x = event.pageX - $('div.moveBar').offset().left;
var abs_y = event.pageY - $('div.moveBar').offset().top;
$(document).mousemove(function (event) {
if (isMove) {
var obj = $('div.moveBar');
obj.css({'left':event.pageX - abs_x, 'top':event.pageY - abs_y});
}
}
).mouseup(
function () {
isMove = false;
}
);
}
);
}
);
</script>
</body>
</html>
其實(shí)代碼量也不多的,嘿嘿。要點(diǎn)就是需要移動(dòng)的div的position是絕對(duì)定位,然后檢測(cè)鼠標(biāo)事件就行了。嘿嘿。
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)的鼠標(biāo)拖動(dòng)浮層功能示例【拖動(dòng)div等任何標(biāo)簽】
- jquery div拖動(dòng)效果示例代碼
- jQuery實(shí)現(xiàn)單擊彈出Div層窗口效果(可關(guān)閉可拖動(dòng))
- jquery拖動(dòng)改變div大小
- 使用jQuery的easydrag插件實(shí)現(xiàn)可拖動(dòng)的DIV彈出框
- jquery實(shí)現(xiàn)可拖動(dòng)DIV自定義保存到數(shù)據(jù)的實(shí)例
- jQuery實(shí)現(xiàn)Div拖動(dòng)+鍵盤控制綜合效果的方法
- jQuery實(shí)現(xiàn)鼠標(biāo)拖動(dòng)div改變位置、大小的實(shí)踐
相關(guān)文章
利用jQuery的動(dòng)畫函數(shù)animate實(shí)現(xiàn)豌豆發(fā)射效果
本文主要講解jQuery的animate函數(shù)的基本用法,利用動(dòng)畫函數(shù)animate實(shí)現(xiàn)豌豆發(fā)射的效果,對(duì)于學(xué)習(xí)animate很有幫助,有需要的可以參考借鑒。2016-08-08jquery實(shí)現(xiàn)對(duì)聯(lián)廣告的方法
這篇文章主要介紹了jquery實(shí)現(xiàn)對(duì)聯(lián)廣告的方法,以一個(gè)完整實(shí)例形式詳細(xì)分析了jQuery對(duì)聯(lián)廣告的樣式與功能實(shí)現(xiàn)方法,是非常實(shí)用的技巧,需要的朋友可以參考下2015-02-02jQuery實(shí)現(xiàn)簡(jiǎn)單日期格式化功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)簡(jiǎn)單日期格式化功能,涉及jQuery調(diào)用javascript針對(duì)日期格式轉(zhuǎn)換擴(kuò)展實(shí)現(xiàn)日期格式化功能相關(guān)操作技巧,需要的朋友可以參考下2017-09-09$.format,jquery.format 使用說(shuō)明
$.format,jquery.format 使用說(shuō)明,需要的朋友可以參考下。2011-07-07jquery 無(wú)限極下拉菜單的簡(jiǎn)單實(shí)例(精簡(jiǎn)濃縮版)
下面小編就為大家?guī)?lái)一篇jquery 無(wú)限極下拉菜單的簡(jiǎn)單實(shí)例(精簡(jiǎn)濃縮版)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05使用jQuery加載html頁(yè)面到指定的div實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇使用jQuery加載html頁(yè)面到指定的div實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07使用Browserify配合jQuery進(jìn)行編程的超級(jí)指南
這篇文章主要介紹了使用Browserify配合jQuery進(jìn)行編程的超級(jí)指南,Browserify 可以讓你使用類似于node的require()的方式來(lái)組織瀏覽器端的JavaScript代碼,需要的朋友可以參考下2015-07-07