简体   繁体   English

鼠标移动时如何使图像变色?

[英]How to make image change color while mouse is moving over?

First off, sorry for the weird title, I don't really know a better way of describing it. 首先,对这个怪异的标题感到抱歉,我真的不知道一种更好的描述方式。

Basically I want the image to change color while the mouse is moving (hovering) over it, but for it to stop when the mouse is still, 基本上,我希望图像在鼠标移动(悬停)于其上方时改变颜色,但要使其在鼠标静止时停止,

While the mouse is stationary, color doesn't change, 鼠标静止不动时,颜色不会改变,

While the mouse is moving, color changes. 鼠标移动时,颜色会改变。

I know there is the hover attribute in CSS, but it only has 2 states, when the mouse is hovering over it, and when it is not, what i'm looking for is something a bit trickier. 我知道CSS中有hover属性,但是它只有2种状态,当鼠标悬停在其上时,当鼠标不在其上时,我正在寻找的东西有些棘手。

Hopefully that explains it :/ 希望能解释一下:/

Please try this: 请尝试以下方法:

document.getElementById("myDiv").onmousemove = function() {
  //Set random background color
  myDiv.style.backgroundColor = "#" + ((1 << 24) * Math.random() | 0).toString(16);
}

Demo: 演示:

 document.getElementById("myDiv").onmousemove = function() { //Set random background color myDiv.style.backgroundColor = "#" + ((1 << 24) * Math.random() | 0).toString(16); } 
 #myDiv { width: 150px; height: 150px; background-color: #ccc; text-align: center; line-height: 140px; } 
 <div id="myDiv">Hover ME !</div> 

You can achieve this with CSS animations, using Javascript to toggle a class when the mouse moves over the image, or stops doing so. 您可以使用CSS动画来实现此目的,可以使用Javascript在鼠标移到图像上或停止在图像上移动时切换类。

The image will need to be wrapped within another tag, if it isn't already, and then a pseudo-element will need to be added to that tag, positioned to sit directly on top of the image, with an initial opacity of 0 . 图像将需要包装在另一个标签中(如果尚未包装),然后需要向该标签添加一个伪元素,该元素应直接位于图像顶部,初始opacity0 We ensure the image is visible behind the pseudo-element by setting the mix-blend-mode property of the latter. 通过设置伪元素的mix-blend-mode属性,我们确保图像在伪元素后面可见。 When the mouse first moves over the image it is, optionally, converted to grayscale and the background-color of the pseudo-element begins animating. 当鼠标第一次在图像上移动时,可以选择将其转换为灰度,然后伪元素的background-color开始动画显示。 When the mouse stops moving, the timeout in the JavaScript adds a class to the parent element which sets the animation-play-state property of the pseudo-element to paused and, when the mouse is moved again, this class is removed. 当鼠标停止移动时,JavaScript中的超时会将一个类添加到父元素,该类将伪元素的animation-play-state属性设置为paused并且当再次移动鼠标时,将删除该类。

You can refine & tweak everything in this (eg, removing the image's filter , adding/removing keyframes , chaning the mix-blend-mode , adjusting the animation-duration ) just by editing the CSS, no need to touch the JavaScript. 您可以通过编辑CSS来优化和调整其中的所有内容(例如,删除图像的filter ,添加/删除keyframes ,更改mix-blend-mode ,调整animation-duration ),而无需触摸JavaScript。

 var figure=document.querySelector("figure"), img=figure.querySelector("img"), timer; img.addEventListener("mousemove",function(){ clearTimeout(timer); figure.classList.remove("paused"); setTimeout(function(){ figure.classList.add("paused"); },300); },0); 
 *{box-sizing:border-box;margin:0;padding:0;} figure{ display:inline-block; position:relative; } img{ vertical-align:middle; transition:-webkit-filter .25s linear 99999s,filter .25s linear 99999s; } img:hover{ -webkit-filter:grayscale(1); filter:grayscale(1); transition-delay:0s; } figure::after{ animation:colours 5s linear infinite; bottom:0; content:""; left:0; mix-blend-mode:overlay; opacity:0; pointer-events:none; position:absolute; right:0; top:0; transition:opacity .25s linear 99999s; } figure:hover::after{ opacity:.75; transition-delay:0s; } figure.paused::after{ animation-play-state:paused; } @keyframes colours{ 0%{background:#f00;} 16.667%{background:#ff0;} 33.333%{background:#0f0;} 50%{background:#0ff;} 66.667%{background:#00f;} 83.333%{background:#f0f;} 100%{background:#f00;} } 
 <figure> <img src="https://unsplash.it/500/500/?random"> </figure> 

I'm posting here as I need almost something like this. 我在这里发布,因为我几乎需要像这样的东西。 I need to change the back ground position on each mouse move. 我需要在每次鼠标移动时更改背景位置。 Image is set as background, here is the image: 图像被设置为背景,这是图像:

http://pbdlbd.org/ipositive/wp-content/uploads/2015/02/one10.jpg http://pbdlbd.org/ipositive/wp-content/uploads/2015/02/one10.jpg

And I want to move the background position on each mouse move. 我想在每次鼠标移动时移动背景位置。 This image has 4 parts(height of each part is 523px) and first it will show the top portion and after mouse move over it will show the 2nd portion and on another mouse move it will show 3rd portion and after 4th part it will repeat for further mouse move over it. 此图像有4个部分(每个部分的高度为523px),首先将显示顶部,将鼠标移到上面将显示第二部分,在另一个鼠标上将显示第三部分,在第四部分之后将重复显示鼠标移到它上面。 After mouse is removed from the image, it will show the default top portion of the image. 从图像中删除鼠标后,它将显示图像的默认顶部。

Something like this: 像这样:

document.getElementById("#ipos .flex_cell").onmousemove = function() {
  //Set background position 523px bottom on each mouse move
  #ipos .flex_cell.style.background-position = center -523px (here i can't make it so that it changes to -1046px by code);
}

Here is the site 是网站

TIA TIA

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

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