jquery拖拽插件Easydrag

在一些有弹出框的页面,会经常用到拖拽效果来增加用户体验,避免因弹出框遮挡一些元素。如果用原生的js来写的话,js兼容性很不好控制,经常由于浏览器事件的不统一而影响效果,今天给大家介绍一个拖拽插件easydrag,EasyDrag可以指定可拖动的区域,一个setHandler搞定,不错开源是个好东西,只需要一行代码便可轻松在主流浏览器上

使用方法示例:

<!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 runat="server">
<title>Easydrag</title>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.easydrag.js"></script>
<script >
    $(document).ready ( function(){
        $("#divPanel").easydrag();
        $("#divPanel").setHandler("divTitle");
    });
</script>
</head>
<body>
<form id="form1" runat="server">
使用标题进行拖拽
    <div id="divPanel" style="width:300px;height:300px;background:white;border:1px solid #000000;position:absolute;left:5px;top:50px" >
        <div id="divTitle" style="width:100%;height:25px;background:lavender">&nbsp;Title</div>
        <div style="width:100%"></div>
    </div>
</form>
</body>
</html>

你可以点击这里下载示例

easydrag源代码:

/**
* EasyDrag 1.5 - Drag & Drop jQuery Plug-in
*
* Thanks for the community that is helping the improvement
* of this little piece of code.
*
* For usage instructions please visit http://fromvega.com/scripts
*/

(function($){

// to track if the mouse button is pressed
var isMouseDown = false;

// to track the current element being dragged
var currentElement = null;

// callback holders
var dropCallbacks = {};
var dragCallbacks = {};

// bubbling status
var bubblings = {};

// global position records
var lastMouseX;
var lastMouseY;
var lastElemTop;
var lastElemLeft;

// track element dragStatus
var dragStatus = {};

// if user is holding any handle or not
var holdingHandler = false;

// returns the mouse (cursor) current position
$.getMousePosition = function(e){
var posx = 0;
var posy = 0;

if (!e) var e = window.event;

if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}

return { ‘x‘: posx, ‘y‘: posy };
};

// updates the position of the current element being dragged
$.updatePosition = function(e) {
var pos = $.getMousePosition(e);

var spanX = (pos.x - lastMouseX);
var spanY = (pos.y - lastMouseY);

$(currentElement).css("top", (lastElemTop + spanY));
$(currentElement).css("left", (lastElemLeft + spanX));
};

// when the mouse is moved while the mouse button is pressed
$(document).mousemove(function(e){
if(isMouseDown && dragStatus[currentElement.id] != ‘false‘){
// update the position and call the registered function
$.updatePosition(e);
if(dragCallbacks[currentElement.id] != undefined){
dragCallbacks[currentElement.id](e, currentElement);
}

return false;
}
});

// when the mouse button is released
$(document).mouseup(function(e){
if(isMouseDown && dragStatus[currentElement.id] != ‘false‘){
isMouseDown = false;
if(dropCallbacks[currentElement.id] != undefined){
dropCallbacks[currentElement.id](e, currentElement);
}

return false;
}
});

// register the function to be called while an element is being dragged
$.fn.ondrag = function(callback){
return this.each(function(){
dragCallbacks[this.id] = callback;
});
};

// register the function to be called when an element is dropped
$.fn.ondrop = function(callback){
return this.each(function(){
dropCallbacks[this.id] = callback;
});
};

// disable the dragging feature for the element
$.fn.dragOff = function(){
return this.each(function(){
dragStatus[this.id] = ‘off‘;
});
};

// enable the dragging feature for the element
$.fn.dragOn = function(){
return this.each(function(){
dragStatus[this.id] = ‘on‘;
});
};

// set a child element as a handler
$.fn.setHandler = function(handlerId){
return this.each(function(){
var draggable = this;

// enable event bubbling so the user can reach the handle
bubblings[this.id] = true;

// reset cursor style
$(draggable).css("cursor", "");

// set current drag status
dragStatus[draggable.id] = "handler";

// change handle cursor type
$("#"+handlerId).css("cursor", "move");

// bind event handler
$("#"+handlerId).mousedown(function(e){
holdingHandler = true;
$(draggable).trigger(‘mousedown‘, e);
});

// bind event handler
$("#"+handlerId).mouseup(function(e){
holdingHandler = false;
});
});
}

// set an element as draggable - allowBubbling enables/disables event bubbling
$.fn.easydrag = function(allowBubbling){

return this.each(function(){

// if no id is defined assign a unique one
if(undefined == this.id || !this.id.length) this.id = "easydrag"+(new Date().getTime());

// save event bubbling status
bubblings[this.id] = allowBubbling ? true : false;

// set dragStatus 
dragStatus[this.id] = "on";

// change the mouse pointer
$(this).css("cursor", "move");

// when an element receives a mouse press
$(this).mousedown(function(e){

// just when "on" or "handler"
if((dragStatus[this.id] == "off") || (dragStatus[this.id] == "handler" && !holdingHandler))
return bubblings[this.id];

// set it as absolute positioned
$(this).css("position", "absolute");

// set z-index
$(this).css("z-index", parseInt( new Date().getTime()/1000 ));

// update track variables
isMouseDown = true;
currentElement = this;

// retrieve positioning properties
var pos = $.getMousePosition(e);
lastMouseX = pos.x;
lastMouseY = pos.y;

lastElemTop = this.offsetTop;
lastElemLeft = this.offsetLeft;

$.updatePosition(e);

return bubblings[this.id];
});
});
};

})(jQuery);

时间: 2024-10-03 13:39:03

jquery拖拽插件Easydrag的相关文章

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

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

JQuery之拖拽插件

一直以来,都对JS获取元素的位置感到非常的困惑:一会client.一会offset.一会scroll. 再加上各大浏览器之间的不兼容,唉,搞得哥晕晕乎乎的. 而很多页面效果都要用到这些位置.不得已,得练练,得记记. 下面就来说说这个基于 JQuery的简易拖拽插件吧. 按惯例,先说说拖拽的原理,以及搞这么一个东东的步骤: 那什么是拖拽呢? 看名字就知道了:就是把一个东东拖来拽去的. 放到我们的DOM上,就是改变它的位置. 它只有两个难点:1.如何知道是在拖? 2.如何知道从哪拖,拖到哪? 其实,

一步一步实现JS拖拽插件

js拖拽是常见的网页效果,本文将从零开始实现一个简单的js插件. 一.js拖拽插件的原理 常见的拖拽操作是什么样的呢?整过过程大概有下面几个步骤: 1.用鼠标点击被拖拽的元素 2.按住鼠标不放,移动鼠标 3.拖拽元素到一定位置,放开鼠标 这里的过程涉及到三个dom事件:onmousedown,onmousemove,onmouseup.所以拖拽的基本思路就是: 1.用鼠标点击被拖拽的元素触发onmousedown (1)设置当前元素的可拖拽为true,表示可以拖拽 (2)记录当前鼠标的坐标x,y

vue拖拽插件(弹框拖拽)

// =======拖拽 插件 cnpm install vuedraggableimport draggable from 'vuedraggable' <draggable v-model="tags" :move="getdata" @update="datadragEnd"> <transition-group> <div class="testdiv" v-for="eleme

jQuery拖拽小插件

基本实现思路是利用了onmousedown,onmouseup,onmousemove三种事件和css中的绝对定位 插件功能: 1.设置拖拽热点 2.设置被拖拽对象 3.设置拖拽范围 默认情况下,拖拽热点和被拖拽对象是同一个元素,拖拽范围是文档窗口 jQuery代码 (function($){ $.fn.drag=function(opts){ var defaults={ dragObj:$(this), scope:{ left:0, top:0, right:$(document).wid

103 extend 插件入口 jquery eq 和get的封装 拖拽插件

//demo.js window.onload = function () { //个人中心 $().getClass("member").hover(function(){ $(this).css("background","url(images/arrow2.png) no-repeat 55px center"); $().getClass("member_ul").show(); },function(){ $().g

自己写一个jqery的拖拽插件

说实话,jQuery比原生的js好用多了,本来想用原生写的,也写出来的,只是,感觉不像插件,所以用jQuery实现了一版. 实现的功能:可以指定拖拽的边界,在拖拽过程中,可以触发几个自定义事件 先说明一下我写的插件的原则: 1.常量分离出来,放在$.zUI.插件中 2.插件的主体执行函数命名为$.zUI.插件.fn 3.销毁函数命名为$.zUI.插件.unfn 这些规范,主要是为了以后写其他插件时,放在一起,精简代码用的,以后可能还会增加其他规则,以写出一个骨架来. 拖拽的原理其实比较简单,就是

JQUERY 拖拽 draggable droppable resizable selectable sortable

今天用了jq ui的拖动碰撞功能,好不容易看到有详细的API解说,记录如下: <script language="JavaScript" type="text/javascript" src="ui/jquery-1.8.2.js"></script> <script language="JavaScript" type="text/javascript" src="

拖拽插件SortableJS

在项目中,经常会遇到一些涉及到拖拽的需求,github上面有一个开源的SortableJS的插件,支持Vue,React,Angular等多种框架,实现效果很好,基本可以满足大部分的需求,下面就第一次在jquery中的使用做个简单的demo记录. 引入文件Sortable.min.js 指定包裹容器的id 根据api开始创建使用即可 <!DOCTYPE html> <html> <head> <meta charset="utf-8" />