简体   繁体   English

使用html CSS和javascript创建轮播时出现问题

[英]Issue creating a carousel with html CSS and javascript

I'm trying to create a carousel using just HTML, CSS and JS. 我正在尝试仅使用HTML,CSS和JS创建轮播。

It works, but not as well as I was hoping for. 它可以工作,但是不如我所希望的那样。

When it finishes one round of images, it takes roughly 8 seconds to start showing the images from the first to the last one, and then it stops again for several seconds and so on... 完成一轮图像处理后,大约需要8秒钟才能开始显示从第一张到最后一张的图像,然后又停止几秒钟,依此类推...

Additionally, the div containing the background-images is 100% width and 100vh height. 此外,包含背景图像的div的宽度为100%,高度为100vh。 I've tried to set the background properties like bg-repeat , bg-size , bg-position , but I can't manage to get the images to display well on the screen - They images become cropped when set background-size: cover , and become too small if I set background-size: contain; 我尝试设置背景属性,如bg-repeatbg-sizebg-position ,但是我无法使图像在屏幕上很好地显示-设置background-size: cover时,它们的图像会被裁剪background-size: cover ,如果我设置background-size: contain; ,它会变得太小background-size: contain; or another property. 或其他财产。

Can you please check this "working" demo? 您能否检查此“工作”演示? Thanks. 谢谢。

 var divi = document.querySelector(".divi"); srcArr = ["https://picsum.photos/id/237/200/300", "https://picsum.photos/id/238/200/300", "https://picsum.photos/id/239/200/300", "https://picsum.photos/id/240/200/300"]; var iter = 0; setInterval(function() { if (iter == (srcArr.length)) { iter = 0; } else { divi.style.backgroundImage = "url('" + srcArr[iter] + "')"; iter++; } }, 4000); 
 * { padding: 0; margin: 0 } .divi { width: 100%; height: 100vh; background-image: url("https://picsum.photos/id/240/200/300"); background-repeat: no-repeat; -webkit-background-size: cover; background-size: cover; } 
 <div class="divi"></div> 

As Tyler Roper says, when (iter == srcArr.length) iter go on zero value but didn't change the image, so for the next one they wait 4 seconds more. 正如泰勒·罗珀Tyler Roper)所说,当(iter == srcArr.length)时,它继续为零值,但没有改变图像,因此对于下一个图像,它们还要等待4秒。

 const divi = document.querySelector(".divi"), srcArr = ["https://picsum.photos/id/237/200/300", "https://picsum.photos/id/238/200/300", "https://picsum.photos/id/239/200/300", "https://picsum.photos/id/240/200/300"]; var iter = -1; setInterval(function() { iter = (iter + 1) % srcArr.length; divi.style.backgroundImage = "url('" + srcArr[iter] + "')"; }, 4000); 
 * { padding: 0; margin: 0 } .divi { width: 100%; height: 100vh; background-image: url("https://picsum.photos/id/240/200/300"); background-repeat: no-repeat; -webkit-background-size: cover; background-size: cover; } 
 <div class="divi"></div> 

Every four seconds, you run an if statement, and only if it evaluates to false do you change the image. 每四秒钟运行一次if语句,并且只有在其结果为false ,您才更改映像。 That means that each time it evaluates to true (when it gets to the end of the array), it has to run through two iterations to continue (which in this case takes 8 seconds). 这意味着每次它的计算结果为true (到达数组末尾),它都必须运行两次迭代才能继续(在这种情况下,需要8秒钟)。

Personally, I prefer to put things like this in functions, and using a recurring setTimeout over a setInterval . 就我个人而言,我更喜欢将这样的事情放在函数中,并在setInterval使用重复的setTimeout I've also returned that timeout in the event you want to stop the slideshow at some point. 如果您想在某个时间停止幻灯片放映,我也会返回该超时。

In regards to your background, that's a pretty broad question. 关于您的背景,这是一个相当广泛的问题。 For starters, I've just applied background-position: center; 首先,我刚刚应用了background-position: center; .

 const divi = document.querySelector(".divi"); const srcArr = ["https://picsum.photos/id/237/200/300","https://picsum.photos/id/238/200/300","https://picsum.photos/id/239/200/300", "https://picsum.photos/id/240/200/300"]; const timer = 1000; const nextImg = (imageArray, index) => { divi.style.backgroundImage = `url('${imageArray[index]}')`; index = ++index % imageArray.length; return setTimeout(()=>nextImg(imageArray, index), timer); }; const startSlideshow = imageArray => nextImg(imageArray,0); //Start slideshow const slideshow = startSlideshow(srcArr); //Stop slideshow //clearTimeout(slideshow); 
 * { padding: 0; margin: 0 } .divi { width: 100%; height: 100vh; background-repeat: no-repeat; -webkit-background-size: cover; background-size: cover; background-position: center; } 
 <div class="divi"></div> 

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

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