Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery-Mobile collapsible slideDown effect

I want to add a slideDown or slideUp effect to a div with data-role='collapsible', so it will not open at once but gradually.

I have tried this:

$('#my-collapsible').live('expand', function() {      
    $('#my-collapsible .ui-collapsible-content').slideDown(2000);
});    
$('#my-collapsible').live('collapse', function() {
    $('#my-collapsible .ui-collapsible-content').slideUp(2000);
});

But it still opens and closes without delay.

Anyone knows how should I call those slideDown and slideUp methods?

like image 685
Pablo Avatar asked Dec 15 '11 11:12

Pablo


People also ask

How do you toggle slideUp and slideDown in jQuery?

The slideToggle() method toggles between slideUp() and slideDown() for the selected elements. This method checks the selected elements for visibility. slideDown() is run if an element is hidden. slideUp() is run if an element is visible - This creates a toggle effect.

How does jQuery slideDown work?

The slideDown() Method in jQuery is used to check the visibility of selected elements or to show the hidden elements. It works on two types of hidden elements: Elements hidden using jQuery methods. Elements hidden using display: none in CSS.

How do you animate a slide in jQuery?

jQuery Animations - The animate() Method The jQuery animate() method is used to create custom animations. Syntax: $(selector).animate({params},speed,callback); The required params parameter defines the CSS properties to be animated.

Which jQuery method is used to slide down an element?

The jQuery slideDown() method is used to slide down an element. Syntax: $(selector).slideDown(speed,callback);


4 Answers

Live Example:

  • http://jsfiddle.net/6txWy/6/
  • http://jsfiddle.net/6txWy/7/

JS

$('#my-collaspible').bind('expand', function () {
    $(this).children().slideDown(2000);
}).bind('collapse', function () {
    $(this).children().next().slideUp(2000);
});

HTML

<div data-role="page">
    <div data-role="content">
        <div data-role="collapsible" id="my-collaspible">
            <h3>My Title</h3>
            <p>My Body</p>
        </div>
    </div>
</div>
like image 143
Phill Pafford Avatar answered Oct 23 '22 02:10

Phill Pafford


For some reason Phill's solution didn't work in my environment, but a slightly modified version did, maybe someone will have usage of this:

$(document).on('expand', '.ui-collapsible', function() {
    $(this).children().next().hide();
    $(this).children().next().slideDown(200);
})

$(document).on('collapse', '.ui-collapsible', function() {
    $(this).children().next().slideUp(200);
});

this also works directly on all collapsible elements in jquery mobile because it uses the .ui-collapsible selector, which all collapsible elements have

like image 40
gitaarik Avatar answered Oct 23 '22 03:10

gitaarik


Maybe an old question, but jQuery Mobile changed a lot since then.

Here is a working example on animating a collapsible set: http://jsfiddle.net/jerone/gsNzT/

/*\
Animate collapsible set;
\*/
$(document).one("pagebeforechange", function () {

    // animation speed;
    var animationSpeed = 200;

    function animateCollapsibleSet(elm) {

        // can attach events only one time, otherwise we create infinity loop;
        elm.one("expand", function () {

            // hide the other collapsibles first;
            $(this).parent().find(".ui-collapsible-content").not(".ui-collapsible-content-collapsed").trigger("collapse");

            // animate show on collapsible;
            $(this).find(".ui-collapsible-content").slideDown(animationSpeed, function () {

                // trigger original event and attach the animation again;
                animateCollapsibleSet($(this).parent().trigger("expand"));
            });

            // we do our own call to the original event;
            return false;
        }).one("collapse", function () {

            // animate hide on collapsible;
            $(this).find(".ui-collapsible-content").slideUp(animationSpeed, function () {

                // trigger original event;
                $(this).parent().trigger("collapse");
            });

            // we do our own call to the original event;
            return false;
        });
    }

    // init;
    animateCollapsibleSet($("[data-role='collapsible-set'] > [data-role='collapsible']"));
});
like image 41
jerone Avatar answered Oct 23 '22 01:10

jerone


Here is my swing which I needed for nested stuff.

 // look for the ui-collapsible-content and collapse that
 // also need the event (which is an arg) to stop the outer expander from taking action. 
 $(document).on('expand', '.ui-collapsible', function(event) {
     var contentDiv = $(this).children('.ui-collapsible-content');
     contentDiv.hide();
     contentDiv.slideDown(300);
     event.stopPropagation(); // don't bubble up
 })

 $(document).on('collapse', '.ui-collapsible', function(event) {
         var contentDiv = $(this).children('.ui-collapsible-content');
         contentDiv.slideUp(300);
     event.stopPropagation(); // don't bubble up
 });
like image 1
Tom Andersen Avatar answered Oct 23 '22 02:10

Tom Andersen