angular-sortable-view icon indicating copy to clipboard operation
angular-sortable-view copied to clipboard

Wrong position of elements being dragged within a modal

Open drinkbird opened this issue 8 years ago • 8 comments

I've tried using angular-sortable-view within a bootstrap modal window, but when an element is being dragged, it jumps to the right side of the screen.

I presume that happens because the modal is absolutely positioned, but the directive doesn't consider that while calculating the value for 'left' positioning.

drinkbird avatar Apr 04 '16 13:04 drinkbird

Same issue here.

jegorami avatar Apr 05 '16 18:04 jegorami

having the same issue

JSOdin avatar Jun 12 '16 03:06 JSOdin

Have you tried to use sv-element="{containment:'.myModalWindow'}"? I had the same behavior that my dragged element jumped somewhere else and above code helped me.

gehrmanng avatar Jun 12 '16 13:06 gehrmanng

same issue

StefanoVollono avatar Jun 23 '16 14:06 StefanoVollono

Thanks @gehrmang I will give it a try

drinkbird avatar Jun 23 '16 18:06 drinkbird

  1. I add: sv-element="{containment:'.myModalWindow'}"

  2. In my css, I change the vertical align of modal from "transform" to negative margin .myModalWindow { height: 666px; min-height: 0; position: absolute; right: 0; top: 50%; // transform: translate(0, -50%); // NOT GOOD FOR DRAG margin-top: -333px; // THIS SOLVE FOR ME }

i hope help you

StefanoVollono avatar Jun 27 '16 15:06 StefanoVollono

If you're using bootstrap / ui-bootstrap (angular) modals and looking for a simple one-liner fix here it is:

.modal-dialog { transform: none !important; }

Note: This override is obviously far reaching so just modify as you see fit.

The caveat here is that modals no longer slide down but just fade in (which I actually like better).

Now that the dragged elements are being positioned properly you can constrain them to a particular area using the following line on the draggable item's dom element as mentioned by others above:

sv-element="{containment:'.some-class-that-is-on-the-constraining-parent-dom-element'}"

Credit and thanks to the previous posters because I was pulling my hair out (and there's not much left) trying to figure out what the heck was causing the incorrect positioning in the first place!

TheLostBrain avatar Nov 04 '16 14:11 TheLostBrain

^ I didn't originally see this answer, which works great, so I wrote a directive to make the dragged item account for the modal position.

app.directive('svFix', ['$window', '$timeout', function($window, $timeout){
  return {
    restrict: 'A',
    link: function(scope, el) {
      angular.element($window).on('resize', resize);
      var styleEl = $('<style>').appendTo('head');
      $timeout(setTransform, 1000); // Waits for the modal to finish it's animation, so that the position values are correct..

      scope.$on('$destroy', function() {
        angular.element($window).off('resize', resize);
        styleEl.remove();
      });

      function setTransform() {
        var rect = el[0].getBoundingClientRect();
        var propStr = 'transform: translate(-' + rect.left + 'px, -' + rect.top + 'px);';
        styleEl.html('.sv-helper{' + 
          '-webkit-' + propStr
          '-ms-' + propStr
          propStr +
        '}', 0);
      }
      function resize() {
        if(styleEl) setTransform();
      }      
    }
  };
}]);

Just add sv-fix attribute to the outer-most element in the modal-body!

mbryk avatar Feb 01 '18 20:02 mbryk