Webkit and jQuery draggable jumping

后端 未结 8 1093
误落风尘
误落风尘 2020-11-28 23:40

As an experiment, I created a few div\'s and rotated them using CSS3.

    .items { 
        position: absolute;
        cursor: pointer;
        background:          


        
8条回答
  •  鱼传尺愫
    2020-11-29 00:24

    David Wick is right about the general direction above, but computing the right coordinates is way more involved than that. Here's a more accurate monkey patch, based on MIT licensed Firebug code, which should work in far more situations where you have a complex DOM:

    Instead replace:

        //The element's absolute position on the page minus margins
        this.offset = this.positionAbs = this.element.offset();

    with the less hacky (be sure to get the whole thing; you'll need to scroll):

        //The element's absolute position on the page minus margins
        this.offset = this.positionAbs = getViewOffset(this.element[0]);
    
        function getViewOffset(node) {
          var x = 0, y = 0, win = node.ownerDocument.defaultView || window;
          if (node) addOffset(node);
          return { left: x, top: y };
    
          function getStyle(node) {
            return node.currentStyle || // IE
                   win.getComputedStyle(node, '');
          }
    
          function addOffset(node) {
            var p = node.offsetParent, style, X, Y;
            x += parseInt(node.offsetLeft, 10) || 0;
            y += parseInt(node.offsetTop, 10) || 0;
    
            if (p) {
              x -= parseInt(p.scrollLeft, 10) || 0;
              y -= parseInt(p.scrollTop, 10) || 0;
    
              if (p.nodeType == 1) {
                var parentStyle = getStyle(p)
                  , localName   = p.localName
                  , parent      = node.parentNode;
                if (parentStyle.position != 'static') {
                  x += parseInt(parentStyle.borderLeftWidth, 10) || 0;
                  y += parseInt(parentStyle.borderTopWidth, 10) || 0;
    
                  if (localName == 'TABLE') {
                    x += parseInt(parentStyle.paddingLeft, 10) || 0;
                    y += parseInt(parentStyle.paddingTop, 10) || 0;
                  }
                  else if (localName == 'BODY') {
                    style = getStyle(node);
                    x += parseInt(style.marginLeft, 10) || 0;
                    y += parseInt(style.marginTop, 10) || 0;
                  }
                }
                else if (localName == 'BODY') {
                  x += parseInt(parentStyle.borderLeftWidth, 10) || 0;
                  y += parseInt(parentStyle.borderTopWidth, 10) || 0;
                }
    
                while (p != parent) {
                  x -= parseInt(parent.scrollLeft, 10) || 0;
                  y -= parseInt(parent.scrollTop, 10) || 0;
                  parent = parent.parentNode;
                }
                addOffset(p);
              }
            }
            else {
              if (node.localName == 'BODY') {
                style = getStyle(node);
                x += parseInt(style.borderLeftWidth, 10) || 0;
                y += parseInt(style.borderTopWidth, 10) || 0;
    
                var htmlStyle = getStyle(node.parentNode);
                x -= parseInt(htmlStyle.paddingLeft, 10) || 0;
                y -= parseInt(htmlStyle.paddingTop, 10) || 0;
              }
    
              if ((X = node.scrollLeft)) x += parseInt(X, 10) || 0;
              if ((Y = node.scrollTop))  y += parseInt(Y, 10) || 0;
            }
          }
        }

    It's a shame the DOM doesn't expose these calculations natively.

提交回复
热议问题