所以我有一个HTML滑块模板,我从W3学校跟随,我按照模板在我公司使用原始HTML管理的Facebook页面上放置幻灯片。我的问题是,首次加载页面时,第一张幻灯片根本不显示。直到用户继续下载幻灯片图像才会加载。
我已将我的代码与原始代码进行了比较,并且应该没有任何错误,因为我只是使用了模板并更改了一些文本/按钮颜色。
这是我遵循的模板:https://www.w3schools.com/howto/howto_js_slideshow.asp
这是代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Woobox Digital Deals Slider</title>
</head>
<body>
<script>
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("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";
}
</script>
<style>
* {box-sizing:border-box}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
.mySlides {
display: none;
}
/* Next & previous buttons */
.prev, .next {
background-color: rgba(0,0,0,0.65);
cursor: pointer;
position: absolute;
top: 50%;
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;
}
/* 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);
}
/* Heading Text */
#slideshow-heading {
color: rgba(222,42,0,1.00); /*Primary site color goes here*/
font-size: 18px;
text-align: center;
padding: 20px 10px 20px 10px
}
/*Comment styling*/
.text {
visibility: hidden;
}
/* Number text (1/3 etc) */
.numbertext {
color: #000000;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
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, .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}
}
</style>
<div>
<p id="slideshow-heading">
Click or Tap image to print
</p>
</div>
<div class="slideshow-container">
<div class="mySlides fade">
<a href="google.com" target="_blank">
<img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-19.jpg" style="width:100%;height:auto;">
</a>
</div>
<div class="mySlides fade">
<img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-18.jpg" style="width:100%;height:auto;">
</div>
<div class="mySlides fade">
<img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-17.jpg" style="width:100%;height:auto;">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</body>
</html>
感谢@Zerone向我解释为什么我的代码无效。我没有意识到,当页面被加载时,它执行的代码是顺序的。这有助于扩展我对HTML行为的理解,再次感谢Zerone!
答案 0 :(得分:0)
当浏览器看到您提供的整个页面时,它将自上而下。这意味着它首先看到JavaScript并尝试执行:
connect
在具有类名&#34; MySlides&#34;的元素之前生成,因此它不知道该怎么做。解决方案是Zerone提出的
答案 1 :(得分:0)
如果您将脚本块移动到html的底部,在最后一个div之后和body标签之前,如下所示:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Woobox Digital Deals Slider</title>
</head>
<body>
<style>
* {box-sizing:border-box}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
.mySlides {
display: none;
}
/* Next & previous buttons */
.prev, .next {
background-color: rgba(0,0,0,0.65);
cursor: pointer;
position: absolute;
top: 50%;
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;
}
/* 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);
}
/* Heading Text */
#slideshow-heading {
color: rgba(222,42,0,1.00); /*Primary site color goes here*/
font-size: 18px;
text-align: center;
padding: 20px 10px 20px 10px
}
/*Comment styling*/
.text {
visibility: hidden;
}
/* Number text (1/3 etc) */
.numbertext {
color: #000000;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
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, .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}
}
</style>
<div>
<p id="slideshow-heading">
Click or Tap image to print
</p>
</div>
<div class="slideshow-container">
<div class="mySlides fade">
<a href="google.com" target="_blank">
<img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-19.jpg" style="width:100%;height:auto;">
</a>
</div>
<div class="mySlides fade">
<img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-18.jpg" style="width:100%;height:auto;">
</div>
<div class="mySlides fade">
<img src="http://www.shoppingindanville.com/wp-content/uploads/2017/06/Artboard-17.jpg" style="width:100%;height:auto;">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<script>
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("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";
}
</script>
</body>
</html>
这将阻止Java执行,直到页面内容的其余部分加载完毕:)