简体   繁体   English

每个功能均无法正常工作

[英]each function not working as expected

So what I try to do is the following: 因此,我尝试执行以下操作:

I got some items in 3 columns and default positioning ( position:static ). 我在3列中得到了一些项目,并使用默认位置( position:static )。
And I'm trying to transition them to top:0; left:0 我正在尝试将它们转换top:0; left:0 top:0; left:0 of the page. 页面的top:0; left:0


Transitions between different positions ( absolute and static ) are not possible in CSS3, so I tried a jQuery workaround: 在CSS3中不可能在不同位置( absolute位置和static位置)之间进行转换,因此我尝试了一种jQuery解决方法:

$(".item").each(function(){
  var items = $(this);
  $(this).css({
      'width': items.width(),
      'height': items.height(),
      'position': 'absolute',
      'top': items.offset().top,
      'left': items.offset().left
  });
});

What this is supposed to do, is to take every item with class item and "duplicate" it at the exisiting position with position:absolute 这应该做的是将每个项目与类item并在以下位置将其“复制”到position:absolute
This is working perfectly for one item. 这非常适合一件商品。 But as soon as I got multiple items, it simply stacks them at the position of the first item... 但是一旦我得到多个项目,它就将它们堆叠在第一个项目的位置...


Here's a fiddle: 这是一个小提琴:

 $(".item").each(function(){ var items = $(this); $(this).css({ 'width': items.width(), 'height': items.height(), 'position': 'absolute', 'top': items.offset().top, 'left': items.offset().left }); }); 
 #items{ -webkit-column-count: 3; -webkit-column-gap: 10px; -moz-column-count: 3; -moz-column-gap: 10px; column-count: 3; column-gap: 10px; } .item{ width:100%; height:20px; text-align:center; overflow:hidden; margin:0 0 10px 0; border:1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="items"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">4</div> </div> 

My desired result is that jQuery takes every item, gets its position on the page, and replaces the CSS, so it's at the same position as before, just with position:absolute , so I can transition it to top:0;left:0; 我希望得到的结果是jQuery接受每个项目,获取其在页面上的位置,并替换CSS,因此它与以前的位置相同,只是position:absolute ,因此我可以将其转换为top:0;left:0; afterwards. 然后。

It will always the same position if you use absolute ! 如果使用absolute ,它将始终处于同一位置! Because you loop each item then apply it immediately so first div is changed to absolute and take it old position with top,left value .Then second div is not still change as absolute so it will also take first div position and duplicate with first div .This will continuing happen till loop end . 因为您循环了每个项目然后立即应用它,所以第一个div更改为absolute并使用top,left值将其置于旧位置。然后,第二个div仍未更改为绝对值,因此它也将使用first div位置并与first div复制。这将持续发生直到循环结束。 So it all will have the same top,left value . 因此,它们将具有相同的top,left值。

So my solution is not to apply css property immediately , store value in one helper object and applied that all property after finished loop . 因此,我的解决方案不是立即应用css属性,将值存储在一个辅助对象中,并在完成循环后应用所有属性。

 var beforeApply = {} $(".item").each(function(){ var items = $(this); beforeApply[$(this).index()] = { 'width': items.width(), 'height': items.height(), 'position':'absolute', 'top': items.offset().top, 'left': items.offset().left, 'text':items.text() }; }); for(var j in beforeApply) { var index = parseInt(j) + 1; var text = beforeApply[j].text; $("#items div:nth-child("+index+")").css(beforeApply[j]).text(text + "Changed position"); } 
 #items{ -webkit-column-count: 3; -webkit-column-gap: 10px; -moz-column-count: 3; -moz-column-gap: 10px; column-count: 3; column-gap: 10px; } .item{ width:100%; height:20px; text-align:center; overflow:hidden; margin:0 0 10px 0; border:1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="items"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">4</div> </div> 

This "Transitions between different positions (absolute and static) are not possible in CSS3, so I tried a jQuery workaround:" can be worked around by having static by default, and then appending a class that has absolute. 此“在CSS3中不可能在不同位置(绝对位置和静态位置)之间进行转换,因此我尝试了jQuery解决方法:”可以通过以下方法解决:默认情况下具有静态位置,然后附加一个具有绝对位置的类。 Then the CSS attribute "transition" will work. 然后CSS属性“ transition”将起作用。

fiddle here https://jsfiddle.net/nikoimp/vL9bahra/ 在这里摆弄https://jsfiddle.net/nikoimp/vL9bahra/


  
 
  
  
$("#1").addClass('1');
$("#2").addClass('2');
$("#3").addClass('3');
$("#4").addClass('4');
$("#5").addClass('5');
    #items{
        -webkit-column-count: 3;
        -webkit-column-gap: 10px;
        -moz-column-count: 3;
        -moz-column-gap: 10px;
        column-count: 3;
        column-gap: 10px;
    }
    .item{
        -webkit-transition: width 2s;
        transition: width 2s;
        width:100%;
        height:20px;
        text-align:center;
        overflow:hidden;
        margin:0 0 10px 0;
        border:1px solid black;
    }
    .1 {
        position: absolute;
        background: red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <div id="items">
      <div class="item" id="1">1</div>
      <div class="item" id="2">2</div>
      <div class="item" id="3">3</div>
      <div class="item" id="4">4</div>
   <div class="item" id="5">4</div>
    </div>

The best solution I was able to find is to append the new content in another hidden div, then remove the old div, and show the new one. 我能够找到的最佳解决方案是将新内容附加到另一个隐藏的div中,然后删除旧的div,然后显示新的div。

You was facing that problem, because while updating the divs to position absolute, the other divs styling was also being updated at the same time because they are floating. 您遇到了这个问题,因为在将divs更新为绝对位置时,其他divs样式也同时在更新,因为它们是浮动的。

 $(function() { $(".item").each(function(){ var items = $(this); var _text = items.text() ; var _css = "width:" + items.width() + "px" + "; height:" + items.height() + "px" + "; position:absolute" + "; top:" + items.offset().top + "px" + "; left:" + items.offset().left + "px" + ";" var _style = "style='" + _css + "'" ; var _div = "<div class='item' " + _style + ">" + _text + "</div>" ; $("#items_clone").append(_div) ; }); $("#items").remove() ; $("#items_clone").show().attr("id", "items") ; }) ; 
 #items{ -webkit-column-count: 3; -webkit-column-gap: 10px; -moz-column-count: 3; -moz-column-gap: 10px; column-count: 3; column-gap: 10px; } #items_clone{ display:none; } .item{ width:100%; height:20px; text-align:center; overflow:hidden; margin:0 0 10px 0; border:1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="items"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> </div> <div id="items_clone"> </div> 

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

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