Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating onclick image overlay

What I have: I currently have code so where if you hover over an image, the overlay will slide from bottom to top.

What I want: I want to be able to achieve the same overlay slide from bottom to top with onclick instead of hover. I'm having trouble figuring out how to achieve this.

Code:

.container {
  position: relative;
  width: 50%;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #008CBA;
  overflow: hidden;
  width: 100%;
  height: 0;
  transition: .5s ease;
}

.container:hover .overlay {
  height: 100%;
}

.text {
  white-space: nowrap; 
  color: white;
  font-size: 20px;
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}
<!DOCTYPE html>
<html>
<head>

</head>
<body>

<h2>Slide in Overlay from the Bottom</h2>
<p>Hover over the image to see the effect.</p>

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

</body>
</html>
like image 377
MMM Avatar asked Apr 08 '26 22:04

MMM


2 Answers

Try this Javascript using jQuery

JS

$(".container").on('click',function(){
    $(this).children(".overlay").css("height","100%");
});

hope this helps..

like image 69
Chandra Shekhar Avatar answered Apr 10 '26 10:04

Chandra Shekhar


To do this, you will need JavaScript, or better yet, jQuery. With jQuery, you can easily just apply the overlay to the children of .container:

$(".container").on('click', function() {
  $(this).children(".overlay").css('height', '100%');
});
.container {
  position: relative;
  width: 50%;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}

.overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #008CBA;
  overflow: hidden;
  width: 100%;
  height: 0;
  transition: .5s ease;
}

.text {
  white-space: nowrap;
  color: white;
  font-size: 20px;
  position: absolute;
  overflow: hidden;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h2>Slide in Overlay from the Bottom</h2>
<p>Click on the image to see the effect.</p>

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image">
  <div class="overlay">
    <div class="text">Hello World</div>
  </div>
</div>

Hope this helps! :)

like image 44
Obsidian Age Avatar answered Apr 10 '26 11:04

Obsidian Age