使叠加层出现在悬停按钮上
make overlay appear on hover button
当我将鼠标悬停在按钮上时,我试图制作一个叠加层,我希望当鼠标离开按钮时,叠加层恢复到 0 的不透明度,我怎样才能做到这一点具有缓入效果?
这是我的代码:
$('#hand').mouseover(function(){
$('.overlay').animate({opacity:0.5}, 100);
});
.content {
position:absolute;
width:100%;
height:100%;
}
#hand{
position:absolute;
width:200px;
height:200px;
top:60%;
left:55%;
margin-left:-80px;
margin-top:-100px;
z-index:2;
}
.overlay{
position:absolute;
background:black;
width:100%;
height:100%;
opacity:0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="content">
<div class="overlay"></div>
<button id="hand">PRESS</button>
</div>
如果我没看错问题 - 您只需为 mouseout
添加另一个事件即可重置叠加层:
$('#hand').mouseover(function() {
$('.overlay').animate({
opacity: 0.5,
}, 100);
}).mouseout(function() {
$('.overlay').animate({
opacity: 0,
}, 100);
});
.content {
position: absolute;
width: 100%;
height: 100%;
}
#hand {
position: absolute;
width: 200px;
height: 200px;
top: 60%;
left: 55%;
margin-left: -80px;
margin-top: -100px;
z-index: 2;
}
.overlay {
position: absolute;
background: black;
width: 100%;
height: 100%;
opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="content">
<div class="overlay"></div>
<button id="hand">PRESS</button>
</div>
也就是说,您也可以完全在 CSS:
中完成此操作
html,
body,
#mask {
height: 100%;
width: 100%;
margin: 0;
position: relative;
}
button {
top: 50%;
position: absolute;
left: 50%;
transform: translateY(-50%) translateX(-50%);
z-index: 1;
}
#mask {
background: black;
position: absolute;
top: 0;
left: 0;
z-index: 0;
opacity: 0;
transition: opacity 100ms;
}
button:hover+#mask {
opacity: .5;
}
<button>BUTTON</button>
<div id='mask'></div>
当我将鼠标悬停在按钮上时,我试图制作一个叠加层,我希望当鼠标离开按钮时,叠加层恢复到 0 的不透明度,我怎样才能做到这一点具有缓入效果?
这是我的代码:
$('#hand').mouseover(function(){
$('.overlay').animate({opacity:0.5}, 100);
});
.content {
position:absolute;
width:100%;
height:100%;
}
#hand{
position:absolute;
width:200px;
height:200px;
top:60%;
left:55%;
margin-left:-80px;
margin-top:-100px;
z-index:2;
}
.overlay{
position:absolute;
background:black;
width:100%;
height:100%;
opacity:0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="content">
<div class="overlay"></div>
<button id="hand">PRESS</button>
</div>
如果我没看错问题 - 您只需为 mouseout
添加另一个事件即可重置叠加层:
$('#hand').mouseover(function() {
$('.overlay').animate({
opacity: 0.5,
}, 100);
}).mouseout(function() {
$('.overlay').animate({
opacity: 0,
}, 100);
});
.content {
position: absolute;
width: 100%;
height: 100%;
}
#hand {
position: absolute;
width: 200px;
height: 200px;
top: 60%;
left: 55%;
margin-left: -80px;
margin-top: -100px;
z-index: 2;
}
.overlay {
position: absolute;
background: black;
width: 100%;
height: 100%;
opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="content">
<div class="overlay"></div>
<button id="hand">PRESS</button>
</div>
也就是说,您也可以完全在 CSS:
中完成此操作html,
body,
#mask {
height: 100%;
width: 100%;
margin: 0;
position: relative;
}
button {
top: 50%;
position: absolute;
left: 50%;
transform: translateY(-50%) translateX(-50%);
z-index: 1;
}
#mask {
background: black;
position: absolute;
top: 0;
left: 0;
z-index: 0;
opacity: 0;
transition: opacity 100ms;
}
button:hover+#mask {
opacity: .5;
}
<button>BUTTON</button>
<div id='mask'></div>