backgroundImage在我的file.js中不起作用。

huangapple go评论71阅读模式
英文:

backgroundImage Not working in my file.js

问题

I'm trying to use element.backgroundImage on JS but never change the background.
我正在尝试在JS中使用element.backgroundImage,但背景没有改变。

I changed the file scss to test if it worked and it did but then it was fixed and seems like file js not working backgroundImage在我的file.js中不起作用。
我更改了scss文件以测试它是否有效,它有效了,但后来它被固定了,似乎JS文件不起作用 backgroundImage在我的file.js中不起作用。

Is not a problem with the link on the html or something like that 'cause everything else is working...
这不是HTML中的链接问题或类似的问题,因为其他一切都在正常工作...

Here's the sass:
这是Sass的代码:

.body-movie {
  width: 100vw;
  min-height: 100vh;
  height: fit-content;
  position: relative;
  /* background-image = url('../scss/img/image-1.jpg') */
  background-position: center;
  background-size: cover;
  background-attachment: fixed;
}

Here the js:
这是JS的代码:

const movieSelect = {
  name: 'Saw',
  back: '../scss/img/image-2.jpg'
}
const back = document.querySelector('.body-movie');
back.style.backgroundImage = `${movieSelect.back}`;

I want the image-2.jpg in the background but image-1.jpg still there even though it is commented.
我希望背景是image-2.jpg,但即使它被注释掉,image-1.jpg仍然在那里。

英文:

I'm trying to use element.backgroundImage on JS but never change the background.

I changed the file scss to test if it worked and it did but then it was fixed and seems like file js not working backgroundImage在我的file.js中不起作用。

Is not a problem with the link on the html or something like that 'cause everything else is working...

Here's the sass:

.body-movie {
  width: 100vw;
  min-height: 100vh;
  height: fit-content;
  position: relative;
  /* background-image = url('../scss/img/image-1.jpg') */
  background-position: center;
  background-size: cover;
  background-attachment: fixed;
}

Here the js:

const movieSelect = {
  name: 'Saw',
  back: '../scss/img/image-2.jpg'
}
const back = document.querySelector('.body-movie');
back.style.backgroundImage = `${movieSelect.back}`;

I want the image-2.jpg in the background but image-1.jpg still there even though it is commented.

答案1

得分: 1

如果即使已被注释掉,图像1仍然存在,那意味着您需要以某种方式刷新页面或重新编译您的Sass。

至于JS部分,它不起作用是因为您忽略了字符串中的 "url(" 部分。所以您需要:

back.style.backgroundImage = `url(${movieSelect.back})`;
英文:

If image-1 is still there even though its commented out, then that means you need to refresh or page or recompile your sass in some way.

For the JS, it's not working because you are neglecting to include the "url(" portion of the string. So you would need

back.style.backgroundImage = `url(${movieSelect.back})`;

huangapple
  • 本文由 发表于 2023年3月4日 04:52:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定