Custom Drag Box Plugin

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.