简体   繁体   English

Chrome忽略了动态更改元素的相对位置

[英]Chrome ignoring relative positioning for dynamically changed element

I want to display a loading image in the middle of a div when I click on it. 我想在div的中间显示加载图像。 But in Chrome the image appears in the top-left. 但是在Chrome中,图片显示在左上角。 It only happens for dynamic content. 它仅适用于动态内容。 The image is positioned correctly in Firefox. 图像在Firefox中的位置正确。

http://jsfiddle.net/uGC25/ http://jsfiddle.net/uGC25/

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style>
    .inner_text {
        position: relative;
        top:45%;
    }
    .loading_img {
        position: relative;
        top:30%;
    }
    .plan_div {
        border: solid 1px;
        cursor: pointer;
        width:420px;
        height:243px;
    }
</style>
<div class='plan_div'>
    <img class="loading_img" src="http://cdn.jsdelivr.net/wp-advanced-ajax-page-loader/2.5.12/loaders/Atom%20Loading.gif" />
</div>
<div class='plan_div'>
    <div class='inner_text'>Click to generate graph.</div>
</div>
<script>
    function start_loading() {
        $(this).html('<img class="loading_img" src="http://cdn.jsdelivr.net/wp-advanced-ajax-page-loader/2.5.12/loaders/Atom%20Loading.gif">');
    }
    $(document).on('click', '.plan_div', start_loading);
</script>

If position is absolute 如果位置是absolute

Use top, left, right and bottom 使用上,左,右和下

When position is relative 当位置是relative

Use margin-top, margin-left, margin-right and margin-bottom 使用上边距,左上边距,右上边距和下边距

.inner_text { position: relative; margin-top:25%; }
.loading_img { position: relative; margin-top:20%;  }

DEMO (Working Fine Both Browsers) 演示 (两种浏览器均可正常工作)

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

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