English 中文(简体)
How can I reveal content and maintain its visibility when mousing to a child element?
原标题:

I m asking a question very similar to this one—dare I say identical?

An example is currently in the bottom navigation on this page.

I m looking to display the name and link of the next and previous page when a user hovers over their respective icons. I m pretty sure my solution will entail binding or timers, neither of which I m seeming to understand very well at the moment.

Currently, I have:

$(document).ready(function() {  

var dropdown = $( span.hide_me );
var navigator = $( a.paginate_link );

dropdown.hide();

$(navigator).hover(function(){
    $(this).siblings(dropdown).fadeIn();
}, function(){
    setTimeout(function(){
        dropdown.fadeOut();
    }, 3000);
});
}); 

with its respective HTML (some ExpressionEngine code included—apologies):

        <p class="older_entry"><a href="{path= blog/ }" class="paginate_link page_back">Older</a><span class="hide_me">Older entry: 
        <br />
        <a href="{path= blog/ }" class="entry_text">{title}</a></span></p>

        {/exp:weblog:next_entry} 

        <p class="blog_home"><a href="http://joshuacody.net/blog" class="paginate_link page_home">Blog Main</a><span class="hide_me">Back to the blog</span></p>

        {exp:weblog:prev_entry weblog="blog"}

        <p class="newer_entry"><a href="{path= blog/ }" class="paginate_link page_forward">Newer</a><span class="hide_me">Newer entry: 
        <br />
        <a href="{path= blog/ }" class="entry_text">{title}</a></span></p>

This is behaving pretty strangely at the moment. Sometimes it waits three seconds, sometimes it waits one second, sometimes it doesn t fade out altogether.

Essentially, I m looking to fade in span.hide_me on hover of the icons ( a.paginate_link ), and I d like it to remain visible when users mouse over the span.

Think anyone could help walk me through this process and understand exactly how the timers and clearing of the timers is working?

Thanks so much, Stack Overflow. You guys have been incredible as I walk down this road of learning to make the internet.

最佳回答

If you just want to get it working, you can try to use a tooltip plugin like this one.

If you want to understand how this should be done: first, get rid of the timeout, and make it work without it. The difference (from the user s point of view) is very small, and it simplifies stuff (developing and debugging). After you get it working like you want, put the timeout back in.

Now, the problem is you don t really want to hide the shown element on the navigator mouse-out event. You want to hide it in its own mouse out event. So I think you can just pass the first argument to the navigator hover function, and add another hover to dropdowns, that will have an empty function as a first argument, and the hiding code in its second argument.

EDIT (according to your response to stefpet s answer)

I understand that you DO want the dropdown to disappear if the mouse moves out of the navigator, UNLESS its moved to the dropdown itself. This complicates a little, but here is how it can be done: on both types of items mouse-out event, you set a timer that calls a function that hides the dropdown. lets say the timer is 1 second. on both kind of item mouse-in even, you clear this timer (see the w3school page on timing for syntax, etc). plus, in the navigator s mouse-in you have to show the dropdown.

问题回答

Another issue with the timer in your code is that it will always execute when mouse-out. Due to the 3 seconds delay you might very well trigger it again when mouse-over but since the timer still exist it will fade out despite you actually have the mouse over the element.

Moving the mouse back and forth quickly will trigger multiple timers.

Try to get it to work without the timer first, then (if really needed) add the additional complexity with the delay (which you must keep track of and remove/reset depending on state).

Here was the final working code, for anyone who comes across this again. Feel free to let me know if I could have improved it in any ways:

$(document).ready(function() {

var dropdown = $( span.hide_me );
var navigator = $( a.paginate_link );

dropdown.hide();

$(navigator).hover(function(){
    clearTimeout(emptyTimer);   
    $(this).siblings(dropdown).fadeIn();
}, function(){
    emptyTimer = setTimeout(function(){
        dropdown.fadeOut();
    }, 500);
});

$(dropdown).hover(function(){
    clearTimeout(emptyTimer);   
}, function(){
    emptyTimer = setTimeout(function(){
        dropdown.fadeOut();
    }, 500);
});
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签