英文:
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
我更改了scss文件以测试它是否有效,它有效了,但后来它被固定了,似乎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
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})`;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论