gpt4 book ai didi

javascript - 如何使用纯 JavaScript 将淡入淡出添加到此脚本中

转载 作者:行者123 更新时间:2023-12-03 02:05:27 25 4
gpt4 key购买 nike

我使用了这个网站的淡入淡出 http://youmightnotneedjquery.com/我想做的是缓慢显示模态(使用淡入淡出效果)。但问题是我不知道这个函数到底应该放在哪里。我可能尝试了一切。

function fadeIn(el) {
el.style.opacity = 0;

var last = +new Date();
var tick = function() {
el.style.opacity = +el.style.opacity + (new Date() - last) / 400;
last = +new Date();

if (+el.style.opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
}
};

tick();
}

fadeIn(el);

还有我的脚本

// Get the modal
var modal'.$post->id.' = document.getElementById("myModal'.$post->id.'");

// Get the button that opens the modal
var btn = document.getElementById("myBtn'.$post->id.'");

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

// When the user clicks on the button, open the modal
btn.onclick = function() {
modal'.$post->id.'.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal'.$post->id.'.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";

}
}

我想做的是将淡入淡出添加到这部分代码中,但我不知道如何正确执行

// When the user clicks on the button, open the modal
btn.onclick = function() {
modal'.$post->id.'.style.display = "block";
}

最佳答案

我总是将这个简单的库用于动画目的。只需从 github 下载 animate.min.css https://github.com/daneden/animate.css将其添加到页面的头部,如下所示:

  <head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
</head>

然后您可以将其添加到您的脚本中,如下所示。

$('#yourElement').addClass('animated fadeOut');

modal'.$post->id.'.classList.add('animated fadeOut');

我希望这有帮助?

关于javascript - 如何使用纯 JavaScript 将淡入淡出添加到此脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49829687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com