简体   繁体   English

背景图像不随 setInterval 改变

[英]Background image not changing with setInterval

I try to change background image using setInterval .我尝试使用setInterval更改背景图像。 Only the image changes just once.只有图像只改变一次。 I have tried to change width value instead and it works.我试图改变宽度值,它可以工作。

var hero = document.getElementById('hero')
console.log(hero)

var heroBg = hero.style.backgroundImage;
console.log(heroBg)

function change() {
  console.log('this is showing in the console')

  if (hero.style.background === 'url(./images/image2.jpg)') {
    console.log('this is not showing in the console')
    hero.style.background = 'url(./images/image1.jpg)'
    console.log('testcondition1')
  } else {
    console.log('this is showing in the console')
    hero.style.background = 'url(./images/image2.jpg)'
  }
}

setInterval(change, 3000);




EDIT: this works编辑:这有效

    if(hero.style.backgroundImage == 'url("./images/image2.jpg")'){
        console.log('test1')
        hero.style.backgroundImage = 'url("./images/image1.jpg")'
        console.log('testcondition1')
    }else{
        console.log('test2')
        hero.style.backgroundImage='url("./images/image2.jpg")'
    }

I have tried to use double quotes inside the parenthesis and this works.我试图在括号内使用双引号,这很有效。 The double quotes outside and single quote inside does not.外面的双引号和里面的单引号没有。

Instead of comparing the background CSS property that may be inconsistent across browsers, you may want to use a boolean flag instead.与其比较可能跨浏览器不一致的背景 CSS 属性,不如使用 boolean 标志。

let flag = true;
function change() {
  console.log('this is showing in the console')
  if (flag) {
    console.log('this is not showing in the console')
    hero.style.background = 'url(./images/image1.jpg)'
    console.log('testcondition1')
  } else {
    console.log('this is showing in the console')
    hero.style.background = 'url(./images/image2.jpg)'
  }
  flag = !flag;
}

I would utilize data attributes to store the state of the hero and just flip between the states.我会利用数据属性来存储英雄的 state 并在状态之间切换。

You can use the value of currentState inside of change , to determine what to do.您可以使用change内部的currentState的值来确定要做什么。

Do not attempt to run style information through a conditional, because you are going to have a bad time.不要试图通过条件来运行样式信息,因为你会度过一段糟糕的时光。

 const maxStates = 2 const hero = document.getElementById('hero') function change() { let currentState = parseInt(hero.dataset.state || 0, 10) currentState = (currentState + 1) % maxStates hero.dataset.state = currentState } setInterval(change, 2000); // Update every 2 seconds.
 #hero { width: 200px; height: 287px; background-color: rgba(0, 0, 0, 0); background-repeat: no-repeat; background-position: top left; background-image: url('https://placekitten.com/200/287'); } #hero[data-state="1"] { background-image: url('https://placekitten.com/200/286'); }
 <div id="hero"></div>

in if change background to backgroundImage如果将背景更改为backgroundImage

hero.style.background

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

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