简体   繁体   English

如何在悬停时使div淡出?

[英]How to make a div fade on hover?

Hi I currently have span that displays over an image on hover, however I want to use a bit of javascript or css transitions to make this div fade in to about 0.8 opacity on hover then back to 0 when the mouse is not hovering. 嗨我目前有悬停显示在图像上的跨度,但是我想使用一些javascript或css过渡使这个div在悬停时淡入大约0.8不透明度然后在鼠标悬停时返回到0。

Here is an example of how I have it setup so far, now all thats needed is the fade and 0.8 opacity: 这是我到目前为止如何设置的示例,现在所需要的是淡入淡出和0.8不透明度:

How its setup - Jsfiddle 它的设置如何 - Jsfiddle

Im sure there is a simple bit of code that someone has to do this 我确定有一个简单的代码,有人必须这样做

Help is much appreciated thanks! 非常感谢帮助谢谢!

So... here's the CSS3 / HTML5-way to do this. 所以......这是CSS3 / HTML5方式。 This won't work in IE though: it will fall back on the regular, immediate way (so it does work, it just isn't as smooth as it is in the real browsers). 这在IE中不起作用:它将以常规的,即时的方式回归(因此它确实起作用,它不像在真实浏览器中那样平滑)。

div.yourDiv {
    -webkit-transition: .4s ease-in-out opacity;
    -moz-transition: .4s ease-in-out opacity;
    -o-transition: .4s ease-in-out opacity;
    transition: .4s ease-in-out opacity;
}

div.yourDiv:hover {
    opacity: 0.8;
}

Since CSS3-transitions are using hardware-accerelation, this really is very smooth! 由于CSS3-transitions使用硬件加速,这真的非常流畅! Besides that, you don't even need any Javascript or jQuery for this =)! 除此之外,你甚至不需要任何Javascript或jQuery =)!

You can use CSS's :hover pseudo-class, unless you need to support IE6: 你可以使用CSS的:hover伪类,除非你需要支持IE6:

.image-hover:hover {
    opacity: .8;
}

* html .image-hover:hover { /* For IE7 and higher */
    filter: alpha(opacity=80);
}

That won't fade to 80%, though, it'll just go there immediately. 然而,它不会褪色到80%,它会立即去那里。 To do that, you can use jQuery's hover and animate functions ( edit : or fadeTo , which is just a convenience wrapper for animate on opacity as shown below): 要做到这一点,你可以使用jQuery的hoveranimate功能( 编辑 :或fadeTo ,这只是对opacity进行animate的便利包装,如下所示):

$(".image-hover").hover(
    function() {
        $(this).stop().animate({opacity: "0.8"});
    },
    function() {
        $(this).stop().animate({opacity: "1"});
    }
);

It's not clear from your question what the text in the span is supposed to be doing, but those are the tools to get you started. 从你的问题中不清楚跨度中的文本应该做什么,但这些是让你入门的工具。

Here's an updated version of your fiddle showing the animation; 这是一个显示动画的小提琴的更新版本 ; I've used 0.6 rather than 0.8 just so it's more obvious. 我使用的是0.6而不是0.8,所以它更明显。

.classa
{
opacity:0.8;
}

you can addClass and removeClass like 你可以像addClassremoveClass一样

$("div.image-hover").hover(
function(){
//fadein
$(this).addClass("classa");
},
function(){
//fadeout
    $(this).removeClass("classa");
}
);

here is the fiddle http://jsfiddle.net/2RN6E/8/ 这里是小提琴http://jsfiddle.net/2RN6E/8/

EDITED after the comment below 在下面的评论后编辑

you can use fadeTo 你可以使用fadeTo

$("div.image-hover").hover(
function(){
//fadein
$(this).fadeTo( "2000", "0.8");
},
function(){
//fadeout
    $(this).fadeTo( "2000","1");
}

here is the fiddle http://jsfiddle.net/2RN6E/14/ ); 这里是小提琴http://jsfiddle.net/2RN6E/14/ );

You could do: 你可以这样做:

function fadein() {
    $('.desc').animate({
        opacity: 0.8,

    }, 1000, function() {
        // Animation complete.
    })

}

function fadeout() {
    $('.desc').animate({
        opacity: 0,

    }, 1000, function() {
        // Animation complete.
    })

}

$('.image-hover').hover(fadein, fadeout);

fiddle here: http://jsfiddle.net/nicolapeluchetti/2RN6E/9/ 在这里小提琴: http//jsfiddle.net/nicolapeluchetti/2RN6E/9/

This code retains the block display for the description element: http://jsfiddle.net/2RN6E/11/ 此代码保留description元素的块显示: http//jsfiddle.net/2RN6E/11/

It just uses the animate function of jQuery: 它只使用jQuery的animate函数:

$(".image-hover").hover(function() {
    $(".desc").animate({opacity: '0.75'},'slow');
}, function() {
    $(".desc").animate({opacity: '0'},'slow');   
});

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

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