国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery拖動div、移動div、彈出層實現(xiàn)原理及示例

2019-11-20 20:51:20
字體:
供稿:網(wǎng)友
代碼演示:

http://www.imqing.com/demo/movediv.html

大概原理:

使div的position為絕對定位absolute,然后控制其top與left值,需要監(jiān)聽鼠標(biāo)事件,主要用到mousedown, mousemove, mouseup。

在mousedown后,記錄mousedown時鼠標(biāo)與需要移動的div的位置,然后取得兩者之差,得到在鼠標(biāo)移動后,div的位置。即:

left = 當(dāng)前鼠標(biāo)位置.x - (鼠標(biāo)點擊時的.x值 - div的初始位置x值)
top = 當(dāng)前鼠標(biāo)位置.y - (鼠標(biāo)點擊時的.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)前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>

其實代碼量也不多的,嘿嘿。要點就是需要移動的div的position是絕對定位,然后檢測鼠標(biāo)事件就行了。嘿嘿。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 萝北县| 宜君县| 水城县| 张家港市| 桑植县| 宾阳县| 泰宁县| 和政县| 乐至县| 太湖县| 麻城市| 宜阳县| 灵宝市| 长子县| 平山县| 阳春市| 民县| 麻江县| 克拉玛依市| 嘉义县| 都匀市| 江达县| 景德镇市| 二连浩特市| 原平市| 孟津县| 德兴市| 凤山县| 隆子县| 湛江市| 杭锦后旗| 墨江| 灵台县| 芦山县| 德惠市| 永城市| 诏安县| 白玉县| 弥渡县| 华宁县| 双桥区|