gpt4 book ai didi

javascript - 设置图像高度=宽度(不是div)

转载 作者:行者123 更新时间:2023-11-28 10:48:34 24 4
gpt4 key购买 nike

这是我现在使用的代码:

(HTML)

<html>
<head>
<title>Float Image Gallery</title>
</head>
<body>
<button onclick="showAllGalleries();">Show gallery</button>
<div id="myGallery" class="gallery">
<div class="gallery-close">
<a href=#><img class="gallery-close-button" src="http://bit.do/closeicon" onclick="hideAllGalleries();" /></a>
</div>
<div class="gallery-content">
<img class="gallery-content-image" src="" alt="Please add image here" />
<img class="gallery-content-image" src="" alt="Please add image here" />
<img class="gallery-content-image" src="" alt="Please add image here" />
<img class="gallery-content-image" src="" alt="Please add image here" /><br>
<img class="gallery-content-image" src="" alt="Please add image here" />
<img class="gallery-content-image" src="" alt="Please add image here" />
<img class="gallery-content-image" src="" alt="Please add image here" />
<img class="gallery-content-image" src="" alt="Please add image here" /><br>
<img class="gallery-content-image" src="" alt="Please add image here" />
<img class="gallery-content-image" src="" alt="Please add image here" />
<img class="gallery-content-image" src="" alt="Please add image here" />
<img class="gallery-content-image" src="" alt="Please add image here" /><br>
</div>
</div>
</body>
</html>

(CSS)

.gallery {
display:none;
width:100%;
height:100%;
left:0;
bottom:0;
top:auto;
right:auto;
position:fixed;
background-color:#cccccc;
opacity:50%;
}

.gallery-close {
width:auto;
height:auto;
margin-top:10px;
margin-left:10px;
}

.gallery-close-button {
width:30px;
height:30px;
}

.gallery-content {
width:100%;
height:auto;
text-align:center;
}

.gallery-content-image {
width:20%;
height:20%;
}

.gallery-content-image:hover {
background-color:#ffffff;
}

(JS)

function showAllGalleries(){
var adsArray = document.getElementsByClassName("gallery");
for (var i = 0; i<adsArray.length; i++){
adsArray[i].style.display='inline';
}
}

function hideAllGalleries(){
var adsArray = document.getElementsByClassName("gallery");
for (var i = 0; i<adsArray.length; i++){
adsArray[i].style.display='none';
}
}

问题出在 css(此处):

.gallery-content-image {
width:20%;
height:20%;
}

如果不编辑其他部分,我可以让高度和宽度一样吗?
有没有不使用 jQuery 的解决方案? (JavaScript 没问题)
任何帮助将不胜感激。

最佳答案

百分比单位是相对于你的浏览器窗口的,所以图片宽度是浏览器窗口宽度的20%,图片高度是浏览器窗口高度的20%。

因此,要使图像成为正方形,您应该将以像素(或其他单位)为单位的值指定为相同。示例:宽度:100px;高度:100px;

编辑

保留图片宽度的css,所以width: 20%;

这是js代码:

window.onresize = function() {
resize();
};

window.onload = function(){
resize();
};

function resize(){
var imgs = document.getElementsByTagName('img');
for(i = 0; i< imgs.length; i++){
document.getElementsByTagName('img')[i].style.height = document.getElementsByTagName('img')[i].clientWidth;
}
}

关于javascript - 设置图像高度=宽度(不是div),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22876266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com