gpt4 book ai didi

javascript - 基于单选按钮选择的模式弹出

转载 作者:行者123 更新时间:2023-11-28 02:46:12 24 4
gpt4 key购买 nike

我有这个非常接近但不完全。我正在寻找的是根据单选按钮选择循环图像,这很好用。我还希望在单击图像时弹出一个模式,因此如果我选择单选选项 1,则图像 1 会显示并且可以单击模式,对于每个其他单选选项也是如此。同样,这工作正常。不是基于单选按钮图像的模态弹出窗口。

这里是使用的CSS:

#piGal ul {
float: left;
list-style-type: none;
padding: 20px 2em 3em 2em;

}
#piGal img {
max-width: 100%;
height: auto;
padding: .5em .5em 2em .5em;
}
#piGal .outer {
float: left;
padding-bottom: 5px;
}



#piGalImg img {
max-width: 100%;
height: auto;
-webkit-box-shadow: 0px 0px 1px #000;
-moz-box-shadow: 0px 0px 1px #000;
padding-bottom: 5px;
}


#piGalImg .outer {
float: left;
padding-bottom: 5px;
}



/* 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%;
}
}

单选按钮(简化)如下:

<div class="contentText">
<section>
<h5>Choose color</h5>
<ul id="radio-attribute" class="radio-attribute">
<li class="radio-attribute">
<label for="1" class="radio-attribute">
<input type="radio" id="1" name="id[27]" value="170" onClick="change_image(this.id)">
<img src="images/silver_swatch.png" alt="GBM0911S Silver" title=" GBM0911S Silver " width="42" height="40" />
</label>
</li>

<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>
</ul>
</section>
</div>

而javascript如下:

    <script type="text/javascript">
<!--

var images = ["GLAMOURFRONT.jpg","GBM0911S.png"];

function preload(arrayOfImages) {
$(arrayOfImages).each(function () {
$('<img />').attr('src',this).appendTo('body').css('display','none');
});
}

preload(images);

var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
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=\"piGal\" 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";
}

document.querySelectorAll('label.radio-attribute img').forEach(function (radioClick) {
radioClick.addEventListener("click", function () {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
});
});
//-->
</script>

以及图片显示的html:

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

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

</div>

模态的 html :

<!-- 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>

目前,只要单击 radio 图像,模式就会打开,但没有图像(存储在图像数组中)。已经很长时间了,我非常接近解决方案....

谢谢。

最佳答案

当您单击单选按钮时,模式会弹出,因为您在事件监听器中使用 modal.style.display = "block"; 显式调用了该行为。 :

document.querySelectorAll('label.radio-attribute img').forEach(function (radioClick) {
radioClick.addEventListener("click", function () {
modal.style.display = "block"; <-- Remove this line here.
modalImg.src = this.src;
captionText.innerHTML = this.alt;
});
});

我看到的另一个问题是,当您切换图像时,您正在破坏事件监听器,当您调用 change_image 时,该事件监听器会在单击图像时启动模态。 .替换 img标签意味着新的 img不会有相同的 img.onclick处理程序。您也没有更换 <a>包装 img 的标签替换 InnerHTML 时的标记.

要解决这个问题,您可以将监听器放在 piGal 上元素代替:

  document.getElementById("piGal").onclick = function(){
// You'll have to update this to target the image as a child of piGal.
}

也在您的 HTML 中 onClick应该是 onclick .

关于javascript - 基于单选按钮选择的模式弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46913260/

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