开发者

JavaScript Prototype draggable

开发者 https://www.devze.com 2023-03-13 04:07 出处:网络
Using prototype I\'m trying to increment X and Y by 20px every time the object is moved. I can do something like:

Using prototype I'm trying to increment X and Y by 20px every time the object is moved. I can do something like:

new Draggable('element', {snap:[20, 20]});

and it will work, however, I need as well to constrain dragging to element's parent node with:

new Draggable('element', {
   snap: function(x, y, draggable) {
      function constrain(n, lower, upper) {
         if (n > upper) return upper;
         else if (n < lower) return lower;
         else return n;
      }
      var el开发者_JAVA技巧ement = draggable.element.getDimensions( );
      var parent = draggable.element.parentNode.getDimensions( );
      return [
         constrain((x + 20), 0, parent.width - element.width),
         constrain((y + 20), 0, parent.height - element.height)
      ];
   }
});

I can increment only by 20px only the first time is moved, but then it will do 1px increments. Thanks


return [
    constrain(Math.round(x/20)*20, 0, parent.width - element.width),
    constrain(Math.round(y/20)*20, 0, parent.height - element.height)
];
0

精彩评论

暂无评论...
验证码 换一张
取 消