简体   繁体   English

jQuery图像悬停颜色叠加

[英]jQuery image hover color overlay

I can't seem to find any examples of this having been done anywhere on the internet before but here is what I am going to attempt to do...I'm trying to go about the cleanest possible way of laying this out. 我似乎找不到以前在互联网上任何地方进行过的操作的任何示例,但这是我将要尝试做的...我正在尝试着以最简洁的方式进行此操作。

So I have an image gallery where the images are all different sizes. 因此,我有一个图片库,其中的图片大小各不相同。 I want to make it so that when you mouseover the image, it turns a shade of orange. 我要制作它,以便当您将鼠标悬停在图像上时,它会变成橙色阴影。 Just a simple hover effect. 只是一个简单的悬停效果。

I want to do this without using an image swap, otherwise I'd have to create an orange colored hover-image for each individual gallery image, I'd like this to be a bit more dynamic. 我想在不使用图像交换的情况下执行此操作,否则我必须为每个单独的图库图像创建一个橙色的悬停图像,我希望它更具动态性。

My plan is just to position an empty div over the image absolutely with a background color, width and height 100% and opacity: 0. Then using jquery, on mouseover I'd have the opacity fade to 0.3 or so, and fade back to zero on mouseout. 我的计划是将一个空的div完全放在背景颜色,宽度和高度100%且不透明度为0的图像上。然后使用jQuery,在鼠标悬停时我将不透明度降低至0.3左右,然后逐渐降低至鼠标移出时为零。

My question is, what would be the best way to layout the html and css to do this efficiently and cleanly. 我的问题是,什么是布置html和css的最佳方法,以便高效,整洁地执行此操作。

Here's a brief, but incomplete setup: 这是一个简短但不完整的设置:

<li>
  <a href="#">
    <div class="hover">&nbsp;</div>
    <img src="images/galerry_image.png" />
  </a>
</li>

.hover {
width: 100%;
height: 100%;
background: orange;
opacity: 0;
}

So let's start with slightly simpler HTML: 因此,让我们从稍微简单的HTML开始:

<ul id="special">
    <li><a href="#"><img src="opensrs-2.png" /></a></li>
    <li><a href="#"><img src="opensrs-1.png" /></a></li>
</ul>

Here's my solution: 这是我的解决方案:

<style type="text/css">
#special a img { border: none;}
</style>
<script type="text/javascript">
$(document).ready(function() {

    $('#special a').bind('mouseover', function(){
        $(this).parent('li').css({position:'relative'});
        var img = $(this).children('img');
        $('<div />').text(' ').css({
            'height': img.height(),
            'width': img.width(),
            'background-color': 'orange',
            'position': 'absolute',
            'top': 0,
            'left': 0,
            'opacity': 0.5
        }).bind('mouseout', function(){
            $(this).remove();
        }).insertAfter(this);
    });

});
</script>

EDIT: With fast fade in, fade out: 编辑:随着快速淡入,淡出:

$('#special a').bind('mouseover', function(){
    $(this).parent('li').css({position:'relative'});
    var img = $(this).children('img');
    $('<div />').text(' ').css({
        'height': img.height(),
        'width': img.width(),
        'background-color': 'orange',
        'position': 'absolute',
        'top': 0,
        'left': 0,
        'opacity': 0.0
    }).bind('mouseout', function(){
        $(this).fadeOut('fast', function(){
            $(this).remove();
        });
    }).insertAfter(this).animate({
        'opacity': 0.5
    }, 'fast');
});

Are you looking for something like this: 您是否正在寻找这样的东西:

jQuery: jQuery的:

<script type="text/javascript">
  $(document).ready(function(){
    $("#images span > img").hover(function(){
      $(this).fadeTo("fast",0.3);
    },function(){
      $(this).fadeTo("fast",1.0);
    });
  });
</script>

HTML: HTML:

<div id="images">
  <span><img src="image1.jpg" /></span>
  <span><img src="image2.jpg" /></span>
  <span><img src="image3.jpg" /></span>
  <span><img src="image4.jpg" /></span>
  <span><img src="image5.jpg" /></span>
  <span><img src="image6.jpg" /></span>
  <span><img src="image7.jpg" /></span>
</div>

CSS: CSS:

<style type="text/css">
  #images span {
    display: inline-block;
    background-color: orange;
  }
</style>

Heres the whole thing 整件事都在这里

<script type="text/javascript">
$(function(){
        $("img").hover(function(){
                            $(this).fadeTo("slow",0);   
                            },
                            function(){
                            $(this).fadeTo("slow",1);       
                            });
});
</script>
<style type="text/css">
#lookhere{
    background-color:orange;
    width:auto;
}
</style>
Heres the html
<div id="lookhere"><img href="you know what goes here"></div>

It works and looks pretty cool. 它的工作原理,看起来很酷。 Nice idea. 好主意。

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

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