CSS3 equivalent to jQuery slideUp and slideDown?

前端 未结 10 1106
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 08:12

My application is performing poorly with jQuery\'s slideDown and slideUp. I\'m looking to use a CSS3 equivalent in browsers which support it.

Is it possible, using C

10条回答
  •  伪装坚强ぢ
    2020-12-04 08:53

    you can't make easisly a slideup slidedown with css3 tha's why I've turned JensT script into a plugin with javascript fallback and callback.

    in this way if you have a modern brwowser you can use the css3 csstransition. if your browser does not support it gracefuly use the old fashioned slideUp slideDown.

     /* css */
    .csstransitions .mosneslide {
      -webkit-transition: height .4s ease-in-out;
      -moz-transition: height .4s ease-in-out;
      -ms-transition: height .4s ease-in-out;
      -o-transition: height .4s ease-in-out;
      transition: height .4s ease-in-out;
      max-height: 9999px;
      overflow: hidden;
      height: 0;
    }
    

    the plugin

    (function ($) {
    $.fn.mosne_slide = function (
        options) {
        // set default option values
        defaults = {
            delay: 750,
            before: function () {}, // before  callback
            after: function () {} // after callback;
        }
        // Extend default settings
        var settings = $.extend({},
            defaults, options);
        return this.each(function () {
            var $this = $(this);
            //on after
            settings.before.apply(
                $this);
            var height = $this.height();
            var width = $this.width();
            if (Modernizr.csstransitions) {
                // modern browsers
                if (height > 0) {
                    $this.css(
                        'height',
                        '0')
                        .addClass(
                            "mosne_hidden"
                    );
                } else {
                    var clone =
                        $this.clone()
                        .css({
                            'position': 'absolute',
                            'visibility': 'hidden',
                            'height': 'auto',
                            'width': width
                        })
                        .addClass(
                            'mosne_slideClone'
                    )
                        .appendTo(
                            'body'
                    );
                    var newHeight =
                        $(
                            ".mosne_slideClone"
                    )
                        .height();
                    $(
                        ".mosne_slideClone"
                    )
                        .remove();
                    $this.css(
                        'height',
                        newHeight +
                        'px')
                        .removeClass(
                            "mosne_hidden"
                    );
                }
            } else {
                //fallback
                if ($this.is(
                    ":visible"
                )) {
                    $this.slideUp()
                        .addClass(
                            "mosne_hidden"
                    );
                } else {
                    $this.hide()
                        .slideDown()
                        .removeClass(
                            "mosne_hidden"
                    );
                }
            }
            //on after
            setTimeout(function () {
                settings.after
                    .apply(
                        $this
                );
            }, settings.delay);
        });
    }
    })(jQuery);;
    

    how to use it

    /* jQuery */
    $(".mosneslide").mosne_slide({
        delay:400,
        before:function(){console.log("start");},
        after:function(){console.log("done");}
    });
    

    you can find a demo page here http://www.mosne.it/playground/mosne_slide_up_down/

提交回复
热议问题