slideToggle and :visible
Asked Answered
R

2

15

When using the sliderToggle method, the :visible expression never seems to return anything other than true.

If I manually use show/ hide in conjunction with :visible expression it'll work just fine.

Example of failure:

jQuery(".fileNode .nodeExpander").click(function() {
    var notes = jQuery(this).parent().siblings(".fileNotes");
    notes.slideToggle ("fast");

    var isVisible = notes.is(":visible"); // Always returns true...

    // Do stuff based on visibility...
});

Example of working:

jQuery(".fileNode .nodeExpander").click(function() {
    var notes = jQuery(this).parent().siblings(".fileNotes");
    var isVisible = notes.is(":visible");

    if (isVisible)
        notes.hide("fast");
    else
        notes.show("fast");

    // Do stuff based on visibility...
});

Some html:

<ul>
    <li class="fileNode">
        <img src="<%= Url.Content ("~/Images/Collapse.png") %>" alt="<%= UIResources.CollpaseAltText %>" class="nodeExpander" />
    </li>
    <li class="fileLink">
        <%= Html.ActionLink (file.Name, "Details", new { id = file.FileId }) %>
    </li>
    <li class="fileNotes">
        <%= file.Description %>
    </li>
</ul>

I'm assuming that the slideToggle doesn't do a show/ hide - is there something else I can check?

I've tried in Firefox 3.5, IE 7, 8 and Chrome 4...all with the same results.

Thanks, K

Rogue answered 28/8, 2009 at 8:11 Comment(0)
R
31

Your first (non-working) code fragment will be testing :visible while slideToggle is mid-transition (more precisely, it tests it just after the transition starts.) Regardless of whether you're opening or closing, the mid-transition state will be :visible - so you always get true.

Try checking .is(":visible") before calling slideToggle

Rok answered 28/8, 2009 at 8:17 Comment(2)
Of course! Thanks, feel like a muppet now - by calling var isVisible = !notes.is(":visible"); before the slideToggle I get the answer required. Thank you!Rogue
It is a bit counter-intuitive, but makes perfect sense. If only we have an internal slide-up-status for the jQuery object...Siclari
D
4

Try adding a handler.

notes.slideToggle ("fast", function() { 
  var isVisible = notes.is(":visible");
});
Deficiency answered 28/8, 2009 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.