简体   繁体   English

如何在简单的幻灯片显示中添加当前幻灯片编号和淡入淡出效果

[英]How to add current slide number and fade effect to simple slideshow

I am trying to create a very simple fullscreen website and I found this script in w3schools but I would like to know if it would be possible to add a fadein fadeout effect the slider. 我正在尝试创建一个非常简单的全屏网站,并且在w3schools中找到了此脚本,但我想知道是否可以在滑块上添加淡入淡出效果。

Could someone find a way to display the current slide number as well? 有人可以找到一种方法来显示当前幻灯片编号吗?

Here goes a link to a fiddle I did. 这里有我做过的小提琴的链接。 https://jsfiddle.net/Lp1nv7wa/1/ https://jsfiddle.net/Lp1nv7wa/1/

If possible can you make the script verbose so I can try to understand the script. 如果可能的话,您可以使脚本变得冗长,以便我尝试理解该脚本。 ( no knowledge of javascript) (不懂JavaScript)

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {showDivs(slideIndex += n);}
function currentDiv(n) {showDivs(slideIndex = n);}

function showDivs(n) {
var i;
var x = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("thumb");
if (n > x.length) {slideIndex = 1}    
if (n < 1) {slideIndex = x.length} ;
for (i = 0; i < x.length; i++) {
 x[i].style.display = "none";  
}
for (i = 0; i < dots.length; i++) {
 dots[i].className = dots[i].className.replace(" thumb_on", "");
}
x[slideIndex-1].style.display = "block";  
dots[slideIndex-1].className += " thumb_on";
}

In your Javascript, there seems to be an error regarding your dots variable as you have no elements with thumb as a class name so I suggest commenting that out if it's not being used. 在您的Javascript中,关于您的dots变量似乎有错误,因为您没有将thumb作为类名的元素,因此,如果不使用它,建议将其注释掉。

One way you can achieve a fade-in-fade-out effect is by using CSS and opacity . 实现淡入淡出效果的一种方法是使用CSS和opacity In your CSS, you can add these lines: 在CSS中,您可以添加以下行:

.mySlides{
  position: absolute;   /* puts all the slides on top of each other */
  top: 0; left: 0;      /* and positions them all at the top left   */
  transition: all 1s;   /* this animates the change in the css      */
                        /* the 1s stands for 1 second, you may      */
                        /* change that to any duration              */
}

For your javascript, you should change all of the x[...].style.display into ones that tweaks opacity. 对于您的JavaScript,您应该将所有x[...].style.displayx[...].style.display不透明度的显示。

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = x.length} ;
  for (i = 0; i < x.length; i++) {
    x[i].style.opacity = 0; //sets the opacity to 0
  }
  x[slideIndex-1].style.opacity = 1; //sets the opacity to 1

  document.getElementById("slideNumber").innerHTML = slideIndex; 
  //the previous line finds the element with an id of slideNumber
  //and sets its content to the current slide index
}

To complete the slide numbering, add this line where you want your slide number to appear. 要完成幻灯片编号,请在您希望幻灯片编号显示的位置添加此行。

<span id="slideNumber"></span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM