jquery实现拖拽、复制、放置
来源:互联网 发布:知乎周刊和知乎一样吗 编辑:程序博客网 时间:2024/06/05 20:12
直接代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>拖拽复制</title>
<style>
.divCopy {
width: 100px;
height: 100px;
background: #ff6666;
position: absolute;
left: 0;
top: 0;
cursor: move;
z-index: 5;
border: 2px dashed green;
}
.box {
width: 320px;
height: 320px;
position: absolute;
top: 10px;
right: 10px;
border: 2px dashed #ccc;
}
.divCopyStyle{
float: left;
margin-top: 2px;
margin-left: 2px;
}
</style>
<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div class="div1 divCopy">
drag
</div>
<div class="box" id="box" draggable ="false">
</div>
<script>
var oldPageX, oldPageY, cloneHtml, dragging;
//1.鼠标按下时复制目标元素
$(document).on("mousedown",".div1",function(event){
dragging = true;
if (dragging) {
var _this = $(event.currentTarget);
oldPageX = event.pageX;
oldPageY = event.pageY;
cloneHtml = $(_this).clone();
oldoffset = $(_this).offset();
$(document.body).append(cloneHtml)
cloneHtml.css({
position: 'fixed',
left: oldoffset.left,
top: oldoffset.top +10
});
}
})
$(document).on({
//2.寻找位置做判断处理
'mousemove':function(event){
if(dragging){
var currPageX = event.pageX;
var currPageY = event.pageY;
var movePageX = currPageX - oldPageX;
var movePageY = currPageY - oldPageX;
cloneHtml.css({
opacity: '0.8',
position: 'fixed',
left: oldoffset.left + movePageX,
top: oldoffset.top - 10 + movePageY,
'z-index': 5
});
var targetBox = $("#box");
var targetBoxOffset = targetBox.offset();
if(currPageX > targetBoxOffset.left && currPageX <targetBoxOffset.left +300 && currPageY > targetBoxOffset.top && currPageY <targetBoxOffset.top +300){
targetBox.css("border","2px dashed blue");
}else{
targetBox.css("border","2px dashed #ccc");
}
}
},
})
//3.释放鼠标将目标元素放置
$(document).on("mouseup",".div1",function(event){
var currPageX = event.pageX;
var currPageY = event.pageY;
var targetBox = $("#box");
var targetBoxOffset = targetBox.offset();
if(!(currPageX > targetBoxOffset.left && currPageX <targetBoxOffset.left +300 && currPageY > targetBoxOffset.top && currPageY <targetBoxOffset.top +300)){
cloneHtml.remove();
dragging = false;
return;
};
console.log($(".box").find(".divCopy").length);
targetBox.append(cloneHtml);
$(event.target).removeClass("div1");
$(event.target).css("position","static");
$(event.target).addClass("divCopyStyle");
targetBox.css("border","2px dashed #ccc");
$(event.target).removeAttr("onmousedown");
dragging = false;
if($(".box").find(".divCopy").length >9){
$(event.target).remove();
}
})
</script>
</body>
</html>
- jquery实现拖拽、复制、放置
- 利用jquery插件中的拖拽与放置实现的相册效果,可以删除和恢复
- JQuery 无插件实现seclet option标签的拖拽放置
- jQuery实现复制节点
- jQuery实现复制到剪切板
- js拖拽和放置效果
- ext js 中tree拖拽放置
- Jquery实现复制内容到粘贴板
- jQuery 实现模块的复制和删除
- jquery实现多个点击复制按钮
- jquery实现table行复制粘贴功能
- Select JQuery 实现左右复制移动内容
- jquery实现复制到剪贴板 通用
- Jquery实现拖拽
- jquery实现拖拽
- jQuery实现拖拽
- 实现ImageButton里放置文字
- js/jquery随机放置网页内容
- 冒泡排序及其优化
- Caused by: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMe
- arm开发板 libusb_open打开失败问题
- 关于unix网络编程第六章select与stdio混用会产生错误的原因解释
- excle 文件上传
- jquery实现拖拽、复制、放置
- Oracle.DataAccess.dll 下载 dll之家
- Cookie工具类
- [leetcode]15. 3Sum
- AlertDialog显示错误 Unable to add window token null is not for an application
- XRecyclerView下拉刷新和上拉加载
- 150盏亮着的电灯,各有一个拉线开关控制,编号为1~150 将编号为1~150倍数的灯,依次拉一下 问:拉完后亮着的灯数为几盏? 哪盏灯被拉的最多?
- 使用shape属性在api15及api16 上出现黑背景的解决方案
- linux环境变量配置后报错..-bash: /usr/local/java/jdk1.7.0_55/bin/java: /lib/ld-linux.so.2: bad ELF interpreter