【问题标题】:How can I let the image of a button change by clicking it?如何通过单击来更改按钮的图像?
【发布时间】:2019-12-03 02:47:47
【问题描述】:

我想将按钮的图像更改为另一张相同大小的图片,并在一定时间后变回。例如,你点击一个房子的图片,它会变成天空,然后没有任何动作回到房子。第 2 张图片变回第 1 张图片之间的时间不应阻止按钮再次被点击。

所以,由于我是新手,所以我进行了很多搜索,但找不到有关我的问题或我理解的内容。我在 HTML 中制作了一个按钮,在 CSS 中链接了一个类,该类具有应该充当按钮的图像的 URL。到目前为止,它有效。 我该如何继续?

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

【问题讨论】:

    标签: html css image button onclick


    【解决方案1】:

    您可以考虑活动状态的转换和延迟:

    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;
    }
    &lt;button&gt;click me&lt;/button&gt;

    也这样:

    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;
    }
    &lt;button&gt;click me&lt;/button&gt;

    【讨论】:

    • @DumbQuestionButAnswerAnywayPle 你肯定有更多的 CSS 参与,如果你能告诉我你的完整代码我可以帮助,没有困难
    • @DumbQuestionButAnswerAnywayPle 代码中没有什么复杂的,所以我不认为这是缺乏支持之类的。你能至少给我看一下整个代码的截图吗?甚至像 body 这样的 HTML 标准 sutff 也会产生问题
    • @DumbQuestionButAnswerAnywayPle 和 CSS?
    • @DumbQuestionButAnswerAnywayPle 尝试添加引号 url("https://picsum.photos/id/10/200/200") 还检查开发选项卡以查看 CSS 是否正在加载,可能路径不正确。
    猜你喜欢
    • 2012-08-18
    • 2020-12-30
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 2019-12-23
    • 1970-01-01
    相关资源
    最近更新 更多