我正在按照以下W3School示例中的说明进行幻灯片放映: https://www.w3schools.com/howto/howto_js_slideshow.asp
我遇到以下问题:刷新页面时未显示活动类。
这是问题的链接。
https://github.com/FrancoisXavierPelletier/Intermarium24.com/issues/1
最好的问候。
我试图修改第一个边距和宽度以适应需要,但是第二个和第三个图像没有显示此边距。
预期的更改:该代码应添加一个要显示的活动类,并默认显示它。这可以用Javascript完成。
结果:刷新页面时,第一张幻灯片应该处于活动状态,而不是不显示幻灯片。
这是我使用的Javascript代码:
var slideIndex = 1;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
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";
}
*{
margin: 0;
padding; 0;
background: darkslategrey;
}
body{
font-family: arial, sans-serif;
}
h1{
font-family: 'Crete Round', serif;
font-size: 45px;
color: whitesmoke;
}
.wrapper{
width: 940px;
margin: 0 auto;
padding: 0 0;
}
.orange{
color: orange;
}
header ul{
list-style: none;
}
header a{
text-decoration: none;
color: whitesmoke;
font-weight: bold;
margin-right: 20px;
}
header{
height:100px;
}
header h1{
float: left;
margin-top: 25px;
}
header nav{
float: right;
margin-top: 50px;
}
header nav li
{
display: inline-block;
}
header nav li a {
text-transform: uppercase;
}
.clear{
clear: both;
}
/* Slideshow */
.slideshow-container {
max-width: 940px;
position: relative;
margin: auto;
background: red;
}
.mySlides {
display: none;
height: 300px;
width: 97.5%;
border: 1px solid white;
border-radius: 10px;
}
#slide1{
background: url('images/img1.jpg') center;
}
#slide2{
background: url('images/img1.jpg') center;
}
#slide3{
background: url('images/img1.jpg') center;
}
.prev, .next {
cursor: pointer;
position: absolute;
top: 40%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
.prev {
left: 150px;
border-radius: 3px 0 0 3px;
}
.next {
right: 150px;
border-radius: 3px 0 0 3px;
}
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
border-radius: 3px 0 0 3px;
}
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
margin-top: 237px;
position: relative;
width: 97.4%;
text-align: justify;
background: black;
opacity: 0.8;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
}
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: relative;
top: 0;
background: transparent;
}
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* 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}
}
/* Category */
.category{
height: 200px;
width: 172px;
background: red;
float: left;
margin: 20px 5px;
border: 1px solid white;
border-radius: 5px;
}
#military{
background: url('images/military.jpg') center;
}
#economy{
background: url('images/economy.jpg') center;
}
#society{
background: url('images/social.jpg') center;
}
#culture{
background: url('images/culture.jpg') center;
}
#tech{
background: url('images/tech.jpg') center;
}
h2{
color: white;
font-size: 20px;
text-align: center;
margin-top: 150px;
padding: 5px;
text-transform: uppercase;
font-family: 'Crete Round', serif;
background: black;
opacity: 0.8;
}
/* footer */
footer{
height: 250px;
background-color: white;
}
.footer-div{
background: white;
margin: 20px;
float: left;
width: 20%;
height: 200px;
}
h3{
background: transparent;
color: black;
text-transform: uppercase;
font-weight: bold;
padding: 10px;
margin-top: 10px;
}
footer ul{
list-style: none;
background: white;
}
footer li{
padding: 5px;
background: white;
}
footer a{
background: white;
}
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title> Intermarium24.pl </title>
<link href="http://fonts.googleapis.com/css?family=Crete+Round" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<script src="myscript.js"></script>
</head>
<header>
<div class="wrapper">
<h1>Intermarium<span class="orange">24</span>.com</h1>
<nav>
<ul>
<li><a href="#main-image">Home</a> </li>
<li><a href="#steps">Discover</a> </li>
<li><a href="#possibilities">Register</a> </li>
<li><a href="#contact">Login</a> </li>
<li><a href="#contact">Contact</a> </li>
</ul>
</nav>
</div>
</header>
<section id="slideshow">
<div class="wrapper">
<div id="slideshow-container">
<div class="mySlides fade" id="slide1">
<div class="numbertext">1 / 3</div>
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade" id="slide2">
<div class="numbertext">2 / 3</div>
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade" id="slide3">
<div class="numbertext">3 / 3</div>
<div class="text">Caption Three</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</div>
</section>
<div class="clear"></div>
<section id="Main-articles">
<div class="wrapper">
<div class="category" id="military">
<h2>Military</h2>
</div>
<div class="category" id="economy">
<h2>Economy</h2>
</div>
<div class="category" id="society">
<h2>Society</h2>
</div>
<div class="category" id="culture">
<h2>Culture</h2>
</div>
<div class="category" id="tech">
<h2>Tech</h2>
</div>
</div>
<div class="clear"></div>
</section>
<footer>
<div class="wrapper">
<div class="footer-div">
<h3> Main menu</h3>
<ul>
<li><a>Home </a></li>
<li><a>Discover </a></li>
<li><a>Register </a></li>
<li><a>Login </a></li>
<li><a>Contact</a></li>
</ul>
</div>
<div class="footer-div" id="footer-category">
<h3> Categories</h3>
<ul>
<li><a>Military </a></li>
<li><a>Economy </a></li>
<li><a>Social </a></li>
<li><a>Cultural </a></li>
<li><a>Tech</a></li>
</ul>
</div>
<div class="footer-div" id="footer-legal">
<h3> Categories</h3>
<ul>
<li><a>Terms of Sales </a></li>
<li><a>Terms of Use </a></li>
<li><a>Privacy Policy </a></li>
<li><a>Legal Contact </a></li>
</ul>
</div>
<div class="footer-div" id="footer-socmed">
<h3> Social medias</h3>
<ul>
<li><a>Facebook </a></li>
<li><a>Twitter </a></li>
<li><a>Contact </a></li>
</ul>
</div>
</div>
</footer>
</html>