简体   繁体   English

AngularJS-可拖动的引导程序模态

[英]AngularJS - draggable bootstrap modal

I am using an AngularJS directive for modals, to make them draggable. 我对模态使用AngularJS指令,以使其可拖动。

This is the directive. 是指令。

In the demo, you can clearly see that if you drag it (especially left and right) it is slower than your mouse. 在演示中,您可以清楚地看到,如果拖动它(尤其是向左和向右),它的速度比鼠标慢。 I understand why this happens (the JavaScript calculates position relative to it's starting position, so in my 1920x1080 screen it goes from -1200px to 1920px on the x axis). 我知道为什么会发生这种情况(JavaScript计算相对于其起始位置的位置,因此在我的1920x1080屏幕上,它在x轴上从-1200px变为1920px)。 And I understand there is a need to use offset instead of position, but after many tries I failed to make it that. 我知道有必要使用偏移量而不是位置,但是经过多次尝试,我未能做到这一点。

This is the relevant JavaScript code: 这是相关的JavaScript代码:

element.on('mousedown', function (event) {
  // Prevent default dragging of selected content
  event.preventDefault();
  startX = event.screenX - x;
  startY = event.screenY - y;
  $document.on('mousemove', function mousemove(event) {
      y = event.screenY - startY;
      x = event.screenX - startX;
      element.css({
         top: y + 'px',
         left: x + 'px'
      });
   });
});

How can I make it rely on the offset and move together with the mouse and not slower? 如何使它依靠偏移量并与鼠标一起移动而不减慢速度?

Try this one: http://plnkr.co/edit/QxIdGj . 试试这个: http : //plnkr.co/edit/QxIdGj I have hardcoded 2 values, which you shouldn't do. 我已经硬编码了2个值,您不应该这样做。 Your "mistake" was that you were putting the draggable directive in the wrong element. 您的“错误”是您将可拖动指令放入了错误的元素。 I added the draggable directive to <div class="modal-content"> which is what I believe is the element that you want moved. 我在<div class="modal-content">中添加了draggable指令,我认为这是您要移动的元素。

I also changed your element.css({ to 我还更改了您的element.css({

element.css({
        top: event.clientY - 30 + 'px',
        left: event.clientX - 10 + 'px'
      });

It's using .clientX/Y which is the actual position of the mouse, without the need of further calculations. 它使用的是.clientX / Y,它是鼠标的实际位置,无需进一步计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM