gpt4 book ai didi

用于幻灯片放映的 Javascript 无法正常工作

转载 作者:行者123 更新时间:2023-11-28 03:33:37 25 4
gpt4 key购买 nike

我正在尝试创建一个幻灯片,其中每张幻灯片以 2000 毫秒的间隔显示,但是,我无法显示第二张幻灯片...我在下面包含了我的 Javascript 和 CSS 以及任何帮助或简单您认为有帮助的修改将不胜感激。我相信这是一个简单的修复,但我只需要另一双眼睛来帮助解决这个问题......

body{
background: white;
font-family: 'Raleway', sans-serif;
margin: 0 auto;
}
/* FOR SLIDES */

/*
essential styles:
these make the slideshow work
*/
#slides{
position: relative;
height: 100%;
padding: 0px;
list-style-type: none;
}

.slide{
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
opacity: 0;
z-index: 1;

-webkit-transition: opacity 1s;
-moz-transition: opacity 1s;
-o-transition: opacity 1s;
transition: opacity 1s;
}

.showing{
opacity: 1;
z-index: 2;
}



/*
non-essential styles:
just for appearance; change whatever you want
*/

.slide{
font-size: 40px;
padding: 40px;
box-sizing: border-box;
background: #333;
color: #fff;
}

.slide:nth-of-type(1){
background: url(Slide0.png) no-repeat;
background-size: cover;
background-position: 50% 50%;
}
.slide:nth-of-type(2){
background: url(Slide1.png) no-repeat;
background-size: cover;
background-position: 50% 50%;
}
.slide:nth-of-type(3){
background: url(Slide2.png) no-repeat;
background-size: cover;
background-position: 50% 50%;
}
.slide:nth-of-type(4){
background: url(Slide3.png) no-repeat;
background-size: cover;
background-position: 50% 50%;
}
.slide:nth-of-type(5){
background: url(Slide4.png) no-repeat;
background-size: cover;
background-position: 50% 50%;
}
<DOCTYPE! html>
<head>
<link href="https://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet">
<!-- CSS -->

<link rel="stylesheet" type="text/css" href="style.css"/>
<title>Widget1.0</title>
<!-- JS!!! -->
<script>
var slides = document.querySelectorAll('#slides .slide');
var currentSlide = 0;
var slideInterval = setInterval(nextSlide,2000);

function nextSlide(){
slides[currentSlide].className = 'slide';
currentSlide = (currentSlide+1)%slides.length;
slides[currentSlide].className = 'slide showing';
}

var playing = true;
var pauseButton = document.getElementById('pause');

function pauseSlideshow(){
pauseButton.innerHTML = 'Play';
playing = false;
clearInterval(slideInterval);
}

function playSlideshow(){
pauseButton.innerHTML = 'Pause';
playing = true;
slideInterval = setInterval(nextSlide,2000);
}

pauseButton.onclick = function(){
if(playing){ pauseSlideshow(); }
else{ playSlideshow(); }
};
</script>
</head>
<body>
<ul id="slides">
<li class="slide showing" id="slide1">Slide 1</li>
<li class="slide" id="slide2">Slide 2</li>
<li class="slide" id="slide3">Slide 3</li>
<li class="slide" id="slide4">Slide 4</li>
<li class="slide" id="slide5">Slide 5</li>
</ul>
<button class="controls" id="pause">Pause</button>
</body>
</html>

最佳答案

您需要将 javascript 放在 DOM 就绪事件上或在元素呈现后移动函数,即:在 </body> 之上标签

body{
background: white;
font-family: 'Raleway', sans-serif;
margin: 0 auto;
}
/* FOR SLIDES */

/*
essential styles:
these make the slideshow work
*/
#slides{
position: relative;
height: 200px;
padding: 0px;
list-style-type: none;
}

.slide{
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
opacity: 0;
z-index: 1;

-webkit-transition: opacity 1s;
-moz-transition: opacity 1s;
-o-transition: opacity 1s;
transition: opacity 1s;
}

.showing{
opacity: 1;
z-index: 2;
}



/*
non-essential styles:
just for appearance; change whatever you want
*/

.slide{
font-size: 40px;
padding: 40px;
box-sizing: border-box;
background: #333;
color: #fff;
}

.slide:nth-of-type(1){
background-color: red;
background-size: cover;
background-position: 50% 50%;
}
.slide:nth-of-type(2){
background-color: blue;
background-size: cover;
background-position: 50% 50%;
}
.slide:nth-of-type(3){
background-color: green;
background-size: cover;
background-position: 50% 50%;
}
.slide:nth-of-type(4){
background-color: magenta;
background-size: cover;
background-position: 50% 50%;
}
.slide:nth-of-type(5){
background-color: orange;
background-size: cover;
background-position: 50% 50%;
}
#pause
<DOCTYPE! html>
<head>
<link href="https://fonts.googleapis.com/css?family=Raleway:100" rel="stylesheet">
<!-- CSS -->

<link rel="stylesheet" type="text/css" href="style.css"/>
<title>Widget1.0</title>
</head>
<body>
<ul id="slides">
<li class="slide showing" id="slide1">Slide 1</li>
<li class="slide" id="slide2">Slide 2</li>
<li class="slide" id="slide3">Slide 3</li>
<li class="slide" id="slide4">Slide 4</li>
<li class="slide" id="slide5">Slide 5</li>
</ul>
<button class="controls" id="pause">Pause</button>
<!-- JS!!! -->
<script>
var slides = document.querySelectorAll('#slides .slide');
var currentSlide = 0;
var slideInterval = setInterval(nextSlide,2000);

function nextSlide(){
slides[currentSlide].className = 'slide';
currentSlide = (currentSlide+1)%slides.length;
slides[currentSlide].className = 'slide showing';
}

var playing = true;
var pauseButton = document.getElementById('pause');

function pauseSlideshow(){
pauseButton.innerHTML = 'Play';
playing = false;
clearInterval(slideInterval);
}

function playSlideshow(){
pauseButton.innerHTML = 'Pause';
playing = true;
slideInterval = setInterval(nextSlide,2000);
}

pauseButton.onclick = function(){
if(playing){ pauseSlideshow(); }
else{ playSlideshow(); }
};
</script>

</body>
</html>

关于用于幻灯片放映的 Javascript 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44593858/

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