jQuery Slide 停止在左侧位置

发布于 2024-11-05 05:20:14 字数 1665 浏览 0 评论 0原文

我通过设置 div 的 css 左侧位置来将 div 定位在 mousemove 事件上,可以在以下位置找到演示:

http://jsfiddle.net/JLtT3/53/

我遇到的问题是设置左右边界以使其停止。

无论我做什么,它似乎都会超出我设置的边界(左和右),所以向左移动 -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:

http://jsfiddle.net/JLtT3/53/

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 技术交流群。

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

发布评论

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

评论(2

装纯掩盖桑 2024-11-12 05:20:14

为什么要重新发明轮子? jQuery UI 具有适当的可拖动实现,没有您需要自己处理的令人讨厌的错误,例如鼠标离开窗口时拖动被卡住、意外的文本选择阻止下一次拖动等。您甚至可以构建自定义 jQuery UI如果您不需要整个内容来保持代码库更短,请在网站上文件。看看这个小提琴: http://jsfiddle.net/JLtT3/54/

使用 jQuery UI 实现:

$(document).ready(function() {
    $(".jspDrag").draggable({
        containment: '.jspHorizontalBar',
        axis: 'x',
        drag: function(event,ui){
            //do your stuff, ui.position.left is the current position
        }
    });
});

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:

$(document).ready(function() {
    $(".jspDrag").draggable({
        containment: '.jspHorizontalBar',
        axis: 'x',
        drag: function(event,ui){
            //do your stuff, ui.position.left is the current position
        }
    });
});
绳情 2024-11-12 05:20:14

这应该是:

if ((sbarLeft >= endLeftPosition) && (moveXposition < endRightPosition)) 

真的是这样吗

if ((moveXposition>= endLeftPosition) && (moveXposition < endRightPosition)) {

您似乎正在检查当前位置(sbarLeft)是否超过左边界,而不是新位置(moveXposition)。

Should this:

if ((sbarLeft >= endLeftPosition) && (moveXposition < endRightPosition)) 

really be this:

if ((moveXposition>= endLeftPosition) && (moveXposition < endRightPosition)) {

?

You seem to be checking that the current position (sbarLeft) is not past the left boundary instead of the new position (moveXposition).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文