How can I use jQuery to move a div across the screen

前端 未结 5 1324
旧时难觅i
旧时难觅i 2020-12-30 23:50

I need to make multiple divs move from right to left across the screen and stop when it gets to the edge. I have been playing with jQuery lately, and it seem like what I wa

5条回答
  •  耶瑟儿~
    2020-12-31 00:15

    Here i have done complete bins for above query. below is demo link, i think it may help you

    Demo: http://codebins.com/bin/4ldqp9b/1

    HTML:


    CSS:

    body{
      background:#ffffef;
    }
    #edge{
      width:500px;
      height:200px;
      border:1px solid #3377af;
      padding:5px;
    }
    
    .box{
      position:absolute;
      left:10;
      width:40px;
      height:40px;
      border:1px solid #a82244;
    }
    

    JQuery:

    $(function() {
        $("#btnAnimate").click(function() {
            var move = "";
            if ($(".box:eq(0)").css('left') == "10px") {
                move = "+=" + ($("#edge").width() - 35);
            } else {
                move = "-=" + ($("#edge").width() - 35);
            }
            $(".box").animate({
                left: move
            }, 500, function() {
                if ($(".box:eq(0)").css('left') == "475px") {
                    $(this).css('background', '#afa799');
                } else {
                    $(".box:eq(0)").css('background', '#f8a2a4');
                    $(".box:eq(1)").css('background', '#a2f8a4');
                    $(".box:eq(2)").css('background', '#5599fd');
                }
    
            });
        });
    });
    

    Demo: http://codebins.com/bin/4ldqp9b/1

提交回复
热议问题