Recently learned something a little too scattered, rest for a while, re-perfect before the JQ plug-in, today first drag it
//JavaScript Document(function($){ varDefaults ={actionelement:"",//gets the element of the event, not requiredRangeelement:window,//elements of a draggable range are not requiredDirection: "",//The default is null, which indicates how to drag arbitrarily, with an optional value of vertical or horizontalmagnetic:0//Magnetic adsorption size is not required } varopts = "";//Storage Parameters varDisx; varDisy; //save elements for subsequent operations vardragele,actionele,rangeelement; //Add a method like the jquery object$.fn.drag =function(options) {opts= $.extend (defaults, Options | | {} ); Dragele= $( This ); Rangeele=$ (opts.rangeelement); varActionselector; Opts.actionelement!= "" ? Actionselector = Opts.actionElement:actionSelector =NULL ; Dragele.on ("MouseDown", ACTIONSELECTOR,DRAGFN); return$( This). each (function(){}); } //Restricted range functionRange (value, MaxValue, MinValue) {if(Value > MaxValue-opts.magnetic) { returnMaxValue; }Else if(Value < MinValue +opts.magnetic) { returnMinValue; } returnvalue; } //drag-and-drop body function functionDRAGFN (EV) {DISX= Ev.clientx-Dragele.offset (). Left; Disy= Ev.clienty-Dragele.offset (). Top; $ (document). On ("MouseMove", MouseMove); $ (document). On ("MouseUp", MouseUp); return false; } //Mouse Move Event functionMouseMove (EV) {varleft = Ev.clientx-Disx; vartop = Ev.clienty-Disy; Left= Range (left, rangeele.width ()-dragele.width (), 0); Top= Range (top, rangeele.height ()-dragele.height (), 0); Switch(opts.direction) { Case"Horizontal": Dragele.css ({"Left": left}); Break; Case"Vertical": Dragele.css ({"Top": Top}); Break; default: Dragele.css ({"Left": Left, "top": Top}); Break; } } //Mouse Lift Remove event functionMouseUp () {$ (document). Off ("MouseUp", MouseUp); $ (document). Off ("MouseMove", MouseMove); }}) (JQuery)
How to use
$ ("#drag"). Drag ({ ". Action", "#container", "Horizontal ", });
Temporarily can not think of the need to leak out what parameters are more useful, for the time being, follow up if necessary to modify
Constantly optimize, refactor my Code-----Drag the jquery plugin