[英]Continuously loop colors on div using javascript on mouseenter
I have a div on a page and I would like to continuously cycle through a set of colors using javascript . 我在页面上有一个div,我想使用javascript 不断循环显示一组颜色 。
I've seen a number of articles on Stack Overflow about continuous loops to show information using arrays and javascript yet I have had trouble trying to implement it into my own project. 我已经在Stack Overflow上看到了很多有关连续循环的文章,这些连续循环使用数组和javascript显示信息 ,但是在尝试将其实现到自己的项目中时遇到了麻烦。
My HTML: 我的HTML:
<div id="box" style="background:grey;" onmouseenter="change()"></div>
and the closest JS solution I can find: 和我能找到的最接近的JS解决方案:
var change = function() {
colors = ['#00b0e2', '#e3144e', '#15e39b'];
count = -1;
return function() {
return colors[++count % colors.length];
}
document.getElementById('box').style.background = colors; // or should this be **colors[]**?
}
I understand what is happening up until the return function but then I am having trouble understanding how to inject the color into the html? 我知道直到返回函数之前发生了什么,但是然后我很难理解如何将颜色注入html?
Any help or tips would be appreciated thanks. 任何帮助或技巧将不胜感激,谢谢。
I think you are really close, but are missing a couple of key things. 我认为您真的很亲密,但是缺少一些关键的东西。
Firstly , when you say onmouseover="change()"
that means that it will run change()
every time the mouseover runs unlike using addEventListener(change())
which would run the function returned by change
as the event handler. 首先 ,当你说onmouseover="change()"
,这意味着它将运行change()
每次鼠标悬停不像使用运行时addEventListener(change())
这将运行由返回的功能change
作为事件处理程序。
Secondly to change the element, all you need to do is get a handle on the element and set the background. 其次,要更改元素,您所需要做的就是获取元素上的手柄并设置背景。
The code below does what I think you were trying to do but more simply. 下面的代码可以简化我所做的工作。 I hope it helps. 希望对您有所帮助。
// setup our colors and state once colors = ['#00b0e2', '#e3144e', '#15e39b']; count = -1; var change = function(element) { element.style.background = colors[++count % colors.length]; }
<!-- Pass in the element when creating the change listener --> <div id="box" style="background:grey;" onmouseenter="change(this)"> Give our box some contents so we can see it. </div>
Explanation: 说明:
the basic concept behind the loop is that we have a count
that tells us colors[count]
is currently active. 循环背后的基本概念是,我们有一个count
来告诉我们colors[count]
当前处于活动状态。
When a mouseover
happens, three things happen in the one line. 当发生mouseover
时,一行中会发生三件事。
++count
: this adds 1
to count
, but unlike count++
, it does so before the value is used. ++count
:这会给count
1
,但与count++
不同,它是在使用值之前这样做的。 Meaning that the first time a mouseover
occurs, the value of count
is 0
表示第一次发生mouseover
时, count
值为0
count % colors.length
: This just lets us wrap around to the first color once it has hit the last color. count % colors.length
:一旦到达最后一种颜色,我们就可以绕到第一种颜色。 The %
( modulus ) operator gives the remainder. %
( 模数 )运算符给出余数。 a % b
will return the remainder after dividing a / b
. a % b
将除以a / b
后将返回余数。 If count = 0
, count % 3
yields 0, but if count = 4
, count % 3
yields 1. You can read more about this and other Arithmetic operators on MDN 如果count = 0
,则count % 3
产生0,但是如果count = 4
, count % 3
产生1。您可以在MDN上阅读有关此运算符和其他算术运算符的更多信息
element.style.background = colors[...]
: This sets the background
css attribute to the color we selected in the last step. element.style.background = colors[...]
:这会将background
css属性设置为我们在上一步中选择的颜色。
so to put it all together, here is the change
function broken out into 3 lines. 综上所述,这里的change
功能分为3行。
var change = function(element){
count++; //increment count before using it.
var our_color = count % colors.length; // wrap properly
element.style.background = colors[our_color];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.