Html 拖拽排序代码(需要引入jquery.js文件)
2014-01-03 17:43
701 查看
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试的拖拽功能</title>
<style type="text/css">
body, div { margin: 0; padding: 0; font-size: 12px; }
body { width: 960px; margin: 0 auto; }
ul, li { margin: 0; padding: 0; list-style: none; }
.clear { clear: both; width: 1px; height: 0px; line-height: 0px; font-size: 1px; }
.box { width: 800px; height: auto; float:left; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; }
.main { position: static; float:left; width: 250px; height: 80px; margin-bottom: 5px; margin-left:10px; border: 1px solid blue; background: #ccc; }
.maindash { position: absolute; width: 250px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; }
.hide { width: 250px; height: 80px; margin-bottom: 5px; }
.dash { position: static; width: 250px; float:left; height: 80px; margin-bottom: 5px; margin-left:10px; border: 1px dashed #f00; };
</style>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
var range = { x: 0, y: 0 };//鼠标元素偏移量
var lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; //拖拽对象的四个坐标
var tarPos = { x: 0, y: 0, x1: 0, y1: 0 }; //目标元素对象的坐标初始化
var theDiv = null, move = false;//拖拽对象 拖拽状态
var theDivId =0,
theDivHeight = 0,
theDivWidth= 0,
theDivHalf = 0,
theDivWHalf= 0,
tarFirstX = 0,
tarFirstY = 0; //拖拽对象的索引、高度、的初始化。
var tarDiv = null, tarFirst, tempDiv, tempDiv2; //要插入的目标元素的对象, 临时的虚线对象
$(".main").each(function(){
$(this).mousedown(function (event){
$("#eventString").html("");
$("#eventString2").html("");
//拖拽对象
theDiv = $(this);
//鼠标元素相对偏移量
range.x = event.pageX - theDiv.offset().left;
range.y = event.pageY - theDiv.offset().top;
theDivId = theDiv.index();
theDivHeight = theDiv.height();
theDivWidth = theDiv.width();
theDivHalf = theDivHeight/2;
theDivWHalf= theDivWidth/2;
move = true;
theDiv.attr("class","maindash");
// 创建新元素 插入拖拽元素之前的位置(虚线框)
$("<div class='dash'></div>").insertBefore(theDiv);
$(".dash").mouseup(function(event) {
$("#eventString").html("dash is mouseup");
theDiv.attr("class", "main");
if($(".dash"))
{
$(".dash").remove();
}
move = false;
});
});
});
$(document).mousemove(function(event) {
if (!move) return false;
document.documentElement.style["WebkitUserSelect"] = "none";
document.unselectable = "on";
document.onselectstart = function(){
return false;
}
lastPos.x = event.pageX; //鼠标的x坐标
lastPos.y = event.pageY; //鼠标的y坐标
lastPos.y1 = lastPos.y + theDivHeight;
lastPos.x1 = lastPos.x + theDivWidth;
// 拖拽元素随鼠标移动
theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'});
// 拖拽元素随鼠标移动 查找插入目标元素
var $main = $('.main'); // 局部变量:按照重新排列过的顺序 再次获取 各个元素的坐标,
tempDiv = $(".dash"); //获得临时 虚线框的对象
$main.each(function () {
tarDiv = $(this);
tarPos.x = tarDiv.offset().left;
tarPos.y = tarDiv.offset().top;
tarPos.y1 = tarPos.y + tarDiv.height();
tarPos.x1 = tarPos.x + tarDiv.width();
tarFirst = $main.eq(0); // 获得第一个元素
tarFirstY = tarFirst.offset().top; // 第一个元素对象的中心纵坐标
tarFirstX = tarFirst.offset().left;
//判断要插入目标元素的 坐标后, 直接插入
if (lastPos.y >= tarPos.y
&& lastPos.x >= tarPos.x
&& lastPos.y <= tarPos.y1
&& lastPos.x <= tarPos.x1) {
var classValue= tarDiv.prev().attr("class");
if(classValue == "main")
{
$("#eventString3").html("check-------"+classValue);
}
$("#eventString2").html(theDiv.prev().attr("class"));
if(tarDiv.prev("div").length >0 && tarDiv.next("div").length>0)
{
$("#eventString2").html(tarDiv.html()+" : "+tarDiv.prev(".main").html());
tempDiv.insertAfter(tarDiv.prev("div"));
}
/*else if(tarDiv.prev(".dash") || tarDiv.prev(".maindash"))
{
$("#eventString2").html(tarDiv.html());
tempDiv.insertAfter(tarDiv);
}*/
else if(tarDiv.next("div").length<=0)
{
$("#eventString2").html(tarDiv.html());
tempDiv.insertAfter(tarDiv);
}
else
{
tempDiv.insertBefore(tarDiv);
}
}
});
}).mouseup(function(event) {
$("#eventString2").html("document is mouseup");
theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虚线div的位置上
theDiv.attr("class", "main"); //恢复对象的初始样式
if($(".dash"))
{
$(".dash").remove();
} // 删除新建的虚线div
move=false;
});
});
</script>
</head>
<body onselectionstart=“javascript:return false;”>
<div class="box" id="box">
<div class="main" id="main0">div1</div>
<div class="main" id="main1">div2</div>
<div class="main" id="main2">div3</div>
<div class="main" id="main3">div4</div>
<div class="main" id="main4">div5</div>
<div class="main" id="main5">div6</div>
<div class="main" id="main6">div7</div>
<div class="main" id="main7">div8</div>
<div class="main" id="main8">div9</div>
<div class="main" id="main9">div10</div>
</div>
<div>
<span id="eventString">
</span>
<br />
<span id="eventString2">
</span>
<br />
<span id="eventString3">
</span>
</div>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试的拖拽功能</title>
<style type="text/css">
body, div { margin: 0; padding: 0; font-size: 12px; }
body { width: 960px; margin: 0 auto; }
ul, li { margin: 0; padding: 0; list-style: none; }
.clear { clear: both; width: 1px; height: 0px; line-height: 0px; font-size: 1px; }
.box { width: 800px; height: auto; float:left; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; }
.main { position: static; float:left; width: 250px; height: 80px; margin-bottom: 5px; margin-left:10px; border: 1px solid blue; background: #ccc; }
.maindash { position: absolute; width: 250px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; }
.hide { width: 250px; height: 80px; margin-bottom: 5px; }
.dash { position: static; width: 250px; float:left; height: 80px; margin-bottom: 5px; margin-left:10px; border: 1px dashed #f00; };
</style>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
var range = { x: 0, y: 0 };//鼠标元素偏移量
var lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; //拖拽对象的四个坐标
var tarPos = { x: 0, y: 0, x1: 0, y1: 0 }; //目标元素对象的坐标初始化
var theDiv = null, move = false;//拖拽对象 拖拽状态
var theDivId =0,
theDivHeight = 0,
theDivWidth= 0,
theDivHalf = 0,
theDivWHalf= 0,
tarFirstX = 0,
tarFirstY = 0; //拖拽对象的索引、高度、的初始化。
var tarDiv = null, tarFirst, tempDiv, tempDiv2; //要插入的目标元素的对象, 临时的虚线对象
$(".main").each(function(){
$(this).mousedown(function (event){
$("#eventString").html("");
$("#eventString2").html("");
//拖拽对象
theDiv = $(this);
//鼠标元素相对偏移量
range.x = event.pageX - theDiv.offset().left;
range.y = event.pageY - theDiv.offset().top;
theDivId = theDiv.index();
theDivHeight = theDiv.height();
theDivWidth = theDiv.width();
theDivHalf = theDivHeight/2;
theDivWHalf= theDivWidth/2;
move = true;
theDiv.attr("class","maindash");
// 创建新元素 插入拖拽元素之前的位置(虚线框)
$("<div class='dash'></div>").insertBefore(theDiv);
$(".dash").mouseup(function(event) {
$("#eventString").html("dash is mouseup");
theDiv.attr("class", "main");
if($(".dash"))
{
$(".dash").remove();
}
move = false;
});
});
});
$(document).mousemove(function(event) {
if (!move) return false;
document.documentElement.style["WebkitUserSelect"] = "none";
document.unselectable = "on";
document.onselectstart = function(){
return false;
}
lastPos.x = event.pageX; //鼠标的x坐标
lastPos.y = event.pageY; //鼠标的y坐标
lastPos.y1 = lastPos.y + theDivHeight;
lastPos.x1 = lastPos.x + theDivWidth;
// 拖拽元素随鼠标移动
theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'});
// 拖拽元素随鼠标移动 查找插入目标元素
var $main = $('.main'); // 局部变量:按照重新排列过的顺序 再次获取 各个元素的坐标,
tempDiv = $(".dash"); //获得临时 虚线框的对象
$main.each(function () {
tarDiv = $(this);
tarPos.x = tarDiv.offset().left;
tarPos.y = tarDiv.offset().top;
tarPos.y1 = tarPos.y + tarDiv.height();
tarPos.x1 = tarPos.x + tarDiv.width();
tarFirst = $main.eq(0); // 获得第一个元素
tarFirstY = tarFirst.offset().top; // 第一个元素对象的中心纵坐标
tarFirstX = tarFirst.offset().left;
//判断要插入目标元素的 坐标后, 直接插入
if (lastPos.y >= tarPos.y
&& lastPos.x >= tarPos.x
&& lastPos.y <= tarPos.y1
&& lastPos.x <= tarPos.x1) {
var classValue= tarDiv.prev().attr("class");
if(classValue == "main")
{
$("#eventString3").html("check-------"+classValue);
}
$("#eventString2").html(theDiv.prev().attr("class"));
if(tarDiv.prev("div").length >0 && tarDiv.next("div").length>0)
{
$("#eventString2").html(tarDiv.html()+" : "+tarDiv.prev(".main").html());
tempDiv.insertAfter(tarDiv.prev("div"));
}
/*else if(tarDiv.prev(".dash") || tarDiv.prev(".maindash"))
{
$("#eventString2").html(tarDiv.html());
tempDiv.insertAfter(tarDiv);
}*/
else if(tarDiv.next("div").length<=0)
{
$("#eventString2").html(tarDiv.html());
tempDiv.insertAfter(tarDiv);
}
else
{
tempDiv.insertBefore(tarDiv);
}
}
});
}).mouseup(function(event) {
$("#eventString2").html("document is mouseup");
theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虚线div的位置上
theDiv.attr("class", "main"); //恢复对象的初始样式
if($(".dash"))
{
$(".dash").remove();
} // 删除新建的虚线div
move=false;
});
});
</script>
</head>
<body onselectionstart=“javascript:return false;”>
<div class="box" id="box">
<div class="main" id="main0">div1</div>
<div class="main" id="main1">div2</div>
<div class="main" id="main2">div3</div>
<div class="main" id="main3">div4</div>
<div class="main" id="main4">div5</div>
<div class="main" id="main5">div6</div>
<div class="main" id="main6">div7</div>
<div class="main" id="main7">div8</div>
<div class="main" id="main8">div9</div>
<div class="main" id="main9">div10</div>
</div>
<div>
<span id="eventString">
</span>
<br />
<span id="eventString2">
</span>
<br />
<span id="eventString3">
</span>
</div>
</body>
</html>
相关文章推荐
- html引入样式表和js文件中间用逗号隔开是什么意思和(function($) {...})(jquery);的意思
- 学习vue-cli时需要引入外部js库文件(如jquery)的问题,百度后解决了,再次备忘一下
- AJAX提交表单后要清空,否则再次提交原来的数据会认为重复提交,提交失败。使用ajaxSubmit 函数需要引入jquery.form.min.js 文件
- html中table数据排序的js代码
- HTML文件中引入其他HTML代码片段
- 自己编写jQuery动态引入js文件插件 (jquery.import.dynamic.script)
- 在Hexo发布博客的MarkDown文件中引入JS代码
- Html5+jquery版工作流设计器(开源的js文件)
- iOS之在webView中引入本地html,image,js,css文件的方法
- HTML/JSP引入JS文件路径问题解析
- html+css+jquery倒计时、拖拽div、滚动条固定在低端、div自动换行、输入框不可写等免费小代码实用学习版.txt
- Eclipse支持HTML&JS&ExtJS&jQuery代码智能提示
- JS+CSS制作DIV层可(最小化/拖拽/排序)功能实现代码
- js/jquery/html前端开发常用到代码片段
- HtmlWebpackPlugin以inine方式引入JS/CSS文件
- 简单的jquery拖拽排序效果实现代码
- 使用jQuery插件时避免重复引入jquery.js文件
- Java实现拖拽文件上传dropzone.js的简单使用示例代码
- 为什么直接把javascript的代码写进html就行但是调用js文件就不行