How to open a Bootstrap modal without jQuery or bootstrap.js javascript?

前端 未结 3 2119
我寻月下人不归
我寻月下人不归 2021-02-19 06:47

I\'m working on a VERY LIGHT survey application. This application runs in third world countries in locations with very limited connection.

We found that the loading-tim

3条回答
  •  梦谈多话
    2021-02-19 07:03

    We write private code for our modal.

    let modal = document.getElementById('our-modal');
    let modalContentElm = modal.querySelector('.modal-content');
    let allModalButtons = modal.querySelectorAll('.modal-footer > .btn');
    let outerClick = true;
    
    let openStyle = () => { //MODAL SHOW
        modal.style.backgroundColor = 'rgba(0,0,0,0.5)';
        modal.style.display = 'block';
        setTimeout(() => { modal.style.opacity = 1; }); //FOR TRANSITION 
    };
    let closeStyle = () => { //MODAL HIDE
        modal.style.display = 'none';
        modal.style.opacity = 0;
    };
    
    //NO CLOSE MODAL WHEN YOU CLICK INSIDE MODAL
    modalContentElm.onclick = () => {
        outerClick = false;
    };
    
    //CLOSE MODAL WHEN YOU CLICK OUTSIDE MODAL
    modal.onclick = () => {
        if(outerClick){ closeStyle(); }
        outerClick = true;
    };
    
    
    for(let btn of allModalButtons){
        btn.onclick = () => {
    
            closeStyle();
    
            if(btn.getAttribute('id') === 'success-btn'){
                //PLEASE WRITE 'success-btn' IN THE ID VALUE OF THE CONFIRM BUTTON
                console.log('Click Yes');
            }
            else{
                console.log('Click Cancel');
            }
            //..... more else if or else for more modal buttons
    
        };
    }
    

    Whenever we want to open modal

    openStyle();
    

    Whenever we want to close modal on manuel

    closeStyle();
    

    It's a bit laborious, but it works. A more general class can be written.

提交回复
热议问题