0

I've done a really simple jQuery slider

$(document).ready(function(){
    $('li').hover(function(){
        $(this).children('div').slideToggle();
    });
});

Like this. It works perfectly, div's are hidden and they display when you hover the li item, but there's a problem. When you press the "back" button in browser sometimes the slidetoggle "freezes" in the "opened" position (meaning the div is displayed) and they start working backwards. Any ideas for fix?

Jason P
  • 26,984
  • 3
  • 31
  • 45
  • How does `hover` make something into a slider? And do you have an example of that? I can't see why clicking back button will make your script freeze. – putvande Aug 19 '13 at 16:51
  • Here's something i forgot to add. The li's hovered are links, and the bug occurs when u go to link and then press the "back" button in browser. Also, it does not always occurs, but it's more of random. – user2697175 Aug 19 '13 at 17:10

1 Answers1

1

Try sliding them up as soon as the document is ready, like so:

$(document).ready(function(){
    $('li > div').slideUp();
    $('li').hover(function(){
        $(this).children('div').slideToggle();
    });
});

This will assure that your divs are slide upwards and will prevent it from working 'backwards'.

amb110395
  • 1,545
  • 13
  • 16
  • Does not work sadly. Is there an jquery function that catches user pressing "back" button in browser? – user2697175 Aug 19 '13 at 17:11
  • Check out this thread: http://stackoverflow.com/questions/2638292/after-travelling-back-in-firefox-history-javascript-wont-run and this one: http://stackoverflow.com/questions/10462511/is-there-a-way-using-jquery-to-detect-the-back-button-being-pressed-cross-browse – amb110395 Aug 19 '13 at 17:30