简体   繁体   English

如何通过单击更改按钮的图像?

[英]How can I let the image of a button change by clicking it?

I'd like to have the image of a button changing to another picture of the same size, and after a certain amount of time changed back. 我想将按钮的图像更改为另一张相同大小的图片,并在一定时间后将其更改回去。 For example, you click on a picture of a house, it will change to the sky, then without any action back to the house. 例如,您单击房屋的图片,它将变为天空,然后无需执行任何操作即可返回房屋。 The time between the 2nd picture changing back to the 1st should not prohibit the button being clicked again. 从第二张图片变回第一张图片之间的时间不应禁止再次单击该按钮。

So, since I'm fairly new, I searched a lot but couldn't find something regarding my problem, or something I understood. 因此,由于我还很新,我进行了很多搜索,但找不到关于我的问题或我理解的东西。 I made a button in HTML, linked a class in CSS which has the URL of the image that's supposed to act as the button. 我用HTML制作了一个按钮,在CSS中链接了一个类,该类具有应该用作按钮的图像的URL。 So far, it works. 到目前为止,它仍然有效。 How do I continue? 我该如何继续?

 -> HTML
<button class="buttonn" > 
-> CSS
.buttonn {
 background-image:url([Link to the image]);

You can consider transition and delay on active state: 您可以考虑活动状态的过渡和延迟:

 button { padding:20px; border:none; background-image: url(https://picsum.photos/id/1/200/200), url(https://picsum.photos/id/10/200/200); background-size:0 0,cover; /* The top one invisible*/ background-position:center; background-repeat:no-repeat; transition: 0s 1s; } button:active { background-size:100% 100%,cover; transition: 0s; } 
 <button>click me</button> 

Like this too: 也是这样:

 button { padding:20px; border:none; background-image: url(https://picsum.photos/id/10/200/200); background-size:cover; background-position:center; background-repeat:no-repeat; transition: 0s 1s; } button:active { background-image: url(https://picsum.photos/id/1/200/200); transition: 0s; } 
 <button>click me</button> 

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

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