admin管理员组

文章数量:1343839

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>

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>

Share Improve this question edited Jul 11, 2017 at 3:40 MMM asked Jul 11, 2017 at 3:29 MMMMMM 3451 gold badge6 silver badges19 bronze badges 6
  • where is your javaScript code? Where is the click action?? – Sudhansu Choudhary Commented Jul 11, 2017 at 3:32
  • you will need to add JS code, by setting on click to add/delete style of the div – Hatik Commented Jul 11, 2017 at 3:33
  • onclick will require JavaScript; have you tried anything yet? By only CSS, you could possibly achieve something using a:visited, but it would likely not work as desired. – Fred Gandt Commented Jul 11, 2017 at 3:33
  • @SudhansuChoudhary -- there isn't any JavaScript. The hover is currently done with CSS. – Obsidian Age Commented Jul 11, 2017 at 3:33
  • You need JavaScript/jQuery to attach and execute an action like click. – Sudhansu Choudhary Commented Jul 11, 2017 at 3:36
 |  Show 1 more ment

6 Answers 6

Reset to default 2

Try this Javascript using jQuery

JS

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

hope this helps..

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./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! :)

@MMM

Slightly improvised Way

CSS

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

JS

$(".container").on('click',function(){
    $(this).children(".overlay").toggleClass("show");
});

What this does is. on click it opens overlay, again on click it hides overlay

hope this helps

You can do this with CSS alone.

I am able to achieve this by adding a :focus selector to match your :hover selector.

If your device supports :hover it will show on hover and not require you to click. If your device does not support :hover then clicking the element - on a touchscreen for example - will trigger the :focus state which in this case will simulate the effects of the :hover state.

In other words we change:

.container:hover .overlay

to

.container:hover .overlay, .container:focus .overlay

Read more about focus and it's usage here

Working example:

.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, .container:focus .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>

Css:

.fullHeight {
    height: 100%;
}

Jquery:

$(document).ready(function(){
    $('.container').click(function(){
        $('.overlay',this).addClass('fullHeight');
    })
})

$(document).ready(function(){
    $('.container').click(function(){
        $('.overlay',this).addClass('fullHeight');
    })
})
.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;
}

.fullHeight {
    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%);
}
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<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>

If you have multiple images on your webpage you can use this snippet I created for my own project, all it does is to check if the element returns true on hover or null if it does not.

function isHover(e) {
    return (e.parentElement.querySelector(':hover') === e);
}
var myDiv = document.getElementById('container');
document.addEventListener('mousemove', function checkHover() {
    var hovered = isHover(myDiv);
    if (hovered !== checkHover.hovered) {
        hovered ? document.getElementById("overlay").style = "width: 100%;" : document.getElementById(
                "overlay").style =
            "";
    }
});

本文标签: javascriptCreating onclick image overlayStack Overflow