[英]Fade out, fade in between pages
What I want to do: 我想做的事:
On page A, there are links to many pages eg page B, page C etc. 在页面A上,有许多页面的链接,例如页面B,页面C等。
When I click on a link, I want the current page to fade out and the new page to fade in. 当我点击链接时,我希望当前页面淡出,新页面淡入。
I searched a lot and got lots of jquery solutions - they work fine but I want to find out how I can do this in vanilla javascript only. 我搜索了很多并得到了很多jquery解决方案 - 它们工作正常,但我想知道如何才能在vanilla javascript中做到这一点。 I tried to work it out on -my own but it doesn't work. 我试图在我自己的工作,但它不起作用。 I've already checked for console errors and put the JS script in the footer. 我已经检查过控制台错误并将JS脚本放在页脚中。
document.addEventListener("click", function () {
var newUrl = this.attr("href");
if (!newUrl || newUrl[0] === "#") {
location.hash = newUrl;
return;
}
document.querySelector("html").fadeOut(function () {
location = newUrl;
});
return false;
});}
I find this a simple and suitable solution. 我发现这是一个简单而合适的解决方案。
css: CSS:
.animate-in {
-webkit-animation: fadeIn .5s ease-in;
animation: fadeIn .5s ease-in;
}
.animate-out {
-webkit-transition: opacity .5s;
transition: opacity .5s;
opacity: 0;
}
@-webkit-keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
Add .animate-in
to the body of every page. 将.animate-in
添加到每个页面的正文中。
js: JS:
window.addEventListener("beforeunload", function () {
document.body.classList.add("animate-out");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.