简体   繁体   English

Javascript将图像添加到div而不更改div的高度和宽度?

[英]Javascript add image to div without changing div height and width?

   <div id="storage-bin-title">
    <div id="storage-bin">
        <div style="z-index:1; width:200px; background-color:black;" id="abc">
            <img src="http://2aek.com/inventory/test/1.png" />
            <img src="http://2aek.com/inventory/test/edit-button.png" id="edit" style="display:none" />
            <img src="http://2aek.com/inventory/test/delete-button.png" id="add" style="display:none;" />
        </div>
    </div>
</div>

<script type="text/javascript">
$(document).ready(function () {
    $("#abc").mouseover(function () {
        $("#edit").css({ display: 'inline', position:'relative', left:15, top:-55  });
        $("#add").css({ display: 'inline', position:'relative', left:15, top:-55  });

    });


    $("#abc").mouseout(function () {
        $("#edit").css({ display: 'none'});
        $("#add").css({ display: 'none' });
    });
});
</script>

Demo at : http://jsfiddle.net/zac1987/nru11uuo/2/ 演示位于: http : //jsfiddle.net/zac1987/nru11uuo/2/

Problem : When mouseover the div, javascript will add another 2 image into the div, so the div width and heigh expended. 问题:将鼠标悬停在div上时,javascript会将另外2张图片添加到div中,因此div的宽度和高度会增加。 How to use css to make the div width and heigh remain the same when the two images are added into it by javascript? 当通过JavaScript将两个图像添加到CSS中时,如何使用CSS使div的宽度和高度保持不变?

Don't use Javascript for CSS functionality. 不要将Javascript用于CSS功能。 CSS solution would be better here. CSS解决方案在这里会更好。 You need to position edit/add buttons absolutely relatively to parent container, just adjust bottom and left positions. 您需要绝对相对于父容器放置编辑/添加按钮,只需调整底部和左侧位置即可。

 #abc {position: relative;} #abc .btn { display: none; position: absolute; bottom: 10px; } #abc #add {left: 30px;} #abc:hover .btn { display: inline-block; } 
 <div id="storage-bin-title"> <div id="storage-bin"> <div style="z-index:1; width:200px; background-color:black;" id="abc"> <img src="http://2aek.com/inventory/test/1.png" /> <img src="http://2aek.com/inventory/test/edit-button.png" class="btn" id="edit" /> <img src="http://2aek.com/inventory/test/delete-button.png" class="btn" id="add" /> </div> </div> </div> 

If the <div> has the CSS property overflow:hidden; 如果<div>具有CSS属性overflow:hidden; , adding an image shouldn't change the size of the div. ,添加图片不应更改div的大小。

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

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