gpt4 book ai didi

用于淡入/淡出 div 背景的 jQuery 替代方案

转载 作者:行者123 更新时间:2023-11-28 18:38:22 25 4
gpt4 key购买 nike

下面的代码有2个独立的<div>每个元素内部都有一个图像。当我将鼠标悬停在任一 div 上时,背景颜色会淡入然后淡出 - 完美!!

我的这个网页在 Firefox 中运行得非常好,但是它在 IE 中无法运行,所以我想知道是否有实现相同功能的 jQuery 版本?

如果有帮助,这里有一个 jsfiddle:http://jsfiddle.net/mcgarriers/NJe63/

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>

<style type="text/css">
.box, .box2 {
float:left;
width:200px;
height:200px;
background:blue;
-webkit-transition: background 1s;
}
.box:hover {
background:red;
transition: background 1s;;
-moz-transition: background 1s;; /* Firefox 4 */
-webkit-transition: background 1s;; /* Safari and Chrome */
-o-transition: background 1s;; /* Opera */
}
.box2:hover {
background:black
}
</style>

</head>
<body>


<div class="box">
<img src="http://www.internetlearningsociety.com/images/logos/google_logo3.jpg" />
</div>

<div class="box2">
<img src="http://www.internetlearningsociety.com/images/logos/google_logo3.jpg" />
</div>

</body>

</html>

有没有一种简单的方法可以使用 jQuery 来复制我在上面所做的事情?

非常感谢任何指点。

最佳答案

如果您使用 jQuery UI,您可以使用 animate 完成此操作

$('.box').mouseenter(function(){
$(this).stop().animate({ backgroundColor: "red" }, 1000);
}).mouseleave(function(){
$(this).stop().animate({ backgroundColor: "blue" }, 1000);
});

$('.box2').mouseenter(function(){
$(this).stop().animate({ backgroundColor: "black" }, 1000);
}).mouseleave(function(){
$(this).stop().animate({ backgroundColor: "blue" }, 1000);
});;

http://jsfiddle.net/NJe63/1/

编辑:

只需在末尾添加 .children('img').hide() .children('img')show()

$('.box').mouseenter(function(){
$(this).stop().animate({ backgroundColor: "red" }, 1000).children('img').show();
}).mouseleave(function(){
$(this).stop().animate({ backgroundColor: "blue" }, 1000).children('img').hide();
});

$('.box2').mouseenter(function(){
$(this).stop().animate({ backgroundColor: "black" }, 1000).children('img').show();
}).mouseleave(function(){
$(this).stop().animate({ backgroundColor: "blue" }, 1000).children('img').hide();
});;

http://jsfiddle.net/NJe63/2/

您可以对图像使用淡入淡出

$('.box').mouseenter(function() {
$(this).children('img').stop(true,true).fadeIn(1000);
}).mouseleave(function() {
$(this).children('img').stop(true,true).fadeOut(1000);
});

$('.box2').mouseenter(function() {
$(this).children('img').stop(true,true).fadeIn(1000);

}).mouseleave(function() {
$(this).children('img').stop(true,true).fadeOut(1000);
});

http://jsfiddle.net/NJe63/3/

您可以使用不透明度来淡入和淡出图像。但由于谷歌 Logo 作为子元素在内部,因此它会随之淡入/淡出。

http://jsfiddle.net/NJe63/5/

关于用于淡入/淡出 div 背景的 jQuery 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12148772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com