[英]How can I put a slideshow in a webpage? I have used the following code but its not working
I wanted to create a slideshow with 3 images inside a div
.我想在
div
中创建一个包含 3 个图像的幻灯片。 The slideshow will appear in a position at the top-left corner of the webpage.幻灯片将出现在网页左上角的 position 中。 There are other divs and functionality in the web page as well.
web 页面中还有其他 div 和功能。
I used this section of code inside the div for slideshow.我在 div 中使用这部分代码进行幻灯片放映。 I used mostly inline styles.
我主要使用内联 styles。 The rest classes are just for positioning and size of the div.
rest 类仅用于 div 的定位和大小。
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"; }
.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; float: left; text-align: center; }
<div role="list" class="banner__list w-dyn-items"> <div style="background-image:url(/frontEnd/5a10aaa4d85f4b0001a5419a_2017-lamborghini-huracan-spyder-orange-exterior-front-angle-royalty-exotic-cars.jpg)" role="listitem" class="banner__item w-dyn-item mySlides"> </div> <div style="background-image:url(/frontEnd/5a10aaa4d85f4b0001a541d8_2015-bentley-continental-gtc-red-car-hero-2-image-royalty-exotic-cars.jpg)" role="listitem" class="banner__item w-dyn-item mySlides"> </div> <div style="background-image:url(/frontEnd/5abda7fc38a916291e1647d3_2018-jeep-wrangler-white-isolated-front-angle-royalty-exotic-cars.jpg)" role="listitem" class="banner__item w-dyn-item mySlides"> </div> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> </div>
The dots are not even appearing.这些点甚至没有出现。 the first image is appearing only.
仅出现第一张图像。 whats wrong here?
这里有什么问题?
Maybe those other classes you told about are overwriting some of your code.也许你告诉过的那些其他类正在覆盖你的一些代码。
I just added width
and height
properties to your banner__list
div and it worked well.我刚刚在您的
banner__list
div 中添加了width
和height
属性,并且效果很好。 I also added some border colors for showing the images are really changing.我还添加了一些边框 colors 以显示图像确实在变化。
See:看:
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"; }
<div role="list" class="banner__list w-dyn-items" style="width: 300px; height: 300px;"> <div style="border: 3px solid red; background-image:url(https://via.placeholder.com/300); width: 100%; height: 100%;" role="listitem" class="banner__item w-dyn-item mySlides"> </div> <div style="border: 3px solid green; background-image:url(https://via.placeholder.com/300); width: 100%; height: 100%;" role="listitem" class="banner__item w-dyn-item mySlides"> </div> <div style="border: 3px solid blue; background-image:url(https://via.placeholder.com/300); width: 100%; height: 100%;" role="listitem" class="banner__item w-dyn-item mySlides"> </div> <span class="dot" style="cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; float:left; text-align:center;" onclick="currentSlide(1)"></span> <span class="dot" style="cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; float:left; text-align:center;" onclick="currentSlide(2)"></span> <span class="dot" style="cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; float:left; text-align:center;" onclick="currentSlide(3)"></span> </div>
The slide show works, you just can't see the images because they're background images, and without styles or content the div
s have a 0px
width and height.幻灯片放映有效,您只是看不到图像,因为它们是背景图像,并且没有 styles 或内容,
div
的宽度和高度为0px
。 Setting that shows the image correctly:正确显示图像的设置:
.banner__item {
width:50px;
height:50px;
}
As a side note, if you put those images in the HTML as actual img
tags, you don't need to specify the width and height.附带说明一下,如果您将这些图像作为实际的
img
标签放在 HTML 中,则无需指定宽度和高度。 This has the added benefit of allowing you to use the alt
attribute, which improves accessibility.这具有允许您使用
alt
属性的额外好处,从而提高了可访问性。
Also, inline styles can cause headaches down the road.此外,内联 styles 可能会导致头疼。 Applying the styles via classes can clean up the markup, and make things easier to maintain in the long run.
通过类应用 styles 可以清理标记,从长远来看使事情更容易维护。
Along with inline styles, inline JS can cause headaches too.与内联 styles 一起,内联 JS 也会引起头痛。 You can move the inline
onClick
handlers into an event listener, to get the markup cleaner still.您可以将内联
onClick
处理程序移动到事件侦听器中,以使标记更清洁。 In the snippet, I've added the listener:在片段中,我添加了监听器:
window.addEventListener("click", currentSlide);
To account for the click listener, rather than passing in the index I looked it up:为了说明点击侦听器,而不是传入索引,我查找了它:
function currentSlide(event) {
const dots = document.getElementsByClassName("dot");
const clickIndex = Array.from(dots).indexOf(event.target);
showSlides(clickIndex);
}
I also modified the showSlides logic to just use 0 based arrays, rather than subtracting 1 from the integer passed in. Hopefully it all still works for you: :)我还修改了 showSlides 逻辑,只使用基于 0 的 arrays,而不是从传入的 integer 中减去 1。希望它仍然对你有用::)
If you're wanting to center things, I usually reach for flexbox these days!如果您想将事物居中,这些天我通常会选择flexbox !
You can see it working on with how you have it in this codepen , or with recommended changes in the snippet below:您可以在此 codepen中看到它的工作方式,或者在下面的代码段中进行推荐的更改:
showSlides(0); function plusSlides(n) { showSlides(slideIndex += n); } function currentSlide(event) { const dots = document.getElementsByClassName("dot"); const clickIndex = Array.from(dots).indexOf(event.target); showSlides(clickIndex); } function showSlides(slideIndex) { var i; var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); 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].style.display = "block"; dots[slideIndex].className += " active"; } window.addEventListener("click", currentSlide);
.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; float: left; text-align: center; }.banner__list { width: 100px; }.banner__item { width: 100%; margin: 0 auto; text-align: center; }.banner__list { display: flex; flex-wrap: wrap; justify-content: center; }
<div role="list" class="banner__list w-dyn-items"> <div role="listitem" class="banner__item w-dyn-item mySlides"> <img src="http://placehold.it/50/bada55" alt="green square" /> </div> <div role="listitem" class="banner__item w-dyn-item mySlides"> <img src="http://placehold.it/50/990000" alt="red square" /> </div> <div role="listitem" class="banner__item w-dyn-item mySlides"> <img src="http://placehold.it/50/000099" alt="blue square" /> </div> <span class="dot"></span> <span class="dot"></span> <span class="dot"></span> </div>
You can try Twitter's Bootstrap.你可以试试 Twitter 的 Bootstrap。 It has a slideshow component.
它有一个幻灯片组件。
Add this to your style:将此添加到您的样式中:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
And here's the sample template:这是示例模板:
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="[IMAGE-1-SOURCE]" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="[IMAGE-2-SOURCE]" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="[IMAGE-3-SOURCE]" alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.