jQuery實(shí)現(xiàn)的簡單對話框拖動功能示例
本文實(shí)例講述了jQuery實(shí)現(xiàn)的簡單對話框拖動功能。分享給大家供大家參考,具體如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>www.dhdzp.com jquery 拖動</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel=" rel="external nofollow" stylesheet">
<style>
*{margin: 0;padding: 0;box-sizing: border-box;-moz-user-select:none;}
body {font: 12px/16px bold 'microsoft yahei,微軟雅黑';}
.dragContainer {
width: 382px;
height: 395px;
position: absolute;
top: 50%;
left: 50%;
border: 1px solid red;
margin-left: -191px;
margin-top: -197.5px;
}
.dragContainer .dragtitle {
width: 100%;
height: 35px;
border-bottom: 1px solid red;
text-align: center;
line-height: 35px;
}
.dragContainer .dragtitle:hover {
cursor: move;
}
.dragContainer .content {
width: 100%;
height: 360px;
}
p.buttonGroup {
width: 100%;
text-align: center;
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 5px;
border-top: 1px solid red;
}
p.buttonGroup input {
padding: 5px 10px;
color: white;
}
p.buttonGroup input:hover {
cursor: pointer;
cursor: hand;
}
p.buttonGroup input:first-child {
margin-left: 25px;
background: blue;
}
p.buttonGroup input.btn2 {
margin-left: 15px;
background: red;
}
</style>
</head>
<body>
<div class="dragContainer" id="drag" style="'position:absolute;" >
<p class="dragtitle">標(biāo)題欄</p>
<div class="content"></div>
<p class="buttonGroup"><input type="button" value="確定"><input type="button" value="取消" class="btn2"></p>
</div>
<script src="http://cdn.bootcss.com/jquery/2.1.0/jquery.js"></script>
<script type="text/javascript">
var mydrag={
mousePoint: {x:0,y:0}, //初始化坐標(biāo)
drag: function(){
var that=this; //保存當(dāng)前對象即(mydrag),如果不保存,在mousedown()里訪問不了mydrag這個對象
var targetid=$(".dragtitle");
targetid.mousedown(function(event){
var e=event;
var offsetLeft=targetid.offset().left; //當(dāng)前div的左偏移距離
var offsetTop=targetid.offset().top; //當(dāng)前div的頂部偏移距離
that.mousePoint.x=e.clientX-offsetLeft;//計(jì)算鼠標(biāo)點(diǎn)擊時離它自己div的橫向距離
that.mousePoint.y=e.clientY-offsetTop; //計(jì)算鼠標(biāo)點(diǎn)擊時離它自己div的縱向距離
$(document).bind('mousemove',move);
e.stopPropagation();
});
function move(event){
var e =event;
var Left=e.clientX-that.mousePoint.x; //移動后,重新計(jì)算左偏移和頂部偏移距離
var Top=e.clientY-that.mousePoint.y;
$("#drag").css({'top':Top,'left':Left,'margin':0});
$(document).bind('mouseup',end);
e.stopPropagation();
};
function end(event){
var e = event;
$(document).unbind('mousemove', move);
$(document).unbind('mouseup',end);
e.stopPropagation();
};
}
}
mydrag.drag();
</script>
</body>
</html>
使用在線HTML/CSS/JavaScript代碼運(yùn)行工具 http://tools.jb51.net/code/HtmlJsRun測試,運(yùn)行效果如下:

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery拖拽特效與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jquery中Ajax用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。
- jquery實(shí)現(xiàn)表格行拖動排序
- jQuery-ui插件sortable實(shí)現(xiàn)自由拖動排序
- 針對后臺列表table拖拽比較實(shí)用的jquery拖動排序
- jQuery拖動元素并對元素進(jìn)行重新排序
- jQuery仿360導(dǎo)航頁圖標(biāo)拖動排序效果代碼分享
- jquery實(shí)現(xiàn)的鼠標(biāo)拖動排序Li或Table
- jquery對元素拖動排序示例
- 基于JQuery的列表拖動排序?qū)崿F(xiàn)代碼
- jQuery實(shí)現(xiàn)移動端懸浮拖動效果
- jquery+css實(shí)現(xiàn)移動端元素拖動排序
相關(guān)文章
BootStrap table表格插件自適應(yīng)固定表頭(超好用)
這篇文章主要介紹了BootStrap table表格插件自適應(yīng)固定表頭(超好用)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
jQuery實(shí)現(xiàn)的下雪動畫效果示例【附源碼下載】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的下雪動畫效果,涉及jQuery插件結(jié)合setInterval、animate進(jìn)行動畫操作的相關(guān)使用技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2018-02-02
50 個 jQuery 插件可將你的網(wǎng)站帶到另外一個高度
Query架構(gòu)的開發(fā)人員能夠創(chuàng)建一個插件代碼來擴(kuò)展其功能,從而能夠產(chǎn)生一些最好的插件,讓你的網(wǎng)站或任何給定的項(xiàng)目達(dá)到一個全新的水平。2016-04-04
jquery編寫Tab選項(xiàng)卡滾動導(dǎo)航切換特效
這篇文章主要為大家詳細(xì)介紹了jquery編寫Tab選項(xiàng)卡滾動導(dǎo)航切換特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-04-04
JQueryEasyUI datagrid框架的進(jìn)階使用
本篇文章小編為大家介紹JQueryEasyUI datagrid框架的基本使用,有需要的朋友可以參考一下2013-04-04
EasyUI Datebox 日期驗(yàn)證之開始日期小于結(jié)束時間
這篇文章主要介紹了EasyUI Datebox 日期驗(yàn)證之開始日期小于結(jié)束時間,需要的朋友可以參考下2017-05-05
基于jquery的劃詞搜索實(shí)現(xiàn)(備忘)
最近,需要做個劃詞搜索功能。在網(wǎng)上找了好些,最后,參照進(jìn)行修改,實(shí)現(xiàn)了想要的功能。這里,做個記錄,以備日后所用。2010-09-09

