Click here to Skip to main content
15,896,478 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
HY!

i make the navigation menue, all the code are listed below, when the mouse hover on the menue then the list will fadein but suddenly it fade out why? can any one fix it,


<pre lang="HTML">
XML
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
    <link rel="stylesheet" href="style.css">
    <script src="jquery.js"></script>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
    $(document).ready(function($) {
        $('#con').addClass('con');
        $('#sub1').hide();
        $('#book').hover(function() {
            /* Stuff to do when the mouse enters the element */
            $('#sub1').fadeIn('slow');
        }, function() {
            /* Stuff to do when the mouse leaves the element */
            $('#sub1').fadeOut('slow');
        });
        });


</script>
</head>
<body>
    <div id="con">
        <ul class="main_menue" id="main">
            <li><a href="#">Home</a></li>
            <li><a href="#" id="book">Books</a>
                <ul class="sub_menue" id="sub1">
                <li><a href="#">C++</a></li>
                <li><a href="#">Java</a></li>
                <li><a href="#">Php</a></li>
                <li><a href="#">V.B</a></li>
                <li><a href="#">C#</a></li>
                </ul>
            </li>
            <li><a href="#" id="tut">Tutorials</a>
                <ul class="sub_menue" id="sub2">
                <li><a href="#">C++</a></li>
                <li><a href="#">Java</a></li>
                <li><a href="#">Php</a></li>
                <li><a href="#">V.B</a></li>
                <li><a href="#">C#</a></li>
                </ul>
            </li>
            <li><a href="#">About Us</a></li>
            <li><a href="#">Contact Us</a></li>
        </ul>
    </div>

</body>
</html>


CSS
<pre lang="css">body
{

    margin: 0px;
    padding: 0px;
    max-width: 100%;
    max-height: 100%;
    background-color: silver;
    color: white;
}
header
{
    margin-top: -20px;
    width: 100%;
    height: 20%;
    background: gray;
}
.con
{
    background: red;
}
.main_menue ul
{
    color: white;
    position: absolute;
    margin:0px;
    padding:0px;
    list-style-type: none;
}
.main_menue li
{
    float: left;
    display: inline;
    padding:50px;
    margin: 0px;
    text-align: center;

}
.main_menue li a
{
    display: inline-block;
    text-decoration: none;
    color: black;
}
.main_menue li .sub_menue
{
    position: absolute;

}
.sub_menue ul
{

    margin: 0px;
    padding: 0px;
    list-style-type: none;
}
.sub_menue li
{
    width: 100px;
    text-align: left;
    display: inherit;
    float: none;
    margin: 0px;
    padding: 0px;
    background-color: gray;
    overflow: hidden;

}
.sub_menue li a
{
    padding-top:2px;
    margin: 0px;
    text-decoration: none;
    color: black;
}
.sub_menue li a:hover
{
    background-repeat: no-repeat;
    display: block;
    background-color:black;
    color: white;

}
.main_menue li:hover .sub_menue
{
    position: absolute;

    display:block;
}

footer
{
    margin-top: 600px;
    width: 100%;
    height: 20%;
    background: gray;
}
Posted
Updated 15-Dec-13 6:37am
v2
Comments
Sergey Alexandrovich Kryukov 15-Dec-13 13:14pm    
Of course. What would you expect? I can explain you what happen, but what effect do you want to achieve?
—SA

All works as programmed. You have two levels of menu, first level is horizontal, the second is vertical. By some reason, your "Bookmarks" item is permanently highlighted and sub-menu is shown. I don't think this is what you meant to do, but let's ignore it for now.

Your question is about "Books". Indeed, when you hover on this element, sub-menu fades in, when you hover out, it fades out. It's not "sudden". The function .hover() invoke the function in first argument when mouse goes inside the element, and the function in second argument when the mouse goes out. This is exactly what happens.

These is nothing to "fix" here. The "fix" could be applied to the code which fails at certain point. In your case, there is not code which would potentially do something useful. This is incomplete code with some exercises tried. (All those incomplete href="#", etc...) Now, you need a plan on what do you want to achieve and how. First of all, the idea of using $('#book') (id selector) is wrong. Of course it works, but is it what you want? Do you understand that all id values should be unique on the page? Your effects for "Books" will only work for "Books".

So, you need to use some other selector. You can select all first-order children of the main menu, or select all element of some CSS class. As the simplest solution, use this: add some distinct CSS class to all top-level menu items and use class selector:
http://api.jquery.com/class-selector/[^].

See also: http://api.jquery.com/category/selectors[^].

Your idea of hiding second level of menu also does not work with .hover() in any useful way. You complained that the child menu hides out. But how can it stay visible, if, to point to any of the second-level menu items with the mouse you have to move the mouse pointer out of "Books", which causes hiding it out? The whole idea is wrong.

One simple approach wold be this:
HTML
<!-- ... -->

        <ul class="main_menue" id="main">
            <li><a href="#">Home</a></li>
	     <div class="hider">
                <li><a href="#">Books</a>
                    <ul class="sub_menu">
                    <li><a href="#">C++</a></li>
                    <li><a href="#">Java</a></li>
                    <li><a href="#">Php</a></li>
                    <li><a href="#">V.B</a></li>
                    <li><a href="#">C#</a></li>
                    </ul>
                </li>
	     </div>
          <!-- ... -->
          </ul>

<!-- ... -->

I added the element <div> with the class "hider" (for simplicity of the example, you can choose some structural selector) to be a target of hover events:
XML
<script>
    $(document).ready(function($) {
        $('#con').addClass('con');
        sub1 = $(".submenu");
        sub1.hide();
        $(".hider").hover(function() {
            /* Stuff to do when the mouse enters the element */
            sub1.fadeIn('slow');
        }, function() {
            /* Stuff to do when the mouse leaves the element */
            sub1.fadeOut('slow');
        });
    });
</script>

I also reused your selector which used "#sub1" (see the variable sub1) and replaced the selector to the class selector.

Now it works: the menu items only hides out when your mouse pointer moves out of the whole block (div) which is a parent of the top-level menu item and all its sub-items. This is just to give you the idea, not a solution. As I already told you, to make it a solution, you need to accurately design and implement whole thing.

—SA
 
Share this answer
 
v2
You can go like this

JavaScript
$(document).ready(function($) {
        $('#con').addClass('con');
        $('#sub1').hide();
        $('#book').onmouseover(function() {
            /* Stuff to do when the mouse enters the element */
            $('#sub1').fadeIn('slow');
        }).onmouseout(function() {
            /* Stuff to do when the mouse leaves the element */
            $('#sub1').fadeOut('slow');
        });
        });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900