我有一个脚本,该脚本允许每个页面在离开页面后淡出,并在选择另一个页面的href时淡入。但是,当使用javascript:history.back()返回上一页时,动画将保留整页,从而使页面内容无法访问。我将发布我的代码..任何帮助将不胜感激:)
#fader {
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 100%;
height: 100%;
z-index: 999999;
pointer-events: none; background:linear-gradient(-45deg, rgb(233, 233, 233), rgb.(226,225, 225), rgb(253, 253, 253), rgb(216, 214, 214));
animation-duration: 700ms;
animation-timing-function: ease-in-out;
-webkit-animation: Gradient 700ms;
-moz-animation: Gradient 700ms;
animation: Gradient 700ms;
}
@-webkit-keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
@-moz-keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
@keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
#fader:before {
content: 'fade'
}
@keyframes fade-out {
from { opacity: 1 }
to { opacity: 0 }
}
@keyframes fade-in {
from { opacity: 0 }
to { opacity: 1 }
}
#fader.fade-out {
opacity: 0;
animation-name: fade-out;
}
#fader.fade-in {
opacity: 1;
animation-name: fade-in;
}
<script>
function fadeInPage() {
if (!window.AnimationEvent) { return; }
var fader = document.getElementById('fader');
fader.classList.add('fade-out');
}
document.addEventListener('DOMContentLoaded', function() {
if (!window.AnimationEvent) { return }
var anchors = document.getElementsByTagName('a');
for (var idx=0; idx<anchors.length; idx+=1) {
if (anchors[idx].hostname !== window.location.hostname) {
return;
}
anchors[idx].addEventListener('click', function(event) {
var fader = document.getElementById('fader'),
anchor = event.currentTarget;
var listener = function() {
window.location = anchor.href;
fader.removeEventListener('animationend', listener);
};
fader.addEventListener('animationend', listener);
event.preventDefault();
fader.classList.add('fade-in');
});
}
});
window.addEventListener('pageshow', function (event) {
if (!event.persisted) {
return;
}
var fader = document.getElementById('fader');
fader.classList.remove('fade-in');
});
</script>
</head>
<body>
<svg id="fader"></svg>
<script>
fadeInPage();
</script>
<p>content</p>
</body>
答案 0 :(得分:0)
您的问题有点令人困惑,但这对我有用。希望这就是您要寻找的。 p>
function fadeInPage() {
if (!window.AnimationEvent) {
return;
}
var fader = document.getElementById("fader");
fader.classList.add("fade-out");
}
document.addEventListener("DOMContentLoaded", function() {
if (!window.AnimationEvent) {
return;
}
var anchors = document.getElementsByTagName("a");
for (var idx = 0; idx < anchors.length; idx += 1) {
if (anchors[idx].hostname !== window.location.hostname) {
return;
}
anchors[idx].addEventListener("click", function(event) {
var fader = document.getElementById("fader"),
anchor = event.currentTarget;
var listener = function() {
window.location = anchor.href;
fader.removeEventListener("animationend", listener);
};
fader.addEventListener("animationend", listener);
event.preventDefault();
fader.classList.add("fade-in");
});
}
});
window.addEventListener("pageshow", function(event) {
if (!event.persisted) {
return;
}
var fader = document.getElementById("fader");
fader.classList.remove("fade-in");
});
fadeInPage();
#fader {
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 100%;
height: 100%;
z-index: 999999;
pointer-events: none;
background: linear-gradient( -45deg, rgb(233, 233, 233), rgb(226, 225, 225), rgb(253, 253, 253), rgb(216, 214, 214));
animation-duration: 700ms;
animation-timing-function: ease-in-out;
-webkit-animation: Gradient 700ms;
-moz-animation: Gradient 700ms;
animation: Gradient 700ms;
}
@-webkit-keyframes Gradient {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
@-moz-keyframes Gradient {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
@keyframes Gradient {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
#fader:before {
content: "fade";
}
@keyframes fade-out {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
#fader.fade-out {
opacity: 0;
animation-name: fade-out;
}
#fader.fade-in {
opacity: 1;
animation-name: fade-in;
}
<svg id="fader"></svg>
<p>content</p>
<a href="http://www.google.com">Google</a>