简体   繁体   English

如何更改背景图像

[英]How to change background image

I am a beginner in JS and am trying to create a simple function where I click on a button and the background image changes but it doesn't work (I was trying to recreate a simpler image slider).我是 JS 的初学者,正在尝试创建一个简单的 function,我单击一个按钮,背景图像发生变化,但它不起作用(我试图重新创建一个更简单的图像滑块)。 I can't understand why.我不明白为什么。

This is my JS.这是我的 JS。 I have used the absolute link hoping it could fix the problem and the alert to see whether the if/else statement was working.我使用了绝对链接,希望它可以解决问题并发出警报以查看 if/else 语句是否有效。

const btnLeft = document.getElementById("btn-left");
const btnRight = document.getElementById("btn-right");

btnLeft.addEventListener("click", whenClicked);
btnRight.addEventListener("click", whenClicked);

function whenClicked() {
if (btnLeft.clicked) {
document.getElementById("img2").style.backgroundImage = 
"url('https://images.unsplash.com/photo-1494783367193-149034c05e8f?ixlib=rb- 
1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&dl=diego-jimenez-A-NVHPka9Rk-unsplash.jpg')";
} else alert("no click!");

}

This is my HTML:这是我的 HTML:

<div class="slide-container">

    <div class="slide" id="img1">
       <button id="btn-left"> <i class="fas fa-arrow-circle-left fa-3x " ></i></button>
        <button id="btn-right"><i class="fas fa-arrow-circle-right fa-3x " ></i></button>
    </div>

    <div class="slide" id="img2">

    </div>

</div>

This is the relevant CSS.这是相关的 CSS。

 .slide-container {
display: flex;  
 }

.slide:nth-child(1) {
background-image: url(../img/img1.jpg);
height: 789px;
width: 100%; 
flex-shrink: 0;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}

.slide:nth-child(2) {
background-image: url(../img/img2.jpg);
height: 789px;
width: 100%; 
flex-shrink: 0;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}

#btn-left {
position: absolute;
top: 400px;
}
#btn-right {
position: absolute;
left: 1380px;
top: 400px;
}

Any help is deeply appreciated, I have been going crazy for hours.非常感谢任何帮助,我已经疯了好几个小时了。 Thanks.谢谢。

EDIT.编辑。 After David784's help, I now have this:在 David784 的帮助下,我现在有了这个:

const btnLeft = document.getElementById("btn-left");
const btnRight = document.getElementById("btn-right");


btnLeft.addEventListener("click", whenClicked);
btnRight.addEventListener("click", whenClicked);

function whenClicked(e) {
if (e.currentTarget === btnLeft ) {
document.getElementById("img1").style.backgroundImage = 
"url('https://images.unsplash.com/photo-1494783367193-149034c05e8f? 
ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&dl=diego-jimenez- 
A-NVHPka9Rk-unsplash.jpg')";
} 
else alert("no click!");
}

and it works.它有效。 But then when I try to add the same for the right button, such as但是当我尝试为右键添加相同的按钮时,例如

function whenClicked(e) {
if (e.currentTarget === btnRight ) {
document.getElementById("img1").style.backgroundImage = 
"url('https://images.unsplash.com/photo-1494783367193-149034c05e8f? 
ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&dl=diego- 
jimenez-A-NVHPka9Rk-unsplash.jpg')";
} 
else alert("no click!");
}

the left button stopped working.左键停止工作。 Why?为什么?

Your question makes it sound as if the backgroundImage part is where the problem is, but I can't seem to duplicate (see working snippet below).您的问题听起来好像backgroundImage部分是问题所在,但我似乎无法复制(请参阅下面的工作片段)。

However I did have trouble with the if (btnLeft.clicked) ...I'm not sure Element.clicked is a thing...at least I've never seen it before.但是我确实遇到了if (btnLeft.clicked)的问题……我不确定Element.clicked是不是一件事……至少我以前从未见过。 When I changed it to e.currentTarget===btnLeft , it just worked.当我将其更改为e.currentTarget===btnLeft时,它就起作用了。 (See Event.currentTarget for more info). (有关详细信息,请参阅Event.currentTarget )。

 const btnLeft = document.getElementById("btn-left"); const btnRight = document.getElementById("btn-right"); btnLeft.addEventListener("click", whenClicked); btnRight.addEventListener("click", whenClicked); function whenClicked(e) { if (e.currentTarget === btnLeft) { document.getElementById("img2").style.backgroundImage = "url('https://images.unsplash.com/photo-1494783367193-149034c05e8f?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&dl=diego-jimenez-A-NVHPka9Rk-unsplash.jpg')"; } else alert("no click;"); }
 .slide-container { display: flex; }.slide:nth-child(1) { background-image: url(../img/img1.jpg); height: 789px; width: 100%; flex-shrink: 0; background-size: cover; background-position: center; background-repeat: no-repeat; }.slide:nth-child(2) { background-image: url(../img/img2.jpg); height: 789px; width: 100%; flex-shrink: 0; background-size: cover; background-position: center; background-repeat: no-repeat; } #btn-left { position: absolute; top: 400px; } #btn-right { position: absolute; left: 1380px; top: 400px; }
 <div class="slide-container"> <div class="slide" id="img1"> <button id="btn-left"> <i class="fas fa-arrow-circle-left fa-3x " >left</i></button> <button id="btn-right"><i class="fas fa-arrow-circle-right fa-3x " >right</i></button> </div> <div class="slide" id="img2"> </div> </div>

*Edit: to address the comment about CSS, this is addressed by CSS Specificity rules . *编辑:解决关于 CSS 的评论,这是由CSS 特殊性规则解决的。 Which states:其中指出:

Inline styles added to an element (eg, style="font-weight: bold;") always overwrite any styles in external stylesheets, and thus can be thought of as having the highest specificity.添加到元素的内联 styles(例如 style="font-weight: bold;")总是覆盖外部样式表中的任何 styles,因此可以认为具有最高的特异性。

When doing element.style.backgroundImage , this creates an inline style, which will override.执行element.style.backgroundImage时,这会创建一个内联样式,该样式将被覆盖。 (with the exception I think of !important rules, which you should almost never use anyway) (除了我想到的!important规则,你几乎不应该使用它)

To address the question about passing the event into the event handler: this is a very helpful tool when using events, called the event object解决有关将事件传递到事件处理程序的问题:这是使用事件时非常有用的工具,称为事件 object

e.target is incredibly useful when you want to set the same event handler on multiple elements and do something to all of them when an event occurs on them.当您想在多个元素上设置相同的事件处理程序并在它们上发生事件时对所有元素执行某些操作时,e.target 非常有用。

So the event object can be named pretty much whatever you want.所以事件 object 可以任意命名。 You could call it e , event , evnt , or whatever.您可以将其称为eeventevnt或其他名称。 But it will give you access to a lot of very useful information, like the target of the click, the x/y coordinates of the click, etc.但它会让您访问很多非常有用的信息,例如点击的target 、点击的 x/y 坐标等。

Or in this case currentTarget , which is slightly different than target , gives us access to the element that the event handler is actually attached to.或者在这种情况下currentTargettarget略有不同,它使我们能够访问事件处理程序实际附加到的元素。 (here the target would most likely be the i tag inside the button tag). (这里的target很可能是button标签内的i标签)。

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

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