How to implement a fade effect (or any other effect) on toggle?

…衆ロ難τιáo~ 提交于 2019-12-24 18:09:12

问题


Here's how the toggle should work: http://jsfiddle.net/uVaQ3/

$("#experience-left-details").on('click', '.see-map, .see-gallery', function (event) {
    event.preventDefault();
    $(".media-container, .swiper-container").toggleClass('hide');
    mySwiper.resizeFix(true);
    mySwiper.reInit(true);

});

GOAL

Add a fade in or any other effect on this.

TRY 1)

So I've tried to apply the effect on the container: http://jsfiddle.net/x29Xk/

$("#experience-left-details").on('click', '.see-map, .see-gallery', function (event) {
    event.preventDefault();
    $("#experience-left-details").fadeToggle();
    $(".media-container, .swiper-container").toggleClass('hide');
    mySwiper.resizeFix(true);
    mySwiper.reInit(true);
    $("#experience-left-details").fadeToggle();
});

Issue: it blinks, doesn't fade.

TRY 2)

http://jsfiddle.net/7T4w4/24/

$("#experience-left-details").on('click', '.see-map, .see-gallery', function (event) {
    event.preventDefault();
    $("#experience-left-details").fadeToggle(
        function(){
            $(".media-container, .swiper-container").toggleClass('hide');
            mySwiper.resizeFix(true);
            mySwiper.reInit(true);    
        }
    );
    $("#experience-left-details").fadeToggle();
});

Fixed the blink, but... Issue: If you click "see map" then resize the window, then click "see gallery" you will see that the gallery is NOT properly rendered.

Any help please? The intended end result is to use a slide effect, not fade, but, if one works, I suppose any other will too.


回答1:


DEMO

The problem you are encountering is that you are asking Swiper to recalculate its size at a moment when its not visible, and therefore has no size. So we need to be calling resizeFix at a moment when the element is visible. To do this wait 16ms (or one frame) of the fadeIn animation to call resizeFix, that way its the minimum partially visible, and Swiper can determine its new size.

var $eld = $("#experience-left-details");
$eld.on('click', '.see-map, .see-gallery', function (event) {
    event.preventDefault();
    $eld.fadeOut(
        function(){
            $(".media-container, .swiper-container").toggleClass('hide');
            // wait one frame to resize
            setTimeout(function(){
                mySwiper.reInit(true);
                mySwiper.resizeFix(true);            
            }, 16);
        }
    ).fadeIn();
});


来源:https://stackoverflow.com/questions/23412741/how-to-implement-a-fade-effect-or-any-other-effect-on-toggle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!