0

Ive got a menu and a drop-down panel. When hovering over the menu the panel falls down. I want the menu and the drop down to be highlighted at the same time.

My code is working very nicely but when hover over the menu the highlighting for the menu adds and wont vanish when you hover out from the menu.

<script type="text/javascript">
 $(window).load(function () {
     $(".menu-item").hover(function () {
         $(".menu-item").removeClass('menuHighlighted');
         $(this).addClass('menuHighlighted');
     });

     $(".panel-item").hover(function () {
         $(this).addClass('listHighlighted');
         $(this).parents('.menu-item').addClass('menuHighlighted');
     },
function () {
   $(this).removeClass('listHighlighted');
   $(this).removeClass('menuHighlighted');
});
});
</script>

I believe im almost done, there is just one little thing left that i cant figure out. I have been trying to add:

      $(".menu-item").mouseleave(function () {
          $(this).removeClass('menuHighlighted');
      });

But that wont work. Any other suggestions would help me.

2 Answers 2

1

You have to provide 2 callback functions to the hover function in jQuery; one that is called when you move over, and one that is called when you move out.

And the final callback $(this).removeClass('menuHighlighted'); removes a class from the panel-item that was never there, so your probably meant the parent menu-item.

EDIT 2:

Add the classes on mouseenter of the menu-item, but remove them when leaving your panel.

So, something like this:

$(window).load(
    function () {
        $(".menu-item").mouseenter(
        function () {
            $(".menu-item").removeClass('menuHighlighted');
            $(this).addClass('menuHighlighted');
        }
    );

    $(".panel-item").hover(function () {
        $(this).addClass('listHighlighted');
    },
    function () {
        $(this).removeClass('listHighlighted');
    });

    $("#pnClub").mouseleave(function () {
        $('.menu-item').removeClass('menuHighlighted');
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

I thank you for you answer and suggestion, tho when trying this out my menu and panel will not be highlighted at the same time. The menu highlight drops out when moving to the panel.
I revised my answer based on your comment. If this doesn't work, please provide your HTML as well
This solution is the same as my starting problem. menuHighlight wont remove. I will add my HTML code.
Revised the answer again: set the class when entering the menu-item with the mouse, but only remove them after leaving the panel.
Yes finally its working! So basically i needed to clear the panel when mouse was leaving it. I thank you for your hard work. Could not have done this without you :).
0

Menu:

            <div id="menu">   
                        <a href="Default.aspx">
                            <div class="menu-item">
                              <span class="menu-text">Home</span> 
                             </div>
                        </a>

                        <div class="menu-item" id="Club" runat="server">
                           <span class="menu-text">Club</span> 
                        </div>

                        <a href="About.aspx">
                            <div class="menu-item">
                                <span class="menu-text">About</span> 
                             </div>
                        </a>               
             </div>

Panel:

 <asp:Panel CssClass="panel-club" ID="pnClub" runat="server">
    <div id="panel-club-area">       
                            <a href="Time.aspx">
                                <div class="panel-item">
                                    <span class="panel-text">Time</span> 
                             </div>
                            </a>

                            <a href="Area.aspx">
                                <div class="panel-item">
                                    <span class="panel-text">Area</span> 
                                </div>
                            </a>

                            <a href="Info.aspx">
                                <div class="panel-item">
                                    <span class="panel-text">Info</span> 
                                </div>
                            </a>

        </div>                      
 </asp:Panel>

CSS:

.menuHighlighted
 {
 background-image: url('img/menu-item.png');
 }

 .listHighlighted
 {
     background-image: url('img/bg.png');
 }

Comments

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.