jQuery UI 可拖动不会返回到第一个点

发布于 2024-11-27 18:30:54 字数 1324 浏览 0 评论 0原文

我有一个 jQuery UI 可拖动元素。这非常简单。它只是一个 div(容器),其中另一个 div(可拖动的部分)设置为网格。问题是我移动元素一次后我无法返回到第一个点。如果我更改网格大小,它可以工作,但我需要它在该网格上工作,因为它匹配其下面的某些元素

相关 JS:

 $('<div class="slider_wrap"><div class="slider"></div></div>').appendTo('#chart');

 $('#chart .slider')
    .draggable({
      containment:'parent',
      grid:[distanceBetweenPoints,0],
      opacity: 0.25
    })
    .bind('mousedown', function(e, ui){
      // bring target to front
      $(e.target.parentElement).append( e.target );
    })
    .bind('drag', function(e, ui){
      // update coordinates manually, since top/left style props don't work on SVG
      e.target.setAttribute('x', ui.position.left);
    })
    .bind('dragstop',function(e, ui){
      //a = true offset of slider piece
      var a = ui.position.left + distanceBetweenPoints;
      var b = containerWidth;
      var c = thePoints.length;
      var d = b / c;
      var x = a / d;
      //Since the points are in an array which starts at 0, not 1, we -1 from the currentPoint
      console.log(x)
      var currentPoint = Math.round(x)-1;
      thisPointIndex = currentPoint;
      chart.series[0].data[currentPoint].select(true);
    });

有什么想法吗?

例子: http://jsbin.com/ucebar

I have a jQuery UI draggable element. It's extremely simple. It's just a div (container) with another div inside (draggable piece) set to a grid. The problem is after I move the element one time I can't go back to the first point. If i change the grid size it works, but I need it to work on this grid as it's matching some element below it

Relevant JS:

 $('<div class="slider_wrap"><div class="slider"></div></div>').appendTo('#chart');

 $('#chart .slider')
    .draggable({
      containment:'parent',
      grid:[distanceBetweenPoints,0],
      opacity: 0.25
    })
    .bind('mousedown', function(e, ui){
      // bring target to front
      $(e.target.parentElement).append( e.target );
    })
    .bind('drag', function(e, ui){
      // update coordinates manually, since top/left style props don't work on SVG
      e.target.setAttribute('x', ui.position.left);
    })
    .bind('dragstop',function(e, ui){
      //a = true offset of slider piece
      var a = ui.position.left + distanceBetweenPoints;
      var b = containerWidth;
      var c = thePoints.length;
      var d = b / c;
      var x = a / d;
      //Since the points are in an array which starts at 0, not 1, we -1 from the currentPoint
      console.log(x)
      var currentPoint = Math.round(x)-1;
      thisPointIndex = currentPoint;
      chart.series[0].data[currentPoint].select(true);
    });

Any ideas?

Example:
http://jsbin.com/ucebar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽梦忆笙歌 2024-12-04 18:30:54

您使用的是分数网格大小,例如 39.7 像素。因此,每次拖动时,div 都会向左偏移一个像素。这意味着位置零很快变得不可用:

也就是说:在点 1,ui.position.left 将是 38 像素或更小。
由于向点 0 移动最小跳跃 (39.7px) 将使 div 超出边界矩形,因此不允许该移动。

而且,使用最接近的整数作为网格大小将很快导致网格和数据点之间不对齐。

解决这一切的一种方法是:

  1. 删除grid:[distanceBetweenPoints,0],参数。

  2. 相反,在拖动停止时捕捉div,如下所示:

    /*--- 捕捉到最近的网格。
    */
    var gridPos = Math.round ( 
                        Math.round(ui.position.left / distanceBetweenPoints) 
                        * 点之间的距离
                    );
    var delta = gridPos - ui.position.left;
    var newOffset = $(this).offset().left + delta;
    $(this).offset ( {left: newOffset} );
    

在 jsBin 上查看它的实际应用。

You're using a fractional grid size, for example 39.7 px. So, with each drag, the div gets offset a pixel to the left. This means that position zero quickly becomes unavailable:

That is: at point 1, ui.position.left will be 38 pixels or less.
Since moving the minimum jump (39.7px) -- towards point 0 -- will take the div outside the bounding rectangle, that move is not allowed.

And, using the nearest integer for grid size will quickly result in misalignment between the grid and the data points.

One way around all this is to:

  1. Delete the grid:[distanceBetweenPoints,0], parameter.

  2. Instead, snap the div upon drag stop, like so:

    /*--- Snap to nearest grid.
    */
    var gridPos     = Math.round ( 
                        Math.round (ui.position.left / distanceBetweenPoints) 
                        * distanceBetweenPoints
                    );
    var delta       = gridPos - ui.position.left;
    var newOffset   = $(this).offset ().left  +  delta;
    $(this).offset ( {left: newOffset} );
    

See it in action at jsBin.

如梦初醒的夏天 2024-12-04 18:30:54

我没有时间找出真正的解决方案,但我发现如果您拖放滑块,每次都会稍微向左移动一些。它无法回到第一名的原因是,在第一次下降之后,就没有足够的空间了。祝你好运!

I didnt have time to work out a real solution, but I found that if you drag and drop the slider moves slightly more to the left each time. The reason it can't go back into first place is that after the first drop, there is not enough room anymore. Good luck!

靖瑶 2024-12-04 18:30:54

我能够通过以下方法解决它:

.draggable({
  ...
  drag : function(event, ui) {
    ui.position.left = Math.round(ui.position.left / distance_between_points) * distance_between_points;
  }
});

I was able to solve it with the following:

.draggable({
  ...
  drag : function(event, ui) {
    ui.position.left = Math.round(ui.position.left / distance_between_points) * distance_between_points;
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文