开发者

JavaScript Prototype draggable

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜