E
E
Egor Rublev2014-05-11 13:21:24
JavaScript
Egor Rublev, 2014-05-11 13:21:24

Appearing block on hover

Hi friends.
In javascript, I'm a dunce, so I'll forgive the help, I need a script so that when you hover over the image with the mouse, it is superimposed on it:
background: rbga(0,0,0,.3);
And in the center of the block:

<p>Заголовок</p>
<p>Текст</p>

ps I will correct the color and text, if you wish, you can add some kind of animation.
As a result, we need such a result when hovering over the image -3760d5d685634ce186df4a803f2de2ff.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anna Bakurova, 2014-05-11
@Libris

html structure

<div class="block-wrap">
      <div class="block-img"></div>
      <div class="block-text">
        <h2>Заголовок</h2>
        <p>Текст</p>
      </div>
    </div>

css structure
.block-wrap{
    position: relative;
    overflow: hidden;
}
    .block-img{
        position: relative;
        width: 100%;
        height: auto;
    }
    .block-text{
        position: absolute;
        top: 0;
        left: 0;
        background: rbga(0,0,0,.3);
        color: #fff;
        opacity: 0;
        visibility: hidden;
width: 100%;
height: 100%;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
padding: 5%;
                -webkit-transition: all 0.2s ease;
                -moz-transition: all 0.2s ease;
                -o-transition: all 0.2s ease;
                -ms-transition: all 0.2s ease;
                transition: all 0.2s ease;
    }
    .block-wrap:hover .block-text{
        opacity: 0;
        visibility: visible;
    }

G
GM2mars, 2014-05-12
@GM2mars

No css animation.
HTML layout:

<div class="cont">
  <div class="overlay">
    <h1>Заголовок</h1>
    <p>Текст</p>
  </div>
  <img src="img1.jpg">
</div>

CSS styles:
.cont {
  width: 500px;
  height: 500px;
  position: relative;
}
.overlay {
  position: absolute;
  text-align: center;
  width: 100%;
  height: 100%;
  background: rbga(0, 0, 0, 0.3);
  display: none;
}

JavaScript (jQuery):
jQuery(document).on("hover", ".cont", function() {
  jQuery(this).children(".overlay").fadeIn("fast");
}, function() {
  jQuery(this).children(".overlay").fadeOut("fast");
});

The principle is simple, there is a container with a background color fill, there is a picture in it, a block with text is absolutely located above the picture, which is hidden (display: none).
When you hover the mouse over the container, we show a hidden block with text, and make the image transparent. It does not pretend to be an ideal solution, but it should work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question