【问题标题】:Repeatedly change background colour of picture onMouseOver反复改变图片onMouseOver的背景颜色
【发布时间】:2018-11-18 01:29:35
【问题描述】:

我有一张图片,希望它的背景发生变化,并从整个光谱中顺序地反复获取随机颜色,直到用户的鼠标退出图片。我想解决方案应该使用 setInterval (see this) 并且以下内容会有所帮助:

var red = Math.round(Math.random() * 255);
var green = Math.round(Math.random() * 255);
var blue = Math.round(Math.random() * 255);
var bg = "background-color: rgb(" + red + "," + green + "," + blue + ");";
x.style = bg;

Here is a fiddle trying to implement what I have in mind: 第一个笑脸应该在onMouseOver 上改变颜色并在onMouseOut 上恢复正常。

这是我的想法:我想实现FontAwesome.com do on their logo at their footer:它会在鼠标悬停时改变颜色,否则会停止。但这不是图片,而是字体(?)。相反,我有一个透明的徽标,我想动态更改背景,以便它复制 Fontawesome 的精彩技巧。有什么想法吗?

* 更新 *

我将根据社区的回答在下面发布我的问题的详细解决方案。看起来我遵循了 Leo 的方式,但 Rakibul 的解决方案也很有效。

【问题讨论】:

  • 您可以使用带有关键帧的 css3 动画。见stackoverflow.com/questions/10843880/…
  • 谢谢!我确实知道如何将图片的背景更改为 A 颜色 (see here)。相反,我要求重复应用颜色生成函数 onMouseOver,这将使图片的背景采用多种颜色,然后在鼠标输出上停止。提前谢谢你。
  • 您好,您可以在hover上使用animation-play-state: pause
  • 谢谢@LeoCaseiro,yes that is towards the right direction。你能用一个完整的示例代码来回复我发布的问题的参数吗?提前致谢。

标签: javascript html css nested


【解决方案1】:

我实现了我想要的。

我希望当用户将鼠标悬停在徽标上时“漂亮”地改变颜色(就像魔术一样,类似于FontAwesome's logo at their footer)。

.OAlogo {
  background-color: transparent;
  animation-play-state: paused;
}

.OAlogo:hover {
  animation: colorReplace 10s infinite;
  animation-timing-function: linear;
  animation-direction: alternate;
}

@keyframes colorReplace {
  0% {
    background-color: rgb(44, 132, 231);
  }
  10% {
    background-color: rgb(61, 192, 90);
  }
  20% {
    background-color: rgb(255, 211, 59);
  }
  30% {
    background-color: rgb(253, 136, 24);
  }
  40% {
    background-color: rgb(243, 129, 174);
  }
  50% {
    background-color: rgb(34, 138, 230);
  }
  60% {
    background-color: rgb(62, 192, 89);
  }
  70% {
    background-color: rgb(255, 211, 59);
  }
  80% {
    background-color: rgb(71, 193, 86);
  }
  90% {
    background-color: rgb(253, 126, 20);
  }
  100% {
    background-color: rgb(233, 109, 132);
  }
}
<img class="OAlogo" src="http://www.stouras.com/OperationsAcademia.github.io/images/OA-logo-solo.png" style="background: black;" width="100">

【讨论】:

  • 对于这个结果,需要使他/她的徽标透明,然后将其绘制在黑色背景上(在我的情况下)。当用户悬停时,彩色动画开始播放。
【解决方案2】:

您必须使用所需的时间间隔声明setInterval()(在下面的示例中设置为 500),以便在确定的时间间隔内随机更改颜色。而onmouseover 用于简单地检测图像上的悬停,然后随机设置颜色。最后,当检测到onmouseout 时,颜色变为无颜色。

var randomColor = function() {
  var r = Math.floor(Math.random() * 12);
  var g = Math.floor(Math.random() * 128);
  var b = Math.floor(Math.random() * 100);
  return "#" + r + g + b;
};

var colorChange;

document.getElementById("myImage").onmouseover = function() {
  colorChange = setInterval(function() {
    document.getElementById("myImage").style.backgroundColor = randomColor();
  }, 500);
};

document.getElementById("myImage").onmouseout = function() {
  this.style.backgroundColor = "";
  clearInterval(colorChange);
};
<img id="myImage" src="https://www.w3schools.com/jsref/smiley.gif" alt="Smiley">

【讨论】:

  • 这是伟大的拉基布尔!我接受它,但我也很喜欢 Leo 的回答。我可能会稍微修改您的代码,使颜色的过渡是线性/平滑的(因为这将转到一个徽标),并稍后发布另一个回复,以防将来的用户发现这个问题。
【解决方案3】:

您可以使用setInterval 函数反复运行您的代码。您的代码中还有一些我已修复的小错误。在此处查看您更新的小提琴:https://jsfiddle.net/zvebco3r/3/

您可以将间隔时间(当前为 25 毫秒)更改为您想要的长度。

HTML:

<img id="img" src="https://www.w3schools.com/jsref/smiley.gif" alt="Smiley" width="32" height="32">

JS:

var img = document.getElementById('img');

img.onmouseover = function() {
    changeIt = setInterval(function(){
       var red = Math.floor(Math.random() * 255);
       var green = Math.floor(Math.random() * 255);
       var blue = Math.floor(Math.random() * 255);
       img.style.backgroundColor="rgb("+red+","+green+","+blue+")";
    },25);
}

img.onmouseout = function() {
    this.style.backgroundColor="transparent";
    clearInterval(changeIt);
}

【讨论】:

    【解决方案4】:

    使用 CSS 动画更改颜色并在悬停时使用 animation-play-state: pause

     .button {
        width:100px;
        height:20px;
        background-color: red;
        animation: colorReplace 5s infinite;
      }
    
      .button:hover {
        animation-play-state: paused;
      }
    
    
    @keyframes colorReplace
    {
      0%   {background-color:red;}
      30%  {background-color:green;}
      60%  {background-color:blue;}
      100%   {background-color:red;}
    }
    &lt;input type="submit" value="submit" class="button" /&gt;

    【讨论】:

    • Leo 有点着急,他上面的想法奏效了。将发布一个完整的答案,该答案完全适用于我所要求的答案。
    猜你喜欢
    • 1970-01-01
    • 2022-11-12
    • 2016-08-21
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多