1

Hello friend I designed a drop down menu using mouseeneter() and mouseout() in jQuery. The problem is when I mouseout() from the main menu, the drop down links slide up. I don't know how to set conditions on it please help. My code is

Jquery

<script>
$(document).ready(function(){
    //alert('hi');
    $('#link-detail').css('display','none');
    $('#hover-detail').mouseenter(function(){
        $('#link-detail').css({position:'absolute',top:'20px',left:'0px',zindex:'99999'});
        $('#link-detail').slideDown();
        });
        $('#hover-detail').mouseout(function(){
        $('#link-detail').slideUp();
        });

    })
</script>

html

<div class="link-detail-wrap"><div id="hover-detail">hover me</div>
<div>
  <ul id="link-detail">
    <li><a href="#">link 1</a></li>
    <li><a href="#">link 1</a></li>
    <li><a href="#">link 1</a></li>
    <li><a href="#">link 1</a></li>
    <li><a href="#">link 1</a></li>
  </ul>
</div></div>
<div>hihihih</div>

CSS

.link-detail-wrap
{
    float:left;
    width:100%;
    position:relative;
    }

ul#link-detail 
{
    margin:0;
    padding:0;
    border:solid 1px #666;
    border-bottom:none;

    }
ul#link-detail  li
{
    list-style:none;
    margin:0;
    padding:0;
    display:inline;

    }
ul#link-detail  li a
{
    text-decoration:none;
    color:#333;
    padding:2px 50px 2px 10px;
    background:#F0F0F0;
    border-bottom:1px solid #666;
    /*line-height:25px;*/
    font:12px "Trebuchet MS", Arial, Helvetica, sans-serif;
    text-transform:capitalize;
    display:block;
    }
ul#link-detail  li a:hover
{
    background:#CCC;
    }

You can also see it on http://jsfiddle.net/36CXK/

PLEASE HELP

Thanks in advance

1
  • Take a look at my answer below :) Commented Apr 6, 2012 at 11:37

4 Answers 4

1

use this script in your script area

<script type="text/javascript">
$(document).ready(function(){
        //alert('hi');
        $('#link-detail').css('display','none');


            $("#hover-detail").hover(
  function () {
    $('#link-detail').css({position:'absolute',top:'20px',left:'0px',zindex:'99999'});
            $('#link-detail').slideDown();
  }, 
  function () {
    // do nothing
  }
);

$(".relation").hover(
  function () {
   // do nothing
  }, 
  function () {
    $('#link-detail').slideUp();
  }
);
 })
</script>

and create on extra css class

.relation {
    position:relative;
    top:-20px;
}
Sign up to request clarification or add additional context in comments.

2 Comments

where to apply class relation ... will you please share it on fiddle
the relation class use in above the "ul" div here is the updated code - http://jsfiddle.net/36CXK/10/
1

One way to do it is using a timeout with a certain delay to slide up the menu on mouse leave. You can clear this timeout every time you hover the menu preventing the animation to occur.

I create an object Dropdown to control the timeout:

var Dropdown = {
    timer: 0,
    hide: function(callback, delay){
        this.timer = setTimeout(function() {
            return callback.call(this, arguments);
        }, delay);
    },
    reset: function(){
        this.timer && clearTimeout(this.timer);
    }
}; 

And you can refactor your code to make use of it:

$('#link-detail').css('display', 'none');

$('#hover-detail').hover(function() {
    Dropdown.reset();
    $('#link-detail').css({
        position: 'absolute',
        top: '20px',
        left: '0px',
        zindex: '99999'
    });
    $('#link-detail').slideDown();
}, function() {
    Dropdown.hide(function(){
        $('#link-detail').slideUp();
    }, 200);
});

$('#link-detail').hover(function() {
    Dropdown.reset();
}, function(){
    Dropdown.hide(function(){
        $('#link-detail').slideUp();
    }, 200);
});

And this is a working fiddle.

Comments

0

Just comment out the $('#link-detail').slideUp(); line.

See here:

http://jsfiddle.net/36CXK/1/

3 Comments

thanks for help stephen...you are right but i want when mouseout() from all then the drop down link should close
@Stephen Young, you didn't get it.
@Kamal in that case just add $('#link-detail').css('display', 'none'); to your mouseout function. Here: jsfiddle.net/36CXK/7
0

Why not use one of the already available jQuery Drop Down List plugins:

They are easy to style and all the functionality has already been implemented. All you need to do is style the drop down to your preferences.

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.