简体   繁体   English

应用CSS缩放转换将新元素放置在父级中包含的子级之上

[英]Position new elements on top of children contained in parent with CSS scale transform applied

After applying the CSS scale transform to a parent container, we cannot use absolute positioning to place new elements on top of its children. 将CSS scale转换应用于父容器后,我们不能使用绝对定位将新元素放置在其子容器的顶部。

HTML: HTML:

<div class='grid'>
    <div class='row'>
        <div class='cell'></div>
        <div class='cell'></div>
        <div class='cell'></div>
        <div class='cell'></div>
        <div class='cell'></div>
        <div class='cell'></div>
    </div>
</div>

CSS: CSS:

div[data-role='page'] { -webkit-transform:scale(2); -webkit-transform-origin:top; height:50% }

JavaScript: JavaScript:

var x = $( '.cell:first' ).offset().left;
var y = $( '.cell:first' ).offset().top;
var new_tile = $( '#templates .tile' ).clone();
new_tile.offset( {left: x, top: y} );
new_tile.addClass( 'show' );
$( '#play_page .all_tiles' ).append( new_tile );

Reproduce (use Chrome): 复制(使用Chrome):

  1. Visit http://panabee-games.herokuapp.com/stroll/stroll 造访http://panabee-games.herokuapp.com/stroll/stroll

  2. Inside of Developer Tools, add the CSS rules below to the #play_page element: -webkit-transform:scale(2); -webkit-transform-origin:top; height:50% 在开发人员工具内部,将以下CSS规则添加到#play_page元素中: -webkit-transform:scale(2); -webkit-transform-origin:top; height:50% -webkit-transform:scale(2); -webkit-transform-origin:top; height:50%

  3. The JS above does not position the new element above the first cell class as expected. 上面的JS不会按预期将新元素放置在第一个cell类之上。

How do we need to adjust the position of the elements to account for the application of the scale transform? 我们如何调整元素的位置以适应scale变换的应用?

Basically You want to add those Green Balls above the Gey one's...I am Right? 基本上,您想在Gey一个人的上方添加这些绿色球……我是对的吗?

If this is want you want to do then you should the append the Green balls( new elements ) along the with other Grey balls ( old elements ) and both of them contained inside a parent div like this: 如果要这样做,则应该将Green球(新元素)与其他Gray球(旧元素)一起附加,并且它们都包含在父div中,如下所示:

<div class='grid'>
    <div class='row'>

        <div class="cellwrap">
             <div class='cell'></div>

              <!-- Green ball appended dynamically -->
              <div class="tile show type_0" type="type_0" style="top: 540px; left: 586.28125px;">
             </div>
        </div>


        <div class='cell'></div>
        <div class='cell'></div>
        <div class='cell'></div>
        <div class='cell'></div>
        <div class='cell'></div>
    </div>
</div>

And after this you can properly change the z-index to highlight whichever element you want to show. 之后,您可以适当地更改z-index以突出显示要显示的任何元素。

Conclusion: instead of figuring out a way to change the positioning of dynamically added elements so that it fits well along with some other scaled element you should make that dynamically added element part of the Div which is going to be scaled. 结论:与其找出一种方法来改变动态添加的元素的位置,使其与其他比例缩放的元素很好地匹配,不如将动态添加的元素作为将要缩放的Div的一部分。 I that case you wont have to worry about positioning. 在那种情况下,您不必担心定位。

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

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