【发布时间】:2017-09-12 06:48:41
【问题描述】:
所以我有一个来自 W3 Schools 的 HTML 滑块模板,我按照模板将幻灯片放在我公司使用原始 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!
【问题讨论】:
-
只需将脚本标签放在正文中的html末尾,它就可以正常工作:) DEMO:jsbin.com/bobasebemi/2/edit?html,output
-
我会被诅咒的。现在我有一个问题作为回报,为什么会这样?
-
通过将脚本标签放在最后,您在所有元素加载后运行脚本(即可通过脚本访问)。通过将脚本放在第一张幻灯片上方不可见,但单击 nxt/back 您调用该函数因此需要输出(因为在加载元素后调用函数)脚注:阅读本文以获得更好的知识github.com/bendc/frontend-guidelines
-
非常感谢@Zerone。严重赞赏。我对 Web 编程还比较陌生,因此非常感谢任何帮助和指点,它有助于提高我对 Web 语言如何运行和行为的了解。
-
我的荣幸先生 :)
标签: javascript html css