desandro / draggabilly

:point_down: Make that shiz draggable
https://draggabilly.desandro.com
MIT License
3.86k stars 386 forks source link

contain top-down, not bottom-up #185

Closed zzzgit closed 6 years ago

zzzgit commented 6 years ago

https://codepen.io/zzzgit/pen/wyLjYN

when the content is heigher than the container, the title will be unreachable after a dragging action. what i want is keeping the title alway in the container to make sure i can drag it anytime could you tell me how to modify the source code?

desandro commented 6 years ago

Thanks for reporting this bug. I'll add a fix for this in the next release. Until then you can fix this by adding this code:

function applyGrid( value, grid, method ) {
  method = method || 'round';
  return grid ? Math[ method ]( value / grid ) * grid : value;
}

Draggabilly.prototype.containDrag = function( axis, drag, grid ) {
  if ( !this.options.containment ) {
    return drag;
  }
  var measure = axis == 'x' ? 'width' : 'height';

  var rel = this.relativeStartPosition[ axis ];
  var min = applyGrid( -rel, grid, 'ceil' );
  var max = this.containSize[ measure ];
  max = applyGrid( max, grid, 'floor' );
  return  Math.max( min, Math.min( max, drag ) );
};

See demo https://codepen.io/desandro/pen/vREyZZ

zzzgit commented 6 years ago

okay thanks can you release a new npm package?

desandro commented 6 years ago

🎉 Draggabilly v2.2.0 has been released with a fix for this issue. 🎉

Please update! Closing this as resolved.