Javascript 按左右键连续移动箱子

Javascript 按左右键连续移动箱子,javascript,jquery,html,css,jquery-animate,Javascript,Jquery,Html,Css,Jquery Animate,尝试向左和向右移动。在按钮上弹出类,连续从左向右和从右向左移动 HTML: 对 左边 弹出窗口 您错过了.popup div的左属性 .popup { width: 200px; height: 50px; border: 1px dashed #000; position: absolute; top: 100px; left:0px; } 您可以按如下方式改进脚本: $(document).ready(function () {

尝试向左和向右移动
。在按钮上弹出
类,连续从左向右和从右向左移动

HTML:

对
左边
弹出窗口

您错过了.popup div的左属性

 .popup {
    width: 200px;
    height: 50px;
    border: 1px dashed #000;
    position: absolute;
    top: 100px;
    left:0px;
 }
您可以按如下方式改进脚本:

$(document).ready(function () {
    var left = parseInt($(".popup").css('left'));
    var refreshIntervalId;
    $(".left").on('mousedown', function () {
        refreshIntervalId = setInterval(function () {
            $('#counter').html(parseInt($('#counter').html()) + 1);
            left += 5;
            $(".popup").css({"left": left})
        }, 10);
    })
    $(".left").on('mouseup', function () {
        clearInterval(refreshIntervalId);
    })
    $(".right").on('mousedown', function () {
        refreshIntervalId = setInterval(function () {
            $('#counter').html(parseInt($('#counter').html()) - 1);
            left -= 5;
            $(".popup").css({"left": left});
        }, 10);
    })
    $(".right").on('mouseup', function () {
        clearInterval(refreshIntervalId);
    })
});

我不明白你到底想做什么,你能再解释一下吗?
$(document).ready(function () {
    var left = parseInt($(".popup").css('left'));
    var refreshIntervalId;
    $(".left").on('mousedown', function () {
        refreshIntervalId = setInterval(function () {
            $('#counter').html(parseInt($('#counter').html()) + 1);
            left += 5;
            $(".popup").css({"left": left})
        }, 10);
    })
    $(".left").on('mouseup', function () {
        clearInterval(refreshIntervalId);
    })
    $(".right").on('mousedown', function () {
        refreshIntervalId = setInterval(function () {
            $('#counter').html(parseInt($('#counter').html()) - 1);
            left -= 5;
            $(".popup").css({"left": left});
        }, 10);
    })
    $(".right").on('mouseup', function () {
        clearInterval(refreshIntervalId);
    })
});