Convert Jquery slidetoggle code to Javascript

后端 未结 5 1595
一向
一向 2020-12-03 06:04

How can I convert the jQuery slidetoggle() function into Javascript?

 var $context = getContext(context);

        $($context).on(\'click\', \'.m_menu\', f         


        
5条回答
  •  一向
    一向 (楼主)
    2020-12-03 06:51

    To reimplement (convert) jquery slideToggle() to vanilla javascript, you need to reimplement jquery slideUp() and slideDown() as well.

    var DOMAnimations = {
        
        /**
        * SlideUp
        *
        * @param {HTMLElement} element
        * @param {Number} duration
        * @returns {Promise}
        */
        slideUp: function (element, duration = 500) {
    
            return new Promise(function (resolve, reject) {
    
                element.style.height = element.offsetHeight + 'px';
                element.style.transitionProperty = `height, margin, padding`;
                element.style.transitionDuration = duration + 'ms';
                element.offsetHeight;
                element.style.overflow = 'hidden';
                element.style.height = 0;
                element.style.paddingTop = 0;
                element.style.paddingBottom = 0;
                element.style.marginTop = 0;
                element.style.marginBottom = 0;
                window.setTimeout(function () {
                    element.style.display = 'none';
                    element.style.removeProperty('height');
                    element.style.removeProperty('padding-top');
                    element.style.removeProperty('padding-bottom');
                    element.style.removeProperty('margin-top');
                    element.style.removeProperty('margin-bottom');
                    element.style.removeProperty('overflow');
                    element.style.removeProperty('transition-duration');
                    element.style.removeProperty('transition-property');
                    resolve(false);
                }, duration)
            })
        },
    
        /**
        * SlideDown
        *
        * @param {HTMLElement} element
        * @param {Number} duration
        * @returns {Promise}
        */
        slideDown: function (element, duration = 500) {
    
            return new Promise(function (resolve, reject) {
    
                element.style.removeProperty('display');
                let display = window.getComputedStyle(element).display;
    
                if (display === 'none') 
                    display = 'block';
    
                element.style.display = display;
                let height = element.offsetHeight;
                element.style.overflow = 'hidden';
                element.style.height = 0;
                element.style.paddingTop = 0;
                element.style.paddingBottom = 0;
                element.style.marginTop = 0;
                element.style.marginBottom = 0;
                element.offsetHeight;
                element.style.transitionProperty = `height, margin, padding`;
                element.style.transitionDuration = duration + 'ms';
                element.style.height = height + 'px';
                element.style.removeProperty('padding-top');
                element.style.removeProperty('padding-bottom');
                element.style.removeProperty('margin-top');
                element.style.removeProperty('margin-bottom');
                window.setTimeout(function () {
                    element.style.removeProperty('height');
                    element.style.removeProperty('overflow');
                    element.style.removeProperty('transition-duration');
                    element.style.removeProperty('transition-property');
                }, duration)
            })
        },
    
        /**
        * SlideToggle
        *
        * @param {HTMLElement} element
        * @param {Number} duration
        * @returns {Promise}
        */
        slideToggle: function (element, duration = 500) {
    
            if (window.getComputedStyle(element).display === 'none') {
    
                return this.slideDown(element, duration);
    
            } else {
    
                return this.slideUp(element, duration);
            }
        }
    }
    
    // ------------------------------------------------------
    
    document.addEventListener("DOMContentLoaded", function() {
    
        var button = document.getElementById('slideToggle');
    
        var cardElement = document.getElementById('firstCard');
    
        button.addEventListener('click', function(event) {
    
            event.preventDefault();
    
            DOMAnimations.slideToggle(cardElement);
        });
    
    });
    * {
        box-sizing: border-box;
    }
    
    /* Add a gray background color with some padding */
    body {
        font-family: Arial;
        padding: 20px;
        background: #f1f1f1;
    }
    
    /* Header/Blog Title */
    .header {
        padding: 10px;
        font-size: 24px;
        text-align: center;
        background: white;
    }
    
    /* Create two unequal columns that floats next to each other */
    /* Left column */
    .leftcolumn {   
        float: left;
        width: 100%;
    }
    
    /* Fake image */
    .fakeimg {
        background-color: #aaa;
        width: 100%;
        padding: 20px;
    }
    
    /* Add a card effect for articles */
    .card {
        position:relative;
        background-color: white;
        padding: 20px;
        margin-top: 20px;
    }
    
    #slideToggle {
        background-color: #f9f5f5;
        color: black;
        border: 2px solid #a9b5a9;
        padding: 5px;
        margin-top:20px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        cursor: pointer;
        font-weight: bold;
        border-radius: 5px;
    }
    
    /* Clear floats after the columns */
    .row:after {
        content: "";
        display: table;
        clear: both;
    }
    
    /* Footer */
    .footer {
        padding: 20px;
        text-align: center;
        background: #ffffd;
        margin-top: 20px;
    }
    
    /* Responsive layout - when the screen is less than 800px wide, make the two columns stack on top of each other instead of next to each other */
    @media screen and (max-width: 800px) {
        .leftcolumn, .rightcolumn {   
            width: 100%;
            padding: 0;
        }
    }
    
    
    
    
    
    
    
    

    Blog Name

    FIRST TITLE HEADING

    Title description, Dec 7, 2018
    Image

    Some text..

    Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.

    SECOND TITLE HEADING

    Title description, Dec 7, 2018
    Image

    Some text..

    Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.

提交回复
热议问题