2

I have the following code on my page.

http://jsfiddle.net/SO_AMK/r7ZDm/

As you see it's a list of links, and every time a link is clicked, the popup box opens up right underneath the link in question.

Now, what I need to do is basically the same, except I need to use the .hover event and delay the execution by 2 seconds. So instead of clicking, the user should keep the cursor over a link for 2 seconds.

Sounds simple enough but I can't get the positioning to work properly. here's what I tried:

$('a.showreranks').hover(function()
    {   
    $(this).data('timeout', window.setTimeout(function()
        {
            position = $(this).position();   
        $('#rerank_details').css('top', position.top + 17);   
        $('#rerank_details').slideToggle(300);    
        }, 2000));
    },
    function()
    {
        clearTimeout($(this).data('timeout'));
    });

Can someone modify this to make it work?

2 Answers 2

4

Try like this:

$('a.showreranks').hover(function()
{   
    var self = this;
    $(this).data('timeout', window.setTimeout(function() {
        var position = $(self).offset();   
        $('#rerank_details').css('top', position.top + 17);   
        $('#rerank_details').slideToggle(300);    
    }, 2000));
},
function()
{
    clearTimeout($(this).data('timeout'));
});

DEMO

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

4 Comments

Hm. Works on the jsFiddle but not on my actual page. I simplified things a bit for this example as the actual code is a lot longer (there's a lot of other functionalities there). Could you take a look here and see if you can figure out why it won't work: jovansfreelance.com/booklist/list/Syn/GoT (note that the div id on this page is #gen_details and the link class is .itemshow
And the JS is in scripts.js. Hovering over book titles is what opens the div, but it opens on top of the page.
@robert What is the problem? The position?
I have updated my answer.Instead of jquery.position you need to use jquery.offset. Read more about -> api.jquery.com/position
0

jsFiddle demo

$('ul').on('mousemove','li',function(e){
    
    var m = {x: e.pageX, y: e.pageY};
    $('#rerank_details').css({left: m.x+20, top: m.y-10});
    
}).on('mouseenter','li',function(){
    
    var t = setTimeout(function() {
        $('#rerank_details').stop().slideDown(300);
    },2000);
    $(this).data('timeout', t);
    
}).on('mouseleave','li',function(){
    
    $('#rerank_details').stop().slideUp(300);
    clearTimeout($(this).data('timeout'));
    
});

The setTimeout will act like a hover intent that actually delays the execution for 2 seconds
and counts the time hovered inside a data attribute of the hovered element - that gets 'nulled' on mouseleave.
I added also a few lines of code that will make your tooltip follow the mousemove.

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.