gpt4 book ai didi

javascript - 如何自动校正此灯箱中的图像尺寸?

转载 作者:行者123 更新时间:2023-11-28 04:39:44 27 4
gpt4 key购买 nike

我正在尝试制作 this lightbox在我的网站上工作,但图像没有以其原始尺寸显示,as you can see here (点击“Galeria de fotos”)。我该如何编写以下代码来纠正此问题?

function openModal() {
document.getElementById('myModal').style.display = "block";
}

function closeModal() {
document.getElementById('myModal').style.display = "none";
}

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
showSlides(slideIndex += n);
}

function currentSlide(n) {
showSlides(slideIndex = n);
}

function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
var captionText = document.getElementById("caption");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
captionText.innerHTML = dots[slideIndex-1].alt;
}
.row > .column {
padding: 0 8px;
}

.row:after {
content: "";
display: table;
clear: both;
}

.column {
float: left;
width: 33.3%;
}

/* The Modal (background) */
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: black;
}

/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
width: 90%;
max-width: 1200px;
}
<div role="tabpanel" class="tab-pane" id="messages">
<div class="trip-tab-gallery">
<ul>
<div class="column">
<li><img src="images/tab-gallery-img-1.jpg" onclick="openModalhttps://s30.postimg.org/rxspwis5p/tab_gallery_img_1.jpgover-shadow">
</li>
</div>
<div class="column">
<li><img src="]https://s30.postimg.org/5n4uwjuvh/tab_gallery_img_2.jpg" onclick="openModal();currentSlide(2)" class="hover-shadow">
</li>
</div>
<div class="column">
<li><img src="https://s30.postimg.org/h0vzlhurx/tab_gallery_img_3.jpg" onclick="openModal();currentSlide(3)" class="hover-shadow">
</li>
</div>
<div class="column">
<li><img src="https://s30.postimg.org/eku67ncp9/tab_gallery_img_4.jpg" onclick="openModal();currentSlide(4)" class="hover-shadow">
</li>
</div>
</ul>

<div id="myModal" class="modal">
<span class="close cursor" onclick="closeModal()">&times;</span>
<div class="modal-content">

<div class="mySlides">
<div class="numbertext">1 / 4</div>
<img src="https://s30.postimg.org/rxspwis5p/tab_gallery_img_1.jpg" style="width:100%">
</div>

<div class="mySlides">
<div class="numbertext">2 / 4</div>
<img src="]https://s30.postimg.org/5n4uwjuvh/tab_gallery_img_2.jpg" style="width:100%">
</div>

<div class="mySlides">
<div class="numbertext">3 / 4</div>
<img src="https://s30.postimg.org/h0vzlhurx/tab_gallery_img_3.jpg" style="width:100%">
</div>

<div class="mySlides">
<div class="numbertext">4 / 4</div>
<img src="https://s30.postimg.org/eku67ncp9/tab_gallery_img_4.jpg" style="width:100%">
</div>

<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a>

<div class="caption-container">
<p id="caption"></p>
</div>

<div class="column">
<img class="demo" src="https://s30.postimg.org/rxspwis5p/tab_gallery_img_1.jpg" onclick="currentSlide(1)" alt="Nature">
</div>

<div class="column">
<img class="demo" src="]https://s30.postimg.org/5n4uwjuvh/tab_gallery_img_2.jpg" onclick="currentSlide(2)" alt="Trolltunga">
</div>

<div class="column">
<img class="demo" src="https://s30.postimg.org/h0vzlhurx/tab_gallery_img_3.jpg" onclick="currentSlide(3)" alt="Mountains">
</div>

<div class="column">
<img class="demo" src="https://s30.postimg.org/eku67ncp9/tab_gallery_img_4.jpg" onclick="currentSlide(4)" alt="Lights">
</div>
</div>
</div>
</div>
</div>

最佳答案

我看到在所有图像上,宽度都是 100%:

<img src="https://s30.postimg.org/5n4uwjuvh/tab_gallery_img_2.jpg" style="width:100%">

将宽度设置为 100% 会强制图像扩展到容器的大小。将其更改为 ma​​x-width 应该可以解决该问题,同时确保图像不会超出容器。

<img src="https://s30.postimg.org/5n4uwjuvh/tab_gallery_img_2.jpg" style="max-width:100%">

之后,您可以考虑将图像居中并使其在较小时看起来不错。

旁注:我注意到一些图片网址上有一个 [,我猜这是一个拼写错误?

更新:要使图像与中心对齐并“移除”背景:

添加以下 CSS:

.mySlides{
background: black;
text-align: center;
}

这会将背景设置为黑色,这样看起来就不会很差,并将图片居中对齐。

关于javascript - 如何自动校正此灯箱中的图像尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41273539/

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