这里我看了下http://qings.blog.51cto.com/4857138/998878/ 的文章,感谢他的分享。

首先我们有一个a标签和一个div,div默认是不显示的,当用户点击时改为显示。

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>drag</title> <style type="text/css">
*{margin: 0;padding: 0;}
#box{border: 5px solid #2e2e2e;width:320px;height: 240px;background-color: #CC9900;
-moz-border-radius: 15px; /* Gecko browsers */
-webkit-border-radius: 15px; /* Webkit browsers */
border-radius:15px; /* W3C syntax */
}
#hd{background-color: #666666;font-size: 14px;padding: 4px;}
span{float: right;padding-right: 4px;}
#cnt{padding: 5px;}
</style>
</head>
<body>
<div id="btn">
<a href="#">点我弹框</a>
</div>
<div id="box">
<div id="hd">
<span>关闭</span><h3>这里是标题</h3>
</div>
<div id="cnt">
这里是一些文字
</div>
</div>
</body>
</html>

首先做点css,效果如下

然后隐藏div,添加jquery

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>drag</title>
<script src="./js/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
// alert(111);
$('#btn a').click(function() {
// alert(222);
$('#box').show();
});
});
</script> <style type="text/css">
*{margin: 0;padding: 0;}
#box{border: 5px solid #2e2e2e;width:320px;height: 240px;background-color: #CC9900;
-moz-border-radius: 15px; /* Gecko browsers */
-webkit-border-radius: 15px; /* Webkit browsers */
border-radius:15px; /* W3C syntax */
display: none;
margin-left: 30px;
margin-top: 20px;
}
#hd{background-color: #666666;font-size: 14px;padding: 4px;}
span{float: right;padding-right: 4px;}
#cnt{padding: 5px;}
</style>
</head>
<body>
<div id="btn">
<a href="#">点我弹框</a>
</div>
<div id="box">
<div id="hd">
<span>关闭</span><h3>这里是标题</h3>
</div>
<div id="cnt">
这里是一些文字
</div>
</div>
</body>
</html>

到此,点击显示就完成了。下面来完成关闭。给关闭span添加了一个鼠标手形的样式。

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>drag</title>
<script src="./js/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
// alert(111);
$('#btn a').click(function() {
// alert(222);
$('#box').show();
});
$('span').click(function() {
// alert(333);
$('#box').hide();
});
});
</script> <style type="text/css">
*{margin: 0;padding: 0;}
#box{border: 5px solid #2e2e2e;width:320px;height: 240px;background-color: #CC9900;
-moz-border-radius: 15px; /* Gecko browsers */
-webkit-border-radius: 15px; /* Webkit browsers */
border-radius:15px; /* W3C syntax */
display: none;
margin-left: 30px;
margin-top: 20px;
}
#hd{background-color: #666666;font-size: 14px;padding: 4px;}
span{float: right;padding-right: 4px;cursor: pointer;}
#cnt{padding: 5px;}
</style>
</head>
<body>
<div id="btn">
<a href="#">点我弹框</a>
</div>
<div id="box">
<div id="hd">
<span>关闭</span><h3>这里是标题</h3>
</div>
<div id="cnt">
这里是一些文字
</div>
</div>
</body>
</html>

div的关闭

下面我们来完成另一个任务,就是拖拽

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>drag</title>
<script src="./js/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
// alert(111);
$('#btn a').click(function() {
// alert(222);
$('#box').show();
});
$('span').click(function() {
// alert(333);
$('#box').hide();
}); $('#hd').mousedown(function(event) {
// alert(444);
var isMove = true;
var abs_x = event.pageX - $('div#box').offset().left;
var abs_y = event.pageY - $('div#box').offset().top;
// alert(abs_x);
// alert(event.pageX);
$(document).mousemove(function(event) {
// alert(555);
if (isMove) {
var obj = $('div#box');
// alert(obj);
obj.css({'left':event.pageX - abs_x, 'top':event.pageY - abs_y});
};
}).mouseup(function(event) {
isMove = false;
});;
});
});
</script> <style type="text/css">
*{margin: 0;padding: 0;}
#box{border: 5px solid #2e2e2e;width:320px;height: 240px;background-color: #CC9900;
-moz-border-radius: 15px; /* Gecko browsers */
-webkit-border-radius: 15px; /* Webkit browsers */
border-radius:15px; /* W3C syntax */
display: none;
margin-left: 30px;
margin-top: 20px;
position: absolute;
}
#hd{background-color: #666666;font-size: 14px;padding: 4px;cursor: move;}
span{float: right;padding-right: 4px;cursor: pointer;}
#cnt{padding: 5px;}
</style>
</head>
<body>
<div id="btn">
<a href="#">点我弹框</a>
</div>
<div id="box">
<div id="hd">
<span>关闭</span><h3>这里是标题</h3>
</div>
<div id="cnt">
这里是一些文字
</div>
</div>
</body>
</html>

拖拽

虽然过程有些曲折,但终于还是完成了。

    left = 当前鼠标位置.x - (鼠标点击时的.x值 - div的初始位置x值)
top = 当前鼠标位置.y - (鼠标点击时的.y值 - div的初始位置y值)

jQuery 学习笔记3 点击弹出一个div并允许拖拽移动的更多相关文章

  1. 点击文字弹出一个DIV层窗口代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  2. jQuery 学习笔记2 点击时弹出一个对话框

    上次学习的是页面加载完成后弹出一个警告框,这里我们改为当用户点击后弹出一个警告框. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...

  3. jQuery第二课 点击弹出一个提示框

    选择器允许您对元素组或单个元素进行操作. jQuery 选择器 在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元 ...

  4. 点击按钮弹出一个div层

    JQuery弹出层,点击按钮后弹出遮罩层,还有关闭按钮 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml& ...

  5. 点击文字弹出一个DIV层窗口代码 【或FORM表单 并且获取点击按钮的ID值】

    点击不同按钮咨询不同的 专家 <?php for($i=1;$i<5;$i++){ $uid=$i; //用户ID ?> <a class="a_click" ...

  6. Jquery学习之路(三) 实现弹出层插件

    弹出层的应用还是比较多的,登陆,一些同页面的操作,别人的总归是别人的,自己的才是自己的,所以一直以来想写个弹出层插件.不多废话,直接开始吧! 不想看可以在这里直接下载源码xsPop.zip 1:遮罩层 ...

  7. elementUI 弹出框添加可自定义拖拽和拉伸功能,并处理边界问题

    开发完后台管理系统的弹出框模块,被添加拖拽和拉伸功能,看了很多网上成熟的帖子引到项目里总有一点问题,下面是根据自己的需求实现的步骤: 首先在vue项目中创建一个js文件eg:dialog.js imp ...

  8. jQuery第一课 加载页面弹出一个对话框

    <script type="text/javascript"> $(document).ready(function(){ alert("欢迎收看:" ...

  9. 页面table的每行都有一个<input type='button' />,如何实现点击按钮在按钮下方弹出一个div,点击空白消失

    \ <input id="test" type="button" />/*按钮*/ <div id="tanchu"> ...

随机推荐

  1. magento的一些小技巧(转)

    1.加载某个attribute: $attributeCode=Mage::getModel('catalog/resource_eav_attribute')                    ...

  2. STS 设置代码注释模板

    打开Window->Preferences->Java->Code Style->Code Templates <?xml version="1.0" ...

  3. web前端学习笔记-瀑布流的算法分析与代码实现

    瀑布流效果目前应用很广泛,像花瓣,新浪轻博,蘑菇街,美丽说等好多网站都有.也有好多支持该效果的前段框架,今天学习了一下这种效果的实现,不依赖插件,自己动手分析实现过程,为了便于叙述清楚,分析中的一些名 ...

  4. mysql快速移植表数据

    使用select into outfile xxx ,  load data infile xxx 例如 : SELECT * into outfile '/tmp/out.txt' FROM `db ...

  5. 探索MVP(Model-View-Presenter)设计模式在SharePoint平台下的实现

    对于SharePoint Developers来说,往往会过多的去关注SharePoint平台和工具,而把设计模式和代码的可测试性放在了一个较低的优先级.这并不是说SharePoint Develop ...

  6. FreeSWITCH收到重复的DTMF信号

    一.背景 用户是运营商手机,拨打的是运营商的固话号码进入的FreeSWITCH的IVR,进入IVR语音播报后,按指定的分机号呼相关人员. 二.现象 用户反映拨打124870找不到指定人员,以前是正常的 ...

  7. 【转】java io 流 设计模式

    知识点:什么是装饰模式: http://wenku.baidu.com/view/ad4eac9f51e79b896802263b.html(原理讲的很清楚) http://wenku.baidu.c ...

  8. [转]java:IO流学习小结

    Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  9. Linux系统和工具集

    Linux系统和工具集 阿里源 http://mirrors.aliyun.com/ http://centos.ustc.edu.cn/ 第三方包管理器 不同的发行版使用不同的软件包管理器,Cent ...

  10. 关于ansbile工具的shell、command、script、raw模块的区别和使用场景

    command模块 [执行远程命令] [root@node1 ansible]# ansible testservers -m command -a 'uname -n' script模块 [在远程主 ...