gpt4 book ai didi

Javascript - CSS 幻灯片不显示默认图像

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:23 25 4
gpt4 key购买 nike

我在当前的 passion-project 中实现的 CSS 幻灯片放映有问题,其中在加载网页时没有显示默认图像。导航/点按后,图像成功显示,但我似乎无法弄清楚将在页面加载时显示第一张图像的小代码片段(可能是 Javascript 函数中缺少的)。我试图简单地将“事件”类添加到所需的图像,但这只会使“点”按钮显示为事件状态,而不会显示相应的图像。感谢任何帮助

代码如下:

var slideIndex = 1;



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

function currentSlide(n) {
'use strict';
showSlides(slideIndex = n);
}

function showSlides(n) {
'use strict';
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");

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";
}
* {
box-sizing: border-box
}

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

.mySlides {
display: none
}


/* Slideshow container */

.slideshow-container {
max-width: 400px;
position: relative;
margin: auto;
}


/* Next & previous buttons */

.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}


/* Position the "next button" to the right */

.next {
right: 0;
border-radius: 3px 0 0 3px;
}


/* On hover, add see-through background */

.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}


/* Caption text */

.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}


/* Number text (1/3 etc) */

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


/* */

.transition {
cursor: pointer;
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}


/* */

.active,
.transition:hover {
background-color: #222;
}


/* Fading animation */

.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}


/* */

@-webkit-keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}


/* */

@keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}


/* On smaller screens, decrease text size */

@media only screen and (max-width: 300px) {
.prev,
.next,
.text {
font-size: 11px
}
}
<div class="homePage">
<div class="slideshow-container">

<div class="mySlides fade">
<div class="numbertext">1 / 2</div>
<img src="graphics/logo.jpg" style="width:100%">
<div class="text"></div>
</div>

<div class="mySlides fade">
<div class="numbertext">2 / 2</div>

<img src="graphics/1.jpg" style="width:100%">
<div class="text"></div>
</div>


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

</div>

<br>

<div style="text-align:center">

<span class="transition" onclick="currentSlide(1)"></span>
<span class="transition" onclick="currentSlide(2)"></span>

<br>
</div>

最佳答案

您可以为 slider 中的第一张图片指定另一个类

<div class="mySlides fade block ">
<div class="numbertext">1 / 2</div>
<img src="#" style="width:100%">
<div class="text"></div>
</div>

然后你给它一个display:block

.block {
display:block;
}

示例:https://jsfiddle.net/1ueun1L1/2/

关于Javascript - CSS 幻灯片不显示默认图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46088818/

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