【发布时间】:2020-02-25 11:53:56
【问题描述】:
我想将图像放入 div 中。但是,当 div 更改大小时,div 中包含的图像不会留在 div 内。以下是 div 具有“正常”大小时的图像:
以下是更改 div 大小时的图像:
当图像不适合 div 时,我希望 div 添加滚动条。 这是一些代码:
// Here is where the tag <img /> is created:
function getData(id) {
$.get(`/api/IgnicoesAPI/${id}`, function(data) {
$('#header').append('<h3 style="text-align:center;"><b>Avaliação da Ignição</b></h3>');
$('#myDiv').append('<p>Estado:' + data.estado + '</p>')
$('#myDiv').append('<p>ID: ' + id + '</p>');
$.each(data.listaOcorrencias, function(o, ocorrencia) {
//Adding images
$('#myDiv').append('<img src="imagensFogos/' + ocorrencia.fotografia + '" onclick="openModal();" class="hover-shadow" style="width:200px; height:190px;" hspace="4"/>');
});
//MORE CODE
});
}
/* The Modal (background) */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.4);
/* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
height: 80%;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.buttonsClass {
position: absolute;
bottom: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<div id="header" class="headerClass"></div>
//Here is where the images are stored
<div id="myDiv" class="added">
</div>
<div id="buttons" class="buttonsClass"></div>
</div>
</div>
【问题讨论】:
-
#myDiv { max-width : 100%; overflow-x: auto;}应该可以解决问题 -
它不起作用。我仍然有同样的问题
-
确实如此。我已经用 Flex 发布了一个有效的答案。
标签: javascript jquery html css