有没有简单的方法来创建代码:如果 URL 更改或单击链接显示 div(例如加载 gif 3 秒),则显示页面?有点像空白的白色页面,加载 gif 旋转 3 秒然后显示页面?
谢谢!
最佳答案
给定 <a class="waitBeforeNavigate" href="somewhere.html">Go somewhere</a>
function waitBeforeNavigate(ev) {
ev.preventDefault(); // prevent default anchor behavior
const goTo = this.getAttribute("href"); // store anchor href
// do something while timeOut ticks ...
setTimeout(function(){
window.location = goTo;
}, 3000); // time in ms
});
document.querySelectorAll(".waitBeforeNavigate")
.forEach(EL => EL.addEventListener("click", waitBeforeNavigate));
使用 jQuery:
$('.waitBeforeNavigate').on("click", function (ev) {
ev.preventDefault(); // prevent default anchor behavior
const goTo = $(this).attr("href"); // store anchor href
// do something while timeOut ticks ...
setTimeout(function(){
window.location = goTo;
}, 3000); // time in ms
});
关于jquery - 单击 - 导航前延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11510478/