0

Morning, I'm trying to display a div onmouseover. I created a function but i don't know what is wrong. Could you help me please.

The HTML code:

<!DOCTYPE html>
<html lang="it">
<head>
    <meta charset="utf-8">
    <title> PHOTOGALLERY</title>
    <link rel="stylesheet" href="style.css" type="text/css"/>
    <link rel="stylesheet" href="css/smoothness/jquery-ui-
         1.9.2.custom.css"/>
    <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
    <script src="http://malsup.github.com/jquery.cycle2.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.9.2.custom.js">
    </script>
    <script>
        function show_img_container() {
            $("#image_container").css("display", "block");
        }
    </script>
</head>
<body>
    <div id="div_container">
        <div id="div_image" class="cycle-slideshow">
            <img src="images/2.jpg" style="height:100%; width:100%">
            <img src="images/3.jpg" style="height:100%; width:100%">
            <img src="images/4.jpg" style="height:100%; width:100%">
            <img src="images/5.jpg" style="height:100%; width:100%">
        </div>
        <div id="image_container" onmouseover="show_img_container()"></div>
    </div>
</body>
</html>

In the css file i have a image_container id where the attribute display is set to 'none';

7
  • I'm curious, is it possible to hover an element that is displayed as none? Commented Jun 9, 2017 at 7:50
  • 2
    It is better to attach CSS code than describing what you have written there Commented Jun 9, 2017 at 7:50
  • Also the code validation it's ok. Commented Jun 9, 2017 at 7:51
  • 3
    @Novice How can you hover an element in which you have displayed as none? Commented Jun 9, 2017 at 7:52
  • you are triggering an function on the element which is not displayed.. Commented Jun 9, 2017 at 7:53

5 Answers 5

3

If you only want to show the <div> on hover you wouldn't need any JS you could try someting like this:

div {
    display: none;
}
    
a:hover + div {
    display: block;
}
<a>Hover over me!</a>
<div>Stuff shown on hover</div>

To explain further:
Since you're using display: none; on the container the mousehover won't take affect since the <div> can't be found by the event since it's not displaying in the first place.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

3

In the css file i have a image_container id where the attribute display is set to 'none';

It is not possible to hover a none blocked element.

You can use the opacity attribute to hide your div.

div {
    opacity: 0;
}
    
div:hover {
    opacity: 1;
}
<div>Stuff shown on hover</div>

Comments

2

You cant hover on div if its display is sets to none.

8 Comments

It should be a comment.
well that's why i downvoted. but seems like everybody else ( almost ) think this is worthy of an answer
@MihaiT, IMHO its worthy of answer
@Satpal an answer should give a solution. IMO. i think that's one of the reasons the comments were created. for ' answers ' such as these. But anyway, i respect others opinions
Im sorry im new on stack.. thank you for correcting me.
|
2

I'll answer based on what OP has provided. wrap the #image_container with a div that contains the function, then it should work.

And since it's quite weird that something appeared when hovered but didn't disappear after hovering out, I added another function for onmouseout

function show_img_container() {
  $("#image_container").css("display", "block");
}
function hide_img_container() {
  $("#image_container").css("display", "none");
}
#image_container {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div onmouseover="show_img_container()" onmouseout="hide_img_container()">
  HOVER ME!!
  <div id="image_container">IM HIDDEN!!!</div>
</div>

Comments

-1

Ok. I know the problem. In the css stylesheet i had this:

#image_container {
position: absolute;
height: 120px;
width: 100%;
bottom: 5%;
background-color: black;
opacity: 0;
**transition: all 0.3s ease-in-out 2s;**
z-index: 1;

}

#image_container:hover {
opacity: 1;
}

If i delete the transition attribute it works:` Could you show me why?

1 Comment

try transistion: opacity 0.3s ease-in-out 2s;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.