Roshi
Roshi

Reputation: 396

make overlay appear on hover button

I am trying to make an overlay when I hover a button and I want that when the mouse is out of the button the overlay come back to 0 of opacity, and how can I do it with a ease in out effect ?

Here is my code:

		
$('#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>

Upvotes: 0

Views: 3194

Answers (1)

SW4
SW4

Reputation: 71170

If I read the question correctly- you simply need to add another event for mouseout to reset the overlay:

$('#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>

That said, you can also accomplish this purely in 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>

Upvotes: 1

Related Questions