jQuery 鼠标拖拽排序

<!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; paading: 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: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; }
		.main { position: static; width: 600px; height: 80px; margin-bottom: 5px; border: 1px solid blue; background: #ccc; }
		.maindash { position: absolute; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; }

		.hide { width: 600px; height: 80px; margin-bottom: 5px; }
		.dash { position: sta;tic; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; };
	</style>

	<script type="text/javascript" src="jquery.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, theDivHalf = 0; tarFirstY = 0; //拖拽对象的索引、高度、的初始化。
	    var tarDiv = null, tarFirst, tempDiv;  //要插入的目标元素的对象, 临时的虚线对象

	    $(".main").each(function(){
	    	$(this).mousedown(function (event){
	            //拖拽对象
	            theDiv = $(this);

	            //鼠标元素相对偏移量
	            range.x = event.pageX - theDiv.offset().left;
	            range.y = event.pageY - theDiv.offset().top;

	            theDivId = theDiv.index();
	            theDivHeight = theDiv.height();
	            theDivHalf = theDivHeight/2;
	            move = true;

	            theDiv.attr("class","maindash");
	            // 创建新元素 插入拖拽元素之前的位置(虚线框)
	            $("<div class=‘dash‘></div>").insertBefore(theDiv);
	        });
	    });

	    $(document).mousemove(function(event) {
	    	if (!move) return false;
	    	lastPos.x = event.pageX - range.x;
	    	lastPos.y = event.pageY - range.y;
	    	lastPos.y1 = lastPos.y + theDivHeight;

	        // 拖拽元素随鼠标移动
	        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()/2;

	            tarFirst = $main.eq(0); // 获得第一个元素
	            tarFirstY = tarFirst.offset().top + theDivHalf ; // 第一个元素对象的中心纵坐标

	            //拖拽对象 移动到第一个位置
	            if (lastPos.y <= tarFirstY) {
	            	tempDiv.insertBefore(tarFirst);
	            }
	            //判断要插入目标元素的 坐标后, 直接插入
	            if (lastPos.y >= tarPos.y - theDivHalf && lastPos.y1 >= tarPos.y1 ) {
	            	tempDiv.insertAfter(tarDiv);
	            }
	        });
	    }).mouseup(function(event) {
	        theDiv.insertBefore(tempDiv);  // 拖拽元素插入到 虚线div的位置上
	        theDiv.attr("class", "main"); //恢复对象的初始样式
	        $(‘.dash‘).remove(); // 删除新建的虚线div
	        move=false;
	    });
	});
</script>
</head>
<body>
	<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>
</body>
</html>

  

时间: 2024-08-28 04:57:47

jQuery 鼠标拖拽排序的相关文章

jquery鼠标拖拽效果分享

//html代码部分 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="../js/jquery-1.11.1.js"></script> </head> <style> body

jquery实现鼠标拖拽排序功能

<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>jQuery UI Sortable - Default functionality</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/

jQuery拖拽插件制作拖拽排序特效

基于jQuery拖拽插件制作拖拽排序特效是一款非常实用的鼠标拖拽布局插件.效果图如下: 在线预览   源码下载 实现的代码. html代码: <h1>水平拖拽</h1> <div class="demo"> <div class="item item1"><span>1</span></div> <div class="item item2"><

jquery -- 拖拽排序分析

今天应一个朋友的委托,研究一下拖拽排序,我记得我上次写拖拽排序,因为方法太死板,效果我一直不是很满意,一直想再从写一个,一直没机会(懒),这次因为公司部门变动所以有了一些时间(无聊)来写,本来准备使用Vue写,奈何功夫不到家在自定义指令的时候,问题卡住了,研究了一段时间之后,还是决定放弃,研究一下Vue再来写过,所以本次还是用了Jquery来写. 直接上代码 这是CSS部分 1 *{/*Css*/ 2 margin: 0px; 3 padding: 0px; 4 list-style: none

一款基于jQuery的支持鼠标拖拽滑动焦点图

记得之前我们分享过一款jQuery全屏广告图片焦点图,图片切换效果还不错.今天我们要分享另外一款jQuery焦点图插件,它的特点是支持鼠标拖拽滑动,所以在移动设备上使用更加方便,你只要用手指滑动屏幕即可切换图片. 在线预览   源码下载 实现的代码. html代码: <div class="main_visual"> <div class="flicking_con"> <a class="on" href=&quo

jQuery实现div横向拖拽排序

参考:https://blog.csdn.net/kongjiea/article/details/45578033 效果图: html <h1>div横向拖拽排序</h1> <div class="box"> <div class="horizontal-div" id="div1">div1</div> <div class="horizontal-div"

jQuery可拖拽3D万花筒旋转特效

jQuery可拖拽3D万花筒旋转特效 这是一个使用了CSS3立体效果的强大特效,本特效使用jQuery跟CSS3 transform来实现在用户鼠标按下拖动时,环形图片墙可以跟随鼠标进行3D旋转动画. 效果体验:http://hovertree.com/texiao/jquery/92/ 进去后可以上下左右的拖动图片. 本示例中使用到了CSS3的transform-style 属性,该规定如何在 3D 空间中呈现被嵌套的元素. 默认值: flat继承性: no版本: CSS3JavaScript

使用knockout-sortable实现对自定义菜单的拖拽排序

在开始之前,照例,我们先看效果和功能实现. 关于自定义菜单的实现,这里就不多说了,需要了解的请访问:http://www.cnblogs.com/codelove/p/4838766.html 这里需要说明的是排序的实现. 我们先来看看关键的页面代码: <div class="row"> <div class="col-lg-12 full-width" id="leftMenus"> <div class=&quo

jquery实现拖拽的效果

上篇讲的是原生js实现拖拽的效果,本篇是jquery实现拖拽的效果. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="utf-8"/> <title> New Documen