gpt4 book ai didi

javascript - 基于单选选择的图像显示和模态弹出

转载 作者:行者123 更新时间:2023-11-28 02:51:30 25 4
gpt4 key购买 nike

我有一个脚本,我可以根据单选按钮选择循环浏览各种图像。这很有效,并且可以毫无问题地循环通过它们。

我的问题是我想为所有图像弹出模式。这些图像名称根据显示的元素而变化,因此我将图像名称保存在一个数组中(n 大小的数组。

这是 radio 代码:

<ul>
<li class="radio-attribute">
<label for="2" class="radio-attribute">
<input type="radio" id="2" name="id[27]" value="175" onClick="change_image(this.id)">
<img src="images/blue_swatch.png" alt="GBM0911B Blue" title=" GBM0911B Blue " width="45" height="40" />
</label>
</li>

<li class="radio-attribute">
<label for="3" class="radio-attribute">
<input type="radio" id="3" name="id[27]" value="173" onClick="change_image(this.id)">
<img src="images/black_swatch.png" alt="GBM0911BK Black" title=" GBM0911BK Black " width="41" height="40" />
</label>
</li>

</ul>

这些按钮工作正常。

javascript如下:

var images = ["GBM0911PL.png","GBM0911R.png"];
var modal = document.getElementById('myModal');
var img = document.getElementById('myImg');
var modalImg = document.getElementById("piGal");
var captionText = document.getElementById("caption");

function change_image(radioID) {
document.getElementById("piGal").innerHTML = "<img id=\"myImg\" src=\"images/"+ images[radioID] +"\" />";
}

// Get the modal

img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}

模态标签如下:

<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close" onclick="document.getElementById('piGal').style.display='none'">&times;</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="piGal">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>

我这样显示图像:

    <div id="piGal" style="float: left;">

<a href="images/GLAMOURFRONT.jpg"><img src="images/GLAMOURFRONT.jpg" alt="Glamour Bubble " title=" Glamour " width="225" height="184" id="myImg" /></a>
</div>

最后是用于模态的 css:

 /* Style the Image Used to Trigger the Modal */
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

/* 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.9); /* Black w/ opacity */
}

/* Modal Content (Image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}

/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}

/* Add Animation - Zoom in the Modal */
.modal-content, #caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
from {-webkit-transform:scale(0)}
to {-webkit-transform:scale(1)}
}

@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}

/* The Close Button */
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}

.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
.modal-content {
width: 100%;
}
}

这只是我从网站 https://www.w3schools.com/howto/howto_css_modal_images.asp 复制的模式开始。

尽管模态框根本没有弹开,但我需要它来弹开从单选选择中选择的图像。

如有任何帮助,我们将不胜感激。

最佳答案

尝试删除:

<a href="images/GLAMOURFRONT.jpg"></a>

我认为这会阻止您的图像触发模态。

关于javascript - 基于单选选择的图像显示和模态弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46652598/

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