简体   繁体   English

javascript imageslider setinterval

[英]javascript imageslider setinterval

var imagecount = 1;
var total = 3;

I'd like to know why I can't make an imageslider this way. 我想知道为什么我不能用这种方式制作图像imageslider I have pictures saved such as studentbild1 , studentbild2 and studentbild3 . 我保存了诸如studentbild1studentbild2studentbild3 How do I get those to show in a setinterval like this. 我该如何将它们显示在这样的setinterval

window.setInterval(function slideA() {
    var image = document.getElementById('studentbild');
    imagecount = imagecount + 1;
    if(imagecount > total){ imagecount = 1;}
    if(imagecount < 1){ imagecount = total;}
    image.style.backgroundImage = 'url("studentbild" + imagecount + ".jpg")';
},1000);

Remove the inner double-quotes, replace them with single quotes: 删除内部的双引号,将其替换为单引号:

image.style.backgroundImage = 'url("studentbild' + imagecount + '.jpg")';

to give

url("studentbild1.jpg")

etc. 等等

 var imagecount = 0; var total = 3; window.setInterval(function slideA() { var image = document.getElementById('studentbild'); imagecount = imagecount + 1; if (imagecount > total) { imagecount = 1; } if (imagecount < 1) { imagecount = total; } image.style.backgroundImage = 'url("http://placehold.it/' + imagecount + '00")'; }, 1000); 
 #studentbild { width: 300px; height: 300px; } 
 <div id="studentbild"></div> 

解决此问题(在加号附近添加单引号)

image.style.backgroundImage = 'url("studentbild' + imagecount + '.jpg")';

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

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