jQuery Slide 停止在左侧位置
我通过设置 div 的 css 左侧位置来将 div 定位在 mousemove 事件上,可以在以下位置找到演示:
我遇到的问题是设置左右边界以使其停止。
无论我做什么,它似乎都会超出我设置的边界(左和右),所以向左移动 -13px。
如果我将左侧位置记录到 firebug 中,它会变成负数,尽管我
有if(sbar < 0)
的代码中
。 jQuery(工作示例包括上述 url 中的 html):
$(document).ready(function() {
var statusbar = $(".jspDrag");
statusbar.mousedown(function(event) {
handleMouseDown(event, this);
});
$("body").mouseup(function(event) {
handleMouseUp(event, this);
});
});
function handleMouseDown(e, sbar) {
var sbarPosition = $(sbar).position();
var sbarPosition = e.pageX - sbarPosition.left;
var endRightPosition = $('.jspCapRight').position();
var endRightPosition = endRightPosition.left - $(sbar).width();
var endLeftPosition = $('.jspCapLeft').position();
var endLeftPosition = endLeftPosition.left;
if (e.button == 0)
{
$("body").bind('mousemove', function(event) {
handleMouseMove(event, $(sbar), sbarPosition, endLeftPosition, endRightPosition);
});
}
}
function handleMouseUp(e, sbar) {
$("body").unbind('mousemove');
}
function handleMouseMove(e, sbar, sbarMousePosition, endLeftPosition, endRightPosition) {
var moveXposition = e.pageX - sbarMousePosition;
var sbarLeft = $(sbar).position();
var sbarLeft = sbarLeft.left;
console.log(sbarLeft); //firebug
if ((sbarLeft >= endLeftPosition) && (moveXposition < endRightPosition))
{
$(sbar).css('left', moveXposition);
}
}
I'm positioning a div on a mousemove event by setting it's css left position, a demo of which can be found at:
What I'm having trouble with is setting the left and right boundaries for it to stop.
No matter what I do it seems to go over the boundaries (left and right) that I set, so goes -13px left.
If I log the left position into firebug it goes into minus figures despite the fact I have:
if(sbar < 0)
in my code.
The jQuery (working example including html at the above url):
$(document).ready(function() {
var statusbar = $(".jspDrag");
statusbar.mousedown(function(event) {
handleMouseDown(event, this);
});
$("body").mouseup(function(event) {
handleMouseUp(event, this);
});
});
function handleMouseDown(e, sbar) {
var sbarPosition = $(sbar).position();
var sbarPosition = e.pageX - sbarPosition.left;
var endRightPosition = $('.jspCapRight').position();
var endRightPosition = endRightPosition.left - $(sbar).width();
var endLeftPosition = $('.jspCapLeft').position();
var endLeftPosition = endLeftPosition.left;
if (e.button == 0)
{
$("body").bind('mousemove', function(event) {
handleMouseMove(event, $(sbar), sbarPosition, endLeftPosition, endRightPosition);
});
}
}
function handleMouseUp(e, sbar) {
$("body").unbind('mousemove');
}
function handleMouseMove(e, sbar, sbarMousePosition, endLeftPosition, endRightPosition) {
var moveXposition = e.pageX - sbarMousePosition;
var sbarLeft = $(sbar).position();
var sbarLeft = sbarLeft.left;
console.log(sbarLeft); //firebug
if ((sbarLeft >= endLeftPosition) && (moveXposition < endRightPosition))
{
$(sbar).css('left', moveXposition);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要重新发明轮子? jQuery UI 具有适当的可拖动实现,没有您需要自己处理的令人讨厌的错误,例如鼠标离开窗口时拖动被卡住、意外的文本选择阻止下一次拖动等。您甚至可以构建自定义 jQuery UI如果您不需要整个内容来保持代码库更短,请在网站上文件。看看这个小提琴: http://jsfiddle.net/JLtT3/54/
使用 jQuery UI 实现:
Why reinvent the wheel? jQuery UI has a proper draggable implementation, without the nasty bugs that you'll need to deal with yourself, like stuck dragging when the mouse leaves the window, accidental text selection preventing the next drag, etc. You can even build a custom jQuery UI file on the website if you don't need the whole thing to keep the codebase shorter. Check out this fiddle: http://jsfiddle.net/JLtT3/54/
Implementation with jQuery UI:
这应该是:
真的是这样吗
?
您似乎正在检查当前位置(sbarLeft)是否超过左边界,而不是新位置(moveXposition)。
Should this:
really be this:
?
You seem to be checking that the current position (sbarLeft) is not past the left boundary instead of the new position (moveXposition).