Javascript 如何设置一个div的动画,使其在单击时淡入或滑入 //获取模式 var modal=document.getElementById('myModal'); //获取打开模式对话框的按钮 var btn=document.getElementById(“myBtn”); //获取关闭模态的元素 var span=document.getElementsByClassName(“关闭”)[0]; //当用户单击该按钮时,打开模式对话框 btn.onclick=函数(){ modal.style.display=“块”; } //当用户单击(x)时,关闭模式对话框 span.onclick=函数(){ modal.style.display=“无”; } //当用户单击模式之外的任何位置时,将其关闭 window.onclick=函数(事件){ 如果(event.target==模态){ modal.style.display=“无”; } }

Javascript 如何设置一个div的动画,使其在单击时淡入或滑入 //获取模式 var modal=document.getElementById('myModal'); //获取打开模式对话框的按钮 var btn=document.getElementById(“myBtn”); //获取关闭模态的元素 var span=document.getElementsByClassName(“关闭”)[0]; //当用户单击该按钮时,打开模式对话框 btn.onclick=函数(){ modal.style.display=“块”; } //当用户单击(x)时,关闭模式对话框 span.onclick=函数(){ modal.style.display=“无”; } //当用户单击模式之外的任何位置时,将其关闭 window.onclick=函数(事件){ 如果(event.target==模态){ modal.style.display=“无”; } },javascript,css,Javascript,Css,我希望在不使用jquery的情况下为div添加一些动画,因为它是用于显示横幅的,我不希望使用库来增大整个文件的大小。应该很简单,但似乎无法使其正常工作。如下更改代码: // Get the modal var modal = document.getElementById('myModal'); // Get the button that opens the modal var btn = document.getElementById("myBtn");

我希望在不使用jquery的情况下为div添加一些动画,因为它是用于显示横幅的,我不希望使用库来增大整个文件的大小。应该很简单,但似乎无法使其正常工作。

如下更改代码:

    // Get the modal
    var modal = document.getElementById('myModal');

    // Get the button that opens the modal
    var btn = document.getElementById("myBtn");

    // Get the <span> element that closes the modal
    var span = document.getElementsByClassName("close")[0];

    // When the user clicks the button, open the modal 
    btn.onclick = function() {
        modal.style.display = "block";
    }

    // When the user clicks on <span> (x), close the modal
    span.onclick = function() {
        modal.style.display = "none";
    }

    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }
关闭样式为:

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
    modal.style.cssText = "visibility: visible;opacity: 1;transition: 0.3s";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.cssText = "visibility: hidden;opacity: 0;transition: 0.3s";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
modal.style.cssText = "visibility: visible;opacity: 1;transition: 0.3s";
工作示例:

modal.style.cssText = "visibility: hidden;opacity: 0;transition: 0.3s";