JS get mouse and reposition mouse position

Source: Internet
Author: User


//Get position
 pt.getpos = function (obj, isnonstatic) {
  var left = 0, top = 0;
 &nb Sp;while (obj) {
   left + = Obj.offsetleft;
   top + = obj.offsettop;
 &N Bsp; obj = obj.offsetparent;
   if (isnonstatic && this.isnonstaticpos (obj)) {
    break;
  &NBSP}
  }
  return {left:left, top:top};
 }

 //mouse positioning
 pt.fallowmouse = function (e, obj, dir) {
  e = e | | window.event;
 &N Bsp;if (obj) | | |!e) {//non-static positioned left top positioning is not valid, or is not an event object
   return false;
&N BSP;&NBSP}
  dir = Dir | | {left:false, top:false};
  var ex = E.clientx;
  var ey = e.clienty;
  var bl = document.body.scrollLeft | | document.documentElement.scrollLeft;
  var bt = Document.body.scrollTop | | document.documentElement.scrollTop;
  var bw = Document.body.offsetWidth | | document.documentElement.offsetWidth;
  //var bh = document.body.offsetHeight | | document.documentElement.offsetHeight;
  var bh = document.documentElement.clientHeight;
  var ow = obj.offsetwidth;
  var oh = obj.offsetheight;

var ol = Dir.left? (Ex-ow < BL ex + Bl:ex + bl-ow): (ex + ow > BW ex + bl-ow:ex + BL);
OL = Math.max (0, BL, OL);
var ot = dir.top? (Ey-oh < BT EY + Bt:ey + Bt-oh): (ey + OH > BH ey + bt-oh:ey + BT);
ot = Math.max (0, BT, OT);

var pnode = This.nearestnonstaticposparenode (obj);
if (Pnode) {
var pos = This.getpos (Pnode);
OL-= Pos.left;
OT-= pos.top;
}
Obj.style.left = ol + ' px ';
Obj.style.top = ot + ' px ';
}

 //Screen central positioning
 pt.setcenter = function (obj) {
  var OCW = obj.offsetwidth;
  v Ar och = obj.offsetheight;
  var BSL = Document.body.scrollLeft | | document.documentElement.scrollLeft;
  var BST = Document.body.scrollTop | | document.documentElement.scrollTop;
  var BCW = document.documentElement.offsetWidth;
  var BCH = document.documentElement.offsetHeight;
  var OSL = BSL + Math.floor ((BCW-OCW)/2);
   osl = Math.max (BSL, OSL);
  var ost = BST + Math.floor ((Bch-och)/2);
   ost = Math.max (BST, OST);
  obj.style.left = OSL + ' px ';
  obj.style.top = ost + ' px ';
 }

 pt.showautoadapttext = function (node, text, pixelwidth, suffix) {
  if (node.nodename) {
   pixelwidth = Pixelwidth | | 14/95;
   var len1 = Text.getlen ();
   if (typeof Text = = ' string ') {
    var len2 = Math.floor (Node.clientwidt h * pixelwidth);
    var tstr = ';
    suffix = suffix | | '...';
    var vstr = len1 > Len2 text.substringpro (len2-suffix.length): text;
    if (Text!= vstr) {
     tstr = text;
   & Nbsp; vstr + = suffix;
    }
    node.title = tstr;
    node.value = vstr;
   }
  }
 }

 pt.getsubcodes = function (data, _code) {
  if (Data.subcodescache = = undefined) {
&NBSP;&N Bsp; data.subcodescache = [];
   for (var code in data) {
    if (/^[d]{2} ([d][1-9]) | ( [1-9] [d])) $/.test (code) {
     var parecode = code.substr (0, 2) + ';
&NBSP;&NBSP;&NBSP;&N Bsp; if (Data.subcodescache[parecode] = = undefined) {
       Data.subcodescache[parecode] = [];
     }
     data.subcodescache[parecode].push (code);
    }
   }
  }
  return Data.subcodescache[_code] | | [];
 }

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.