Javascript 悬停时Div变为黑色
所以我想让这个div淡出另一个div,它有一个0.5不透明度的黑色背景。但当我悬停时,它会忽略已在div中的图像,填充一个黑色,然后淡入另一个div。因此,它会变成image-->Solid black div-->然后淡入第二个div。 HTML: JQuery:Javascript 悬停时Div变为黑色,javascript,jquery,html,css,Javascript,Jquery,Html,Css,所以我想让这个div淡出另一个div,它有一个0.5不透明度的黑色背景。但当我悬停时,它会忽略已在div中的图像,填充一个黑色,然后淡入另一个div。因此,它会变成image-->Solid black div-->然后淡入第二个div。 HTML: JQuery: $(document).ready(function(){ $('.box1').on('mouseenter', function() { $('.img_hover_effect').fadeIn(1000); }
$(document).ready(function(){
$('.box1').on('mouseenter', function() {
$('.img_hover_effect').fadeIn(1000);
});
});
您可以像这样使用jQuery
hover
:
$('.box1').hover(function() {
$('.hover').fadeIn(1000);
}, function(){
$('.hover').fadeOut(1000);
});
还有一些css更改:
.box1 {
position:relative;
height: 400px;
width: 350px;
margin: 0 auto;
margin-top: 100px;
float: left;
overflow: hidden;
}
.tree {
height: 100%;
width: auto;
display: block;
position: relative;
}
.hover {
position:absolute;
background-color: rgba(0, 0, 0, 0.5);
width: 100%;
height: 100%;
display: none;
z-index: 10;
}
JSFIDDLE:您可以使用CSS伪类来实现所需的悬停效果。我建议将该类直接应用于希望具有悬停效果的图像,而不是与另一个div重叠
.hover-effect:hover{
background-color: #000000; //or whatever colour fade you are looking for
opacity: 0.5;
-webkit-transition: all 0.2s ease-in-out;
-moz-transition: all 0.2s ease-in-out;
-o-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
这就是你想要的效果吗?一些html和css更改和所有工作:)
.box1 {
position:relative;
height: 400px;
width: 350px;
margin: 0 auto;
margin-top: 100px;
float: left;
overflow: hidden;
}
.tree {
height: 100%;
width: auto;
display: block;
position: relative;
}
.hover {
position:absolute;
background-color: rgba(0, 0, 0, 0.5);
width: 100%;
height: 100%;
display: none;
z-index: 10;
}
.hover-effect:hover{
background-color: #000000; //or whatever colour fade you are looking for
opacity: 0.5;
-webkit-transition: all 0.2s ease-in-out;
-moz-transition: all 0.2s ease-in-out;
-o-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}