简体   繁体   English

无法将div动画到所需位置两次

[英]Unable to animate a div to the required position twice

I have 8 divs which I can swap based on the values given in the respective drop down, As you can be seen in the image below: 我有8个div,可以根据相应下拉列表中的值进行交换,如下图所示:

在此处输入图片说明

However the animation is not working for the same div twice, as you can see in this jsfiddle . 但是,该动画不能在同一个div上工作两次,如您在此jsfiddle中所见 Say if I swap div 1 with div 4 and then I try swapping new div 1 with div 7, the div is animating bizzarely all over the page! 假设如果我将div 1与div 4交换,然后尝试将新的div 1与div 7交换,则div会在整个页面上动画化! Tried debugging it with firebug, all the values for animating were correct and the flow was also perfect. 尝试使用Firebug进行调试,所有动画值均正确且流程也很完美。 Still its causing this problem. 仍然是导致此问题的原因。 Don't know whether I am missing something about the animate() method!! 不知道我是否缺少有关animate()方法的东西! This is my HTML code : 这是我的HTML代码:

<table cellspacing="30">
  <tr>
    <td>
        <div id="img1" style="width:100px;height:40px;position:relative;">
            <select id="drop1" name="" class="drop">
                <option>1</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" disabled="disabled" checked="checked">
            <img src="" width="120" height="40" id="img_banner1">
        </div>
    </td>
    <td>
        <div id="img2" style="width:100px;height:40px;position:relative;">
            <select id="drop2" name="" class="drop">
                <option>2</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" disabled="disabled" checked="checked">
            <img src="" width="120" height="40" id="img_banner2">
        </div>
    </td>
    <td>
        <div id="img3" style="width:100px;height:40px;position:relative;">
            <select id="drop3" name="" class="drop">
                <option>3</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" disabled="disabled" checked="checked">
            <img src="" width="120" height="40" id="img_banner3">
        </div>
    </td>
    <td>
        <div id="img4" style="width:100px;height:40px;position:relative;">
            <select id="drop4" name="" class="drop">
                <option>4</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" disabled="disabled" checked="checked">
            <img src="" width="120" height="40" id="img_banner4">
        </div>
    </td>
  </tr>
  <tr>
    <td>
        <div id="img5" style="width:100px;height:40px;position:relative;">
            <select id="drop5" name="" class="drop">
                <option>5</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" disabled="disabled" checked="checked">
            <img src="" width="120" height="40" id="img_banner5">
        </div>
    </td>
    <td>
        <div id="img6" style="width:100px;height:40px;position:relative;">
            <select id="drop6" name="" class="drop">
                <option>6</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" disabled="disabled" checked="checked">
            <img src="" width="120" height="40" id="img_banner6">
        </div>
    </td>
    <td>
        <div id="img7" style="width:100px;height:40px;position:relative;">
            <select id="drop7" name="" class="drop">
                <option>7</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" disabled="disabled" checked="checked">
            <img src="" width="120" height="40" id="img_banner7">
        </div>
    </td>
    <td>
        <div id="img8" style="width:100px;height:40px;position:relative;">
            <select id="drop8" name="" class="drop">
                <option>8</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
            </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" disabled="disabled" checked="checked">
            <img src="" width="120" height="40" id="img_banner8">
        </div>
    </td>
  </tr>
</table>

Script: 脚本:

  $(".drop").change(function () {
  var flag = false;

  for (var i = 1; i <= 8; i++) {
      var dropdown = document.getElementById('drop' + i);

      if (flag == true && source.value != i) {
          dropdown.value = i;
      }
      if (dropdown.value != 0 && dropdown.value != i && flag == false) {
          var a = i;
          var b = dropdown.value;
          var pos = i;
          var source_div = document.getElementById('img' + i);
          var source = dropdown;
          var destination = document.getElementById('drop' + dropdown.value);
          var destination_div = document.getElementById('img' + dropdown.value);
          var posDrop = Math.ceil(dropdown.value / 4);
          var posI = Math.ceil(i / 4);

          var modDrop = (dropdown.value % 4);
          var modI = (i % 4);
          if (modDrop == 0) modDrop = 4;
          if (modI == 0) modI = 4;
          if (modDrop > modI) {
              // move right
              var diff = modDrop - modI;
              $("#img" + i).animate({
                  left: +((diff) * 130) + "px"
              });
              $("#img" + dropdown.value).animate({
                  right: +((diff) * 130) + "px"
              });
          } else if (modDrop < modI) {
              // move left
              var diff = modI - modDrop;
              $("#img" + i).animate({
                  right: +((diff) * 130) + "px"
              });
              $("#img" + dropdown.value).animate({
                  left: +((diff) * 130) + "px"
              });
          }

          if (posDrop > posI) {
              //move down
              $("#img" + i).animate({
                  top: +(70) + "px"
              });
              $("#img" + dropdown.value).animate({
                  bottom: +(70) + "px"
              });
          } else if (posDrop < posI) {
              // move up
              $("#img" + i).animate({
                  bottom: +(70) + "px"
              });
              $("#img" + dropdown.value).animate({
                  top: +(70) + "px"
              });
          }
          flag = true;

      }
  }
  destination.value = pos;

  var temp_div = source_div.id;
  source_div.id = destination_div.id;
  destination_div.id = temp_div;

  var temp = source.id;
  source.id = destination.id;
  destination.id = temp;
});

Can I suggest that you use a wholly different approach? 我可以建议您使用完全不同的方法吗? Rather then animating the divs directly, use an array to store information about which div is where and then re-render this part of the page every time a re-ordering has been made. 而不是直接为div设置动画,而是使用数组存储有关哪个div位于何处的信息,然后在每次进行重新排序时都重新渲染页面的这一部分。

so say you've got an array like this: 所以说你有一个像这样的数组:

[0, 1, 2, 3, 4, 5, 6, 7] [0、1、2、3、4、5、6、7]

then pushing value 'x' on div 'y' leads to storing array[y-1] in z, setting array[y-1] to x-1, then setting array[x-1] to z. 然后将值'x'推入div'y'导致将array [y-1]存储在z中,将array [y-1]设置为x-1,然后将array [x-1]设置为z。

Now you've switched them in your array in a stable manner. 现在,您已经以稳定的方式在阵列中切换了它们。

Then re-render the table. 然后重新渲染表。

You could also add the array values to the divs with an data-arVal attribute if you like. 如果愿意,还可以使用data-arVal属性将数组值添加到div中。 This could be the basis of some nice animations. 这可能是一些不错的动画的基础。

Loop through the array like this 这样遍历数组

jQuery.each(array, function(i, arVal){
     //and set the values of the data attribute
     $('td:nth-child(' + i + ') div)'.attr('data-arVal', '' + arVal + '' );
});

data attributes are your friend. 数据属性是您的朋友。 And jQuery each loops are way more handy and powerfull then for loops, I highly recommend you use the jQuery.each loop. 而且jQuery每个循环比循环更方便和强大,我强烈建议您使用jQuery.each循环。

ps could be some typos here, didn't test what I wrote. ps可能是一些错字,没有测试我写的内容。

I created http://jsfiddle.net/qa03f7g1/27/ . 我创建了http://jsfiddle.net/qa03f7g1/27/ It may not be perfect but the code is simpler. 它可能并不完美,但是代码更简单。 I also removed the div 's from the table and positioned them absolutely. 我还从表中删除了div并对其进行了绝对定位。

$(".drop").change(function () {

    var $thisElem = $(this).parent();

    var swapIndex = $(this).children(":selected").val();
    var $swapElem = $("#img" + swapIndex);

    var thisPosition = $thisElem.position();
    var swapPosition = $swapElem.position();

    $thisElem.finish()
        .animate({left: swapPosition.left})
        .animate({top: swapPosition.top});

    $swapElem.finish()
        .animate({left: thisPosition.left})
        .animate({top: thisPosition.top});

    $swapElem.attr("id", $thisElem.attr("id"));
    $thisElem.attr("id", "img" + swapIndex);

});

Some problems: 一些问题:

  • If on the same left position, there will be a delay as it animates each item to the same left position. 如果在同一left位置,则将每个项目设置为同一left位置时会有延迟。 This can be fixed with an if statement. 可以使用if语句解决此问题。
  • You may want to set each option to it's corresponding location. 您可能需要将每个选项设置为其对应的位置。 This is easily done, comment if you want it done. 这很容易完成,如果您想完成,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM