您好,我从w3学校获得的代码仅适用于一个图像,但是我的网页上有多个图像,如何使用带for循环的数组?请帮助我被困了几个小时。我有一个主意,但这涉及多个功能,我会使用switch语句获取图像ID吗? https://www.w3schools.com/howto/howto_css_modal_images.asp
这是小提琴:https://jsfiddle.net/sfL71epx/ 点击第二张图片 由于我编辑时会显示某些原因,html被隐藏了。让我知道你是否知道为什么。
node_modules
// Get the modal
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("img01");
var captionText = document.getElementById("caption");
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";
}
答案 0 :(得分:0)
问题是,实际上打开模式的click事件侦听器仅附加到单个图像-来自w3schools示例的图像。您需要将其附加到每个图像。 幸运的是,事情并不难。您所有的图片都具有相同的 img-fluid img-thumbnail 类。使用名为 getElementsByClassName()的便捷Javascript函数,我们可以获取您站点上所有共享该特定类的图像的数组。 完成之后,您只需要遍历返回的数组,并将点击侦听器附加到每个数组。
var images = document.getElementsByClassName("img-fluid img-thumbnail");
for (var a = 0; a < images.length; a++) {
images[a].onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
}
这是现场演示:
// Get the modal
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("img01");
var captionText = document.getElementById("caption");
img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
var images = document.getElementsByClassName("img-fluid img-thumbnail");
for (var a = 0; a < images.length; a++) {
images[a].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";
}
/*!
* Start Bootstrap - Clean Blog v5.0.5 (https://startbootstrap.com/template-overviews/clean-blog)
* Copyright 2013-2019 Start Bootstrap
* Licensed under MIT (https://github.com/BlackrockDigital/startbootstrap-clean-blog/blob/master/LICENSE)
*/
body {
font-size: 20px;
color: #212529;
font-family: Lora, 'Times New Roman', serif
}
p {
line-height: 1.5;
margin: 30px 0
}
p a {
text-decoration: underline
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-weight: 800;
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif
}
a {
color: #212529;
-webkit-transition: all .2s;
transition: all .2s
}
a:focus,
a:hover {
color: #0085a1
}
blockquote {
font-style: italic;
color: #868e96
}
.section-heading {
font-size: 36px;
font-weight: 700;
margin-top: 60px
}
.caption {
font-size: 14px;
font-style: italic;
display: block;
margin: 0;
padding: 10px;
text-align: center;
border-bottom-right-radius: 5px;
border-bottom-left-radius: 5px
}
::selection {
color: #fff;
background: #0085a1;
text-shadow: none
}
img::selection {
color: #fff;
background: 0 0
}
img::-moz-selection {
color: #fff;
background: 0 0
}
#mainNav {
position: absolute;
border-bottom: 1px solid #e9ecef;
background-color: #fff;
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif
}
#mainNav .navbar-brand {
font-weight: 800;
color: #343a40
}
#mainNav .navbar-toggler {
font-size: 12px;
font-weight: 800;
padding: 13px;
text-transform: uppercase;
color: #343a40
}
#mainNav .navbar-nav>li.nav-item>a {
font-size: 12px;
font-weight: 800;
letter-spacing: 1px;
text-transform: uppercase
}
@media only screen and (min-width:992px) {
#mainNav {
border-bottom: 1px solid transparent;
background: 0 0
}
#mainNav .navbar-brand {
padding: 10px 20px;
color: #fff
}
#mainNav .navbar-brand:focus,
#mainNav .navbar-brand:hover {
color: rgba(255, 255, 255, .8)
}
#mainNav .navbar-nav>li.nav-item>a {
padding: 10px 20px;
color: #fff
}
#mainNav .navbar-nav>li.nav-item>a:focus,
#mainNav .navbar-nav>li.nav-item>a:hover {
color: rgba(255, 255, 255, .8)
}
}
@media only screen and (min-width:992px) {
#mainNav {
-webkit-transition: background-color .2s;
transition: background-color .2s;
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
-webkit-backface-visibility: hidden
}
#mainNav.is-fixed {
position: fixed;
top: -67px;
-webkit-transition: -webkit-transform .2s;
transition: -webkit-transform .2s;
transition: transform .2s;
transition: transform .2s, -webkit-transform .2s;
border-bottom: 1px solid #fff;
background-color: rgba(255, 255, 255, .9)
}
#mainNav.is-fixed .navbar-brand {
color: #212529
}
#mainNav.is-fixed .navbar-brand:focus,
#mainNav.is-fixed .navbar-brand:hover {
color: #0085a1
}
#mainNav.is-fixed .navbar-nav>li.nav-item>a {
color: #212529
}
#mainNav.is-fixed .navbar-nav>li.nav-item>a:focus,
#mainNav.is-fixed .navbar-nav>li.nav-item>a:hover {
color: #0085a1
}
#mainNav.is-visible {
-webkit-transform: translate3d(0, 100%, 0);
transform: translate3d(0, 100%, 0)
}
}
header.masthead {
margin-bottom: 50px;
background: no-repeat center center;
background-color: #868e96;
background-attachment: scroll;
position: relative;
background-size: cover
}
header.masthead .overlay {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: #ca5981;
opacity: .5;
}
header.masthead .page-heading,
header.masthead .post-heading,
header.masthead .site-heading {
padding: 200px 0 150px;
color: #fff
}
@media only screen and (min-width:768px) {
header.masthead .page-heading,
header.masthead .post-heading,
header.masthead .site-heading {
padding: 200px 0
}
}
header.masthead .page-heading,
header.masthead .site-heading {
text-align: center
}
header.masthead .page-heading h1,
header.masthead .site-heading h1 {
font-size: 50px;
margin-top: 0
}
header.masthead .page-heading .subheading,
header.masthead .site-heading .subheading {
font-size: 40px;
font-weight: 400;
line-height: 1.1;
display: block;
margin: 10px 0 0;
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
@media only screen and (min-width:768px) {
header.masthead .page-heading h1,
header.masthead .site-heading h1 {
font-size: 80px
}
}
header.masthead .post-heading h1 {
font-size: 35px
}
header.masthead .post-heading .meta,
header.masthead .post-heading .subheading {
line-height: 1.1;
display: block
}
header.masthead .post-heading .subheading {
font-size: 24px;
font-weight: 600;
margin: 10px 0 30px;
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif
}
header.masthead .post-heading .meta {
font-size: 20px;
font-weight: 300;
font-style: italic;
font-family: Lora, 'Times New Roman', serif
}
header.masthead .post-heading .meta a {
color: #fff
}
@media only screen and (min-width:768px) {
header.masthead .post-heading h1 {
font-size: 55px
}
header.masthead .post-heading .subheading {
font-size: 30px
}
}
.post-preview>a {
color: #212529
}
.post-preview>a:focus,
.post-preview>a:hover {
text-decoration: none;
color: #0085a1
}
.post-preview>a>.post-title {
font-size: 30px;
margin-top: 30px;
margin-bottom: 10px
}
.post-preview>a>.post-subtitle {
font-weight: 300;
margin: 0 0 10px
}
.post-preview>.post-meta {
font-size: 18px;
font-style: italic;
margin-top: 0;
color: #868e96
}
.post-preview>.post-meta>a {
text-decoration: none;
color: #212529
}
.post-preview>.post-meta>a:focus,
.post-preview>.post-meta>a:hover {
text-decoration: underline;
color: #0085a1
}
@media only screen and (min-width:768px) {
.post-preview>a>.post-title {
font-size: 36px
}
}
.floating-label-form-group {
font-size: 14px;
position: relative;
margin-bottom: 0;
padding-bottom: .5em;
border-bottom: 1px solid #dee2e6
}
.floating-label-form-group input,
.floating-label-form-group textarea {
font-size: 1.5em;
position: relative;
z-index: 1;
padding: 0;
resize: none;
border: none;
border-radius: 0;
background: 0 0;
-webkit-box-shadow: none !important;
box-shadow: none !important;
font-family: Lora, 'Times New Roman', serif
}
.floating-label-form-group input::-webkit-input-placeholder,
.floating-label-form-group textarea::-webkit-input-placeholder {
color: #868e96;
font-family: Lora, 'Times New Roman', serif
}
.floating-label-form-group label {
font-size: .85em;
line-height: 1.764705882em;
position: relative;
z-index: 0;
top: 2em;
display: block;
margin: 0;
-webkit-transition: top .3s ease, opacity .3s ease;
transition: top .3s ease, opacity .3s ease;
vertical-align: middle;
vertical-align: baseline;
opacity: 0
}
.floating-label-form-group .help-block {
margin: 15px 0
}
.floating-label-form-group-with-value label {
top: 0;
opacity: 1
}
.floating-label-form-group-with-focus label {
color: #0085a1
}
form .form-group:first-child .floating-label-form-group {
border-top: 1px solid #dee2e6
}
footer {
padding: 50px 0 65px
}
footer .list-inline {
margin: 0;
padding: 0
}
footer .copyright {
font-size: 14px;
margin-bottom: 0;
text-align: center
}
.btn {
font-size: 14px;
font-weight: 800;
padding: 15px 25px;
letter-spacing: 1px;
text-transform: uppercase;
border-radius: 0;
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif
}
.btn-primary {
background-color: #0085a1;
border-color: #0085a1
}
.btn-primary:active,
.btn-primary:focus,
.btn-primary:hover {
color: #fff;
background-color: #00657b !important;
border-color: #00657b !important
}
.btn-lg {
font-size: 16px;
padding: 25px 35px
}
body>div:nth-child(3)>ul {
float: right;
margin: auto;
padding: 10px;
display: flexbox;
list-style-type: none;
}
body>div:nth-child(3)>ul>li {
border: 2px solid #e485a5;
border-radius: 20px;
text-align: center;
margin-bottom: 20px
}
body>div:nth-child(3)>ul>li:hover {
border: 2px solid aqua;
background-color: #e485a5;
border-radius: 20px;
text-align: center;
-webkit-animation: flip 3s linear 0s infinite normal;
animation: flip 3s linear 0s infinite normal;
}
body>div:nth-child(3)>ul>li:nth-child(3),
body>div:nth-child(3)>ul>li:nth-child(4) {
border: 2px solid #e485a5;
padding-left: 5px;
padding-right: 5px;
}
body>div:nth-child(3)>ul>li:nth-child(3):hover,
body>div:nth-child(3)>ul>li:nth-child(4):hover {
border: 2px solid #e485a5;
padding-left: 5px;
padding-right: 5px;
border: 2px solid aqua;
background-color: #e485a5;
}
@-webkit-keyframes flip {
0% {
-webkit-transform: translateY(0);
transform: rotateY(0deg);
}
50% {
-webkit-transform: translateY(180);
transform: rotateY(180deg);
}
100% {
-webkit-transform: translateY(0);
transform: rotateY(0deg);
}
}
@keyframes flip {
0% {
transform: rotateY(0deg);
}
50% {
transform: rotateY(180deg);
}
100% {
transform: rotateY(0deg);
}
}
#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 */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation */
.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="container">
<div class="row text-center text-lg-left">
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="http://powermovesandpumps.com/wp-content/uploads/2018/09/Walking-through-Kuta-Bali-2-300x400.jpg" alt="">
</a>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail " id="myImg" src="http://powermovesandpumps.com/wp-content/uploads/2018/09/IMG_4082-768x512.jpg" alt="">
</a>
</div>
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="https://source.unsplash.com/EUfxH-pze7s/400x300" alt="">
</a>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="https://source.unsplash.com/M185_qYH8vg/400x300" alt="">
</a>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="https://source.unsplash.com/sesveuG_rNo/400x300" alt="">
</a>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="https://source.unsplash.com/AvhMzHwiE_0/400x300" alt="">
</a>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="https://source.unsplash.com/2gYsZUmockw/400x300" alt="">
</a>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="https://source.unsplash.com/EMSDtjVHdQ8/400x300" alt="">
</a>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="https://source.unsplash.com/8mUEy0ABdNE/400x300" alt="">
</a>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="https://source.unsplash.com/G9Rfc1qccH4/400x300" alt="">
</a>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="https://source.unsplash.com/aJeH0KcFkuc/400x300" alt="">
</a>
</div>
<div class="col-lg-3 col-md-4 col-6">
<a href="#" class="d-block mb-4 h-100">
<img class="img-fluid img-thumbnail" src="https://source.unsplash.com/p2TQ-3Bh3Oo/400x300" alt="">
</a>
</div>
</div>
</div>