简体   繁体   English

鼠标悬停更改图像颜色

[英]Mouseover Change Image Color

I have a big grid of images. 我的图像网格很大。 When a user mouseovers an image I want the image to tint blue 0000FF. 当用户将鼠标悬停在图像上时,我希望图像具有蓝色0000FF。 Is there a way to do this in JS or jquery? 有没有办法在JS或jquery中做到这一点? Ideally, I wouldn't have to apply a class to each image. 理想情况下,我不必对每个图像都应用一个类。 This treatment should affect all images on the screen. 这种处理应影响屏幕上的所有图像。

After searching the forums here and elsewhere I learned that some folks use a div over the image that has a color and opacity, but how would I apply that to all img? 在这里和其他地方的论坛中搜索后,我了解到有些人在具有颜色和不透明度的图像上使用了div,但是如何将其应用于所有img?

Another thing I keep seeing is paintbrushJS and pixastic but I don't know how to make those work for this purpose. 我一直看到的另一件事是paintbrushJS和pixastic,但是我不知道如何使它们达到这个目的。

Here's the page I'm working on: http://www.rollinleonard.com/elements/ 这是我正在处理的页面: http : //www.rollinleonard.com/elements/

EDIT: the images need to be be clickable so the div can't obstruct the linked img. 编辑:图像需要是可单击的,以便div不会阻塞链接的img。 Is there a way to click through the div or put the div below or something? 有没有办法点击div或将div放在下方或其他内容? Some solutions offered don't use a div but I can't figure them out. 提供的某些解决方案不使用div,但我无法弄清楚。

Thanks! 谢谢! Rollin 罗林

This is how you're gonna want to do it: http://jsfiddle.net/ztKJB/1/ 这就是您要这样做的方式: http : //jsfiddle.net/ztKJB/1/

Javascript / jQuery: Javascript / jQuery:

$overlay = $('#overlay');

$('img').bind('mouseenter', function () {
    $this = $(this);
    if ($this.not('.over')) {
        $this.addClass('over');
        $overlay.css({
            width  : $this.css('width'),
            height : $this.css('height'), 
            top    : $this.offset().top + 'px',
            left   : $this.offset().left + 'px',
        }).show();
    }
}).bind('mouseout', function () {
    $(this).removeClass('over');
});

CSS: CSS:

#overlay {
    display: block;
    position: absolute;
    background-color: rgba(0, 0, 255, 0.5);
    top: 0px;
    left: 0px;
    width: 0px;
    height: 0px;
}

HTML: HTML:

<div id="overlay"></div>
<img src="http://www.rollinleonard.com/elements/zzzthumbs/JPEG/rgb-dots-olan3.jpg" width="150" height="150">
<img src="http://www.rollinleonard.com/elements/zzzthumbs/JPEG/rgb-dots-olan2.jpg" width="150" height="150">
<img src="http://www.rollinleonard.com/elements/zzzthumbs/JPEG/IMG_3291.jpg" width="225" height="150">
<img src="http://www.rollinleonard.com/elements/zzzthumbs/JPEG/1153-1188.jpg" width="200" height="150">
<img src="http://www.rollinleonard.com/elements/zzzthumbs/JPEG/P1010036.jpg" width="200" height="150">
<img src="http://www.rollinleonard.com/elements/zzzthumbs/JPEG/dressRehearsal.jpg" width="267" height="150">
<img src="http://www.rollinleonard.com/elements/zzzthumbs/JPEG/sinWave.jpg" width="225" height="150"
<img src="http://www.rollinleonard.com/elements/zzzthumbs/JPEG/mockUp2.jpg" width="225" height="150">
<img src="http://www.rollinleonard.com/elements/zzzthumbs/JPEG/PICT0453.jpg" width="113" height="150">

The idea of using a div over the image would work. 在图像上使用div的想法将起作用。 You can generate the div on-the-fly as needed (or generate a hidden div to reuse throughout the page), and position it over the image during the onmouseover event: 您可以根据需要即时生成div(或生成隐藏的div以在整个页面中重复使用),并在onmouseover事件期间将其放在图片上:

$('img').mouseover(function() {
    // generate a div
    // position over current image
});

Append a span inside each anchor, and adjust it's opacity on hover: 在每个锚点内附加一个span ,并在悬停时调整其不透明度:

<script>
$(function() {
    $('a').each(function() {
        $(this).appendChild('<span class="overlay" />');
    });
});
</script>

<style>    
    a {
        position: relative;
    }

    a .overlay {
        background-color: #00f;
        height: 100%;
        left: 0px;
        opacity: 0;
        position: absolute;
        top: 0px;
        width: 100%;
    }

    a:hover .overlay {
        opacity: 0.4; /* adjust to suit */
    }
</style>

Note: you'll need to adjust your styles so the anchors are being float ed rather than the images. 注意:您需要调整样式,以使锚点float而不是图像float

If you wanted a fade in/out, you could either use CSS3 transitions or hide the span initially and use a jQuery mouseover event to fade it in: 如果您想要淡入/淡出,则可以使用CSS3过渡或最初隐藏span并使用jQuery mouseover事件将其淡入:

$('a').each(function() {
    $(this).appendChild($('<span class="overlay" />').hide()).hover(function() {
        $(this).find('.overlay').fadeIn(500);
    }, function() {
        $(this).find('.overlay').fadeOut(1000);
    });
});

This jquery plugin should do the thing you asked pretty well. 这个jQuery插件应该可以很好地完成您所要求的事情。 (tancolor.js) (tancolor.js)

$("#myImageID").tancolor({mode: "blue"});

There's an interactive demo . 有一个交互式演示 You can play around with it. 您可以尝试一下。

Check out the documentation on the usage, it is pretty simple. 查看有关用法的文档,这很简单。 docs 文档

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

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