Javascript 如何使我的网站预加载在消失前持续3秒,即使页面已加载(至少停留3秒)
我需要gif在消失之前至少等待3秒钟,即使页面已加载,帮助Javascript 如何使我的网站预加载在消失前持续3秒,即使页面已加载(至少停留3秒),javascript,html,Javascript,Html,我需要gif在消失之前至少等待3秒钟,即使页面已加载,帮助 var loader=document.getElementById(“loader”); addEventListener(“加载”,函数(){ loader.style.height=“100px”; loader.style.width=“100px”; loader.style.borderRadius=“50%”; loader.style.visibility=“隐藏”; }) 答复 文本文本文本文本文本文本文本文本文本
var loader=document.getElementById(“loader”);
addEventListener(“加载”,函数(){
loader.style.height=“100px”;
loader.style.width=“100px”;
loader.style.borderRadius=“50%”;
loader.style.visibility=“隐藏”;
})
答复
文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本
您应该能够将您的visibility=hidden
行包装在setTimeout
中:
setTimeout(() => loader.style.visibility = "hidden", 3000)
其中3000
是在执行第一个参数中给定的函数之前要等待的毫秒数
以下是有关设置超时的更多信息:
编辑:而不是window.addEventListener('load',…
试试
document.addEventListener('DOMContentLoaded',…
在开始设置超时之前,应该等待文档的实际内容加载。
setTimeout
欢迎使用SO:)在html代码的第一行,输入以下内容:
<script>
const renderTimeStart = new Date();
function hidePreloader(){
// Do hiding stuff here...
const loader = document.getElementById("loader");
loader.style.visibility = "hidden";
}
function scheduleHidePreloader(renderTimeStart){
const passedTimeMilliSeconds = new Date() - renderTimeStart;
const renderAfterMilliSeconds = 3000 - passedTimeMilliSeconds;
setTimeout(hidePreloader, renderAfterMilliSeconds);
}
window.addEventListener("load", () => {
scheduleHidePreloader(renderTimeStart);
}
</script>
const renderTimeStart=新日期();
函数hidePreloader(){
//在这里藏东西。。。
常量加载器=document.getElementById(“加载器”);
loader.style.visibility=“隐藏”;
}
函数scheduleHidePreloader(renderTimeStart){
const passedTime毫秒=新日期()-renderTimeStart;
const renderAfterMilliSeconds=3000-passedtimems;
setTimeout(hidePreloader、RenderAverMilliSeconds);
}
window.addEventListener(“加载”,()=>{
scheduleHidePreloader(renderTimeStart);
}
这样,如果在3000毫秒之前调用预加载程序,则renderAfterMilliSeconds
将计算剩余的所需时间,并通过超时调用。否则,如果超时时间超过3000毫秒,则超时时间将收到负值并立即调用
不要忘记,这段代码的全部功能都取决于
const renderTimeStart=new Date()
在导入任何资源之前,在html的第一行中调用该行。这是否回答了您的问题?我已经尝试过了,但我很困惑,因为它不起作用idk该怎么办lol@EvanMedeiros请尝试纠正您的问题。如果您尝试了setTimeout
,请显示de.@EvanMedeiros我编辑过——祝你好运!(另外,异端猴子是对的,如果你已经尝试过一种策略,看到这一点很有帮助。)