JQuery-Mobile складной слайд-эффект - PullRequest
8 голосов
/ 15 декабря 2011

Я хочу добавить slideDown или slideUp эффект к div с data-role='collapsible', чтобы он не открывался сразу, а постепенно

Я пробовал это:

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

Но он все равно открывается и закрывается без задержки.

Кто-нибудь знает, как мне вызывать эти slideDown и slideUp методы?

Ответы [ 5 ]

9 голосов
/ 15 декабря 2011

Пример:

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>
4 голосов
/ 23 октября 2012

Возможно старый вопрос, но с тех пор jQuery Mobile сильно изменился.

Вот рабочий пример анимации складного набора: 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']"));
});
4 голосов
/ 27 сентября 2012

По какой-то причине решение Филла не сработало в моей среде, но слегка измененная версия сработала, возможно, кто-то воспользуется этим:

$(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);
});

это также работает напрямую со всеми складными элементами в jquery mobile, поскольку использует селектор .ui-collapsible, который есть у всех складных элементов

1 голос
/ 27 февраля 2014

Вот отличный ответ Джерона для jQuery Mobile 1.4 (имена событий немного изменились, data-role = "collapsible-set" теперь data-role = "collapsibleset"):

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

  // animation speed;
  var animationSpeed = 500;

  function animateCollapsibleSet( elm ) {

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

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

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

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

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

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

        // trigger original event;
        $( this ).parent().trigger( 'collapsiblecollapse' );
      } );

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

  // init;
  animateCollapsibleSet( $( '[data-role="collapsibleset"] > [data-role="collapsible"]' ) );
} );
1 голос
/ 10 апреля 2013

Вот мои качели, которые мне нужны для вложенных вещей.

 // 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
 });
...