jquery实现div移动(转帖)
2010-12-08 11:27
295 查看
1.jquery.move.js
(function($){
$.fn.jquerymove = function(){
var moveobj = $(this);
var old_position = {};
var new_position = {};
var offset = {};
var isover = 1;
var offset_fake = {};
moveobj.css({position: "absolute",cursor: "move" });
moveobj.mousedown(
function (e) {
old_position = {X:e.clientX,Y:e.clientY};
offset = moveobj.offset();
isover = 0;
$('body').append('<div id="_moveobj"></div>');
$('#_moveobj').css({
width: moveobj.width() - 50,
height: moveobj.height()-100,
left: offset.left,
top: offset.top - 20,
cursor: 'move',
position: 'absolute',
display: 'none',
zIndex: '10000',
border: '1px solid red'
});
$('#_moveobj').mouseup(
function (e) {
isover = 1;
$('#_moveobj').css({display: 'none'});
$('#_moveobj').remove();
}
);
$('#_moveobj').css({display: 'block'});
offset_fake = $('#_moveobj').offset();
}
);
$('body').mousemove(
function (e) {
if (!isover) {
new_position = {X:e.clientX,Y:e.clientY};
$('#_moveobj').css({
left: offset_fake.left+new_position.X-old_position.X,
top: offset_fake.top+new_position.Y-old_position.Y
})
moveobj.css({
left: offset.left+new_position.X-old_position.X,
top: offset.top+new_position.Y-old_position.Y
})
}
}
);
}
})(jQuery);
2.query.move.html
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.move.js"></script>
<script type="text/javascript" >
jQuery(document).ready(
function () {
// jQuery(".cn").jquerymove();
jQuery("#name").jquerymove();
}
);
</script>
</head>
<body>
<div id="name" class="cn" style="width:200px;height:200px;background:gray;text-align:center;">
</div>
</body>
</html>
转帖地址:http://bbs2.chinaunix.net/viewthread.php?tid=1309400
代码很容易就能看懂,按照自己的需求修改一下jquery.move.js里的函数,源文件在上面的地址下载就可以
(function($){
$.fn.jquerymove = function(){
var moveobj = $(this);
var old_position = {};
var new_position = {};
var offset = {};
var isover = 1;
var offset_fake = {};
moveobj.css({position: "absolute",cursor: "move" });
moveobj.mousedown(
function (e) {
old_position = {X:e.clientX,Y:e.clientY};
offset = moveobj.offset();
isover = 0;
$('body').append('<div id="_moveobj"></div>');
$('#_moveobj').css({
width: moveobj.width() - 50,
height: moveobj.height()-100,
left: offset.left,
top: offset.top - 20,
cursor: 'move',
position: 'absolute',
display: 'none',
zIndex: '10000',
border: '1px solid red'
});
$('#_moveobj').mouseup(
function (e) {
isover = 1;
$('#_moveobj').css({display: 'none'});
$('#_moveobj').remove();
}
);
$('#_moveobj').css({display: 'block'});
offset_fake = $('#_moveobj').offset();
}
);
$('body').mousemove(
function (e) {
if (!isover) {
new_position = {X:e.clientX,Y:e.clientY};
$('#_moveobj').css({
left: offset_fake.left+new_position.X-old_position.X,
top: offset_fake.top+new_position.Y-old_position.Y
})
moveobj.css({
left: offset.left+new_position.X-old_position.X,
top: offset.top+new_position.Y-old_position.Y
})
}
}
);
}
})(jQuery);
2.query.move.html
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.move.js"></script>
<script type="text/javascript" >
jQuery(document).ready(
function () {
// jQuery(".cn").jquerymove();
jQuery("#name").jquerymove();
}
);
</script>
</head>
<body>
<div id="name" class="cn" style="width:200px;height:200px;background:gray;text-align:center;">
</div>
</body>
</html>
转帖地址:http://bbs2.chinaunix.net/viewthread.php?tid=1309400
代码很容易就能看懂,按照自己的需求修改一下jquery.move.js里的函数,源文件在上面的地址下载就可以
相关文章推荐
- jquery实现div上下移动
- jQuery拖动div、移动div、弹出层实现原理及示例
- Jquery实现鼠标移动div
- jquery如何实现div可以随意移动
- jQuery实现div跟随鼠标移动
- jquery实现div跟随滚动条移动 不闪烁
- jQuery拖动div、移动div、弹出层实现原理及示例
- jquery实现div上下移动
- jquery 实现鼠标点击div盒子移动功能
- jquery实现div绝对居中+div可移动+后背景变暗
- Jquery实现Div上下移动示例
- Jquery实现Div上下移动示例
- 基于JQuery的Swiper插件实现移动手机端幻灯片切换(可在每一张幻灯片div中插入自定义的元素,代码简介,扩展性好。)
- 使用jquery实现DIV动画移动
- jquery实现select选择框内容左右移动代码分享
- jQuery实现点击按钮弹出一个div,点击其他区域关闭该div
- 基于jquery实现的定时显示与隐藏div广告的实现代码
- JS获取div滚动条距离实现弹出标签位置动态移动
- jquery监听div内容的变化具体实现思路
- 使用jquery实现div的tab切换实例代码