JavaScript slidedown without jQuery

前端 未结 11 969
离开以前
离开以前 2020-11-27 14:03

I wish to have a similar effect to jQuery slidedown but without using jQuery or any other libary. I know it\'s \"possible\" as anything in jQuery can be don

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 14:43

    this pure js solution doesn't require knowing the the max-height beforehand, and uses css-transitions.

    it will set max-height to 2x browser window's height during transition, and then the remove max-height after the transition is done.

    full slideDown, slideUp demo @ https://kaizhu256.github.io/node-swagger-lite/build..alpha..travis-ci.org/app/index.html

    css

    .swggAnimateSlide {
        overflow-y: hidden;
        transition: all 500ms linear;
    }
    .swggAnimateSlideUp {
        border-bottom: 0 !important;
        border-top: 0 !important;
        margin-bottom: 0 !important;
        margin-top: 0 !important;
        max-height: 0 !important;
        padding-bottom: 0 !important;
        padding-top: 0 !important;
    }
    

    js

    domAnimateSlideDown = function (element) {
    /*
     * this function will slideDown the dom-element
     */
        if (element.style.maxHeight || element.style.display !== 'none') {
            return;
        }
        element.classList.add('swggAnimateSlideUp');
        element.classList.add('swggAnimateSlide');
        element.style.display = '';
        setTimeout(function () {
            element.style.maxHeight = 2 * window.innerHeight + 'px';
            element.classList.remove('swggAnimateSlideUp');
        }, 50);
        setTimeout(function () {
            element.style.maxHeight = '';
            element.classList.remove('swggAnimateSlide');
        }, 500);
    };
    
    
    domAnimateSlideUp = function (element) {
    /*
     * this function will slideUp the dom-element
     */
        if (element.style.maxHeight || element.style.display === 'none') {
            return;
        }
        element.style.maxHeight = 2 * window.innerHeight + 'px';
        element.classList.add('swggAnimateSlide');
        setTimeout(function () {
            element.classList.add('swggAnimateSlideUp');
            element.style.maxHeight = '0px';
        }, 50);
        setTimeout(function () {
            element.style.display = 'none';
            element.style.maxHeight = '';
            element.classList.remove('swggAnimateSlide');
            element.classList.remove('swggAnimateSlideUp');
        }, 500);
    };
    

提交回复
热议问题