jQuery UI 可拖动不会返回到第一个点
我有一个 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);
});
有什么想法吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用的是分数网格大小,例如 39.7 像素。因此,每次拖动时,div 都会向左偏移一个像素。这意味着位置零很快变得不可用:
也就是说:在点 1,
ui.position.left
将是 38 像素或更小。由于向点 0 移动最小跳跃 (39.7px) 将使 div 超出边界矩形,因此不允许该移动。
而且,使用最接近的整数作为网格大小将很快导致网格和数据点之间不对齐。
解决这一切的一种方法是:
删除
grid:[distanceBetweenPoints,0],
参数。相反,在拖动
停止
时捕捉div,如下所示:在 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:
Delete the
grid:[distanceBetweenPoints,0],
parameter.Instead, snap the div upon drag
stop
, like so:See it in action at jsBin.
我没有时间找出真正的解决方案,但我发现如果您拖放滑块,每次都会稍微向左移动一些。它无法回到第一名的原因是,在第一次下降之后,就没有足够的空间了。祝你好运!
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!
我能够通过以下方法解决它:
I was able to solve it with the following: