gpt4 book ai didi

javascript - 有什么方法可以在一个页面上放很多不同的幻灯片,而无需为每个幻灯片复制一堆重命名的函数?

转载 作者:行者123 更新时间:2023-11-28 02:41:57 27 4
gpt4 key购买 nike

嘿,我有一个包含一堆模态图像的页面,当单击其中任何一个时,它会打开相应的图像幻灯片。但是模式使用 js 进行幻灯片放映,所以有什么方法可以编辑我的函数,这样每张图片都会打开一个不同的幻灯片放映相册,而不只是一遍又一遍地复制相同的函数并更改变量名称,因为这似乎是一种不好的方法.

我有一个 JSfiddle 来显示一张图片的样子,以及我所有的代码。

https://jsfiddle.net/nhk3o0m1/26/

HTML:

<h2 style="text-align:center">Modal Albums</h2>

<div class="row">

<img src="https://www.yosemitehikes.com/images/wallpaper/yosemitehikes.com-bridalveil-winter-1200x800.jpg" style="max-width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">

</div>

<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="http://chasingseals.com/wp-content/uploads/2014/02/greenlandBanner2000x800.jpg" class="img">
</div>

<div class="mySlides">
<div class="numbertext">2 / 4</div>
<img src="http://www.catholicevangelism.org/wp-content/uploads/2013/06/1200x800.gif" class="img">
</div>

<div class="mySlides">
<div class="numbertext">3 / 4</div>
<img src="http://www.a1carpet-to.com/wp-content/uploads/2015/08/600x400.png" class="img">
</div>

<div class="mySlides">
<div class="numbertext">4 / 4</div>
<img src="https://support.kickofflabs.com/wp-content/uploads/2016/06/800x1200.png" class="img">
</div>

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

CSS:

   body {
font-family: Verdana, sans-serif;
margin: 0;
}

* {
box-sizing: border-box;
}
.img {
max-width:100%;
max-height: 100%;
width: auto;
heigth: auto;
object-fit: scale-down;
}
.row > .column {
padding: 0 8px;
}

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

.column {
float: left;
width: 25%;
}
/* 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: rgba(0, 0, 0, 0.9);
}
/* Modal Content */

.modal-content {
position: relative;
background-color: rgba(0, 0, 0, 0.9);
margin: auto;
padding: 0;
width: 90%;
max-width: 1200px;
}
/* The Close Button */

.close {
color: white;
position: absolute;
top: 10px;
right: 25px;
font-size: 35px;
font-weight: bold;
}

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

.mySlides {
display: none;
}

.cursor {
cursor: pointer
}
/* Next & previous buttons */

.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-user-select: none;
}
/* Position the "next button" to the right */

.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */

.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
text-decoration: none;
}
/* Number text (1/3 etc) */

.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}

img {
margin-bottom: -4px;
}

t img.hover-shadow {
transition: all .2s ease-in-out;
}

.hover-shadow:hover {
-webkit-transition: all .2s ease-in;
-webkit-transform: scale(1.1);
-ms-transition: all .2s ease-in;
-ms-transform:scale(1.1);
-moz-transition:all .2s ease-in;
-moz-transform: scale(1.1);
transition: all .2s ease-in;
transform: scale(1.1);
}
.hover-shadow{
-webkit-transition: all .2s ease-in;
-webkit-transform: scale(1);
-ms-transition: all .2s ease-in;
-ms-transform:scale(1);
-moz-transition:all .2s ease-in;
-moz-transform: scale(1);
transition: all .2s ease-in;
transform: scale(1);
}

.modal-content {
-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)
}
}


.mySlides img {
height:100%;
margin:0 auto;
margin-bottom:50px;
}

.mySlides {
text-align:center;
height:80vh;
}

JS:

 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;
}

最佳答案

我会这样处理

  1. 所有幻灯片的 ID。
  2. 在 HTML mySlides div 中调用函数时输入图像地址和幻灯片的 ID
  3. `myFunc( http://image , mySlidesID);
  4. 然后制作<image>标签 SRC="image I Called"

对不起,如果我解释得有点笨拙。

function aFunction(imageURL1,imageURL2,imageURL3,imageURL4, imageID1,imageID2,imageID3, imageID4 ) {

getElementByID(imageID1)
make the src of the image src = imageURL1

}

有关 JavaScript 的更多帮助,我会使用 W3Schools https://www.w3schools.com/js/default.asp

关于javascript - 有什么方法可以在一个页面上放很多不同的幻灯片,而无需为每个幻灯片复制一堆重命名的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47127394/

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