1, the HTML code follows the introduction of jquery and the drag.js of the self-calibration, the path to open the page correctly can see the effect
<!doctype html>
2. Custom Drag.js/** * @author: Wangl */(function ($) {/** plug-in entry **/$.fn. Wangldrag = function (options) {var z_index = 0;this.each (function () {////parameter priority from high to Low: attribute parameter, call parameter, default parameter var opts = $.extend ({} , $.fn. Wangldrag.defaults,options,$.fn. Wangldrag.parseoptions (this)), alert (Json.stringify (opts)), Opts.box = $ (this); Init (opts);}); var z_index = -1;/** initialization **/function init (opts) {var $box = Opts.box,x = 0,y = 0,left = 0,top = 0,b_width = $ (document). Width (), B_height = $ (document). Height (), maxWidth = b_width-$box. Outerwidth (), maxheight = b_height-$box. Outerheight (); $ Handle = Opts.handle? $box. Find (Opts.handle): $box, $handle. CSS ("cursor", ' move '), $box. On (' MouseMove ', function (e) {$box. css ({"Z-index": Z _index++}); $handle. On (' MouseDown ', function (e) {x = E.clientx;y = E.clienty;var $this = $ (this); var offset = $ (this). offset (); Fset.left;top = Offset.top;var Isdrag = True;//alert (' x: ' +x+ ', y: ' +y+ ', left: ' +left+ ', Top: ' +top '); $ (document). On (' MouseMove ', function (e) {if (Isdrag) {var new_left = left + E.clientx-x;var NEW_top = top + e.clienty-y;if (opts.arrow && opts.arrow== "Top") new_left=left;//controls the vertical direction if (Opts.arrow && opts.arrow== "left") new_top=top;//control horizontal direction if (new_left<=0) New_left = 2;if (new_top<=0) new_top = 2;if (New_left > = maxWidth) New_left = maxwidth;if (new_top >= maxheight) new_top = maxheight; $box. css ({"Top": New_top, "left": New_left });}}). On (' MouseUp ', function () {Isdrag = false;});};/ * * Default parameter */$.fn. Wangldrag.defaults = {handle: "", Arrow: ""}/** attribute parameter */$.fn. Wangldrag.parseoptions = function (target) {var $target = $ (target), return {handle: $target. attr (' handle '), Arrow: $ Target.attr (' Arrow ')}}) (JQuery)
Custom Drag Box Plugin