JavaScript slidedown without jQuery

前端 未结 11 956
离开以前
离开以前 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:48

    After tinkering a little with Symba's answer I came up with this to take into account padding and borders:

    toggleSlide = function (el) {
        var el_max_height = 0;
        if (el.getAttribute('data-max-height')) {
            if (el.style.maxHeight.replace('px', '').replace('%', '') === '0') {
                el.style.maxHeight = el.getAttribute('data-max-height');
                el.style.paddingTop = el.getAttribute('data-pad-top');
                el.style.paddingBottom = el.getAttribute('data-pad-bottom');
                el.style.borderTop = el.getAttribute('data-border-top');
                el.style.borderBottom = el.getAttribute('data-border-bottom');
            } else {
                el.style.maxHeight = '0';
                el.style.paddingTop = '0';
                el.style.paddingBottom = '0';
                el.style.borderBottom = '0';
                el.style.borderTop = '0';
            }
        } else {
            el_max_height = getHeight(el) + 'px';
            el.style['transition-property'] = 'max-height, padding-top, padding-bottom, border-bottom, border-top';
            el.style['transition-duration'] = '0.5s';
            el.style['transition-timing-function'] = 'ease-in-out';
            el.style.overflowY = 'hidden';
            el.style.maxHeight = '0';
            el.setAttribute('data-max-height', el_max_height);
            el.setAttribute('data-pad-top', el.style.paddingTop);
            el.setAttribute('data-pad-bottom', el.style.paddingBottom);
            el.setAttribute('data-border-top', el.style.borderTop);
            el.setAttribute('data-border-bottom', el.style.borderBottom);
            el.style.display = 'block';
            setTimeout(function () { el.style.maxHeight = el_max_height; }, 10);
        }
    }
    

    There's a bit of flicker at top border when expanding transition begins and I don't know (yet) how to fix that.

    I didn't modify Symba's getHeight function; for that see his answer.

提交回复
热议问题