惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Y
Y Combinator Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
IT之家
IT之家
量子位
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
罗磊的独立博客
S
SegmentFault 最新的问题
博客园_首页
N
Netflix TechBlog - Medium
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
A
About on SuperTechFans
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
MyScale Blog
MyScale Blog

博客园 - 西狐

我眼中的Java架构师 使用命令行写一个 Java Servlet 关于真正的Ajax方式上传文件 用CSS的 filter 来轻松实现图层半透明 用CSS3的 border-radius 来轻松实现图层圆角 用CSS3的 box-shadow 来轻松实现图层阴影效果 VS2010 让你的Javascript代码可以折叠 JSON 转成 C# 动态类 C# 将对象序列化为XML Lamborghini 兰博基尼 Gallardo Lamborghini 兰博基尼 Murcielago Lamborghini 兰博基尼 Reventon Lamborghini 兰博基尼 JavaScript 的 StringBuilder 无法加载"sybdrvado20.dll" 的原因和解决办法 js把 CheckBox 复选框 做成 radio 单选 的效果 搜狗"云"输入法,实现原理. - 西狐 - 博客园 TXT小说下载,交流 贾君鹏你妈妈喊你回家吃饭
用jQuery轻松实现Div拖动
西狐 · 2011-05-20 · via 博客园 - 西狐

用jQuery轻松实现Div拖动,只需只行代码,很简单. 试试效果:

hooyes

代码:

<!DOCTYPE html>
<html>
<head>
<title>hooyes Drag demo</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"
type
="text/javascript"></script>
<style type="text/css">
.drag
{
position
:absolute;
width
:100px; height:100px;
border
:1px solid #96C2F1; background-color: #EFF7FF;
cursor
:move; line-height:100px; text-align:center;
}
</style>
<script type="text/javascript">
(
function (document) {
//Usage: $("#id").drag()
//Author: hooyes
$.fn.Drag = function () {
var M = false;
var Rx, Ry;
var t = $(this);
t.mousedown(
function (event) {
Rx
= event.pageX - (parseInt(t.css("left")) || 0);
Ry
= event.pageY - (parseInt(t.css("top")) || 0);
t.css(
"position", "absolute").css('cursor', 'move').fadeTo(20, 0.5);
M
= true;
})
.mouseup(
function (event) {
M
= false; t.fadeTo(20, 1);
});
$(document).mousemove(
function (event) {
if (M) {
t.css({ top: event.pageY
- Ry, left: event.pageX - Rx });
}
});
}
})(document);

$(document).ready(

function () {
$(
"#Div1").Drag();
});
</script>
</head>
<body>
<div id="Div1" class="drag">hooyes</div>
</body>
</html>