14

是否有任何简单的方法来创建代码:如果 URL 更改或单击链接显示 div(如加载 gif 3 秒)然后显示页面?有点像加载 gif 旋转 3 秒然后显示页面的空白白页?

谢谢!

4

1 回答 1

43

给定一个<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
}); 
于 2012-07-16T18:53:44.283 回答