dav*_*tin 5 css jquery jquery-plugins css-transitions css-animations
试图用smoothstate.js动画一些页面过渡,我陷入了僵局.
这是一个jsfiddle,但它正在抛出一个控制台错误,我在开发时没有得到.但是你仍然可以看到我正在谈论的CSS问题:http://jsfiddle.net/3f5wp9xL/6/
这是另一个没有控制台错误的演示:http: //davidwesleymartin.com/animtest/index.html
所有动画都在CSS中设置(通过@keyframe,动画属性).
动画在页面加载时起作用,但是当我在单击链接后尝试反转动画时,它们表现得很奇怪.我试图通过在单击链接后将退出类('is-exiting')添加到主容器时重置'animation-direction'属性来反转它们.
我能让它工作的唯一方法是为'animation-name'设置一个NEW值,如果这个值和以前一样,它就不起作用了.
相关HTML:
<div id='main'>
<aside class='main-side'>
<div class='anim_element anim_element--fadeIn'>
<h2>sidebar</h2>
<ul>
<li>
<a href='index.html'>Link 1</a>
</li>
<li>
<a href='index.html'>Link 2</a>
</li>
<li>
<a href='index.html'>Link 3</a>
</li>
</ul>
</div>
</aside>
<div class='main-content'>
<div class='anim_element anim_element--fadeInLeft'>
<h1>Main Content</h1>
<p>Lorem...</p>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
相关的CSS(注意:一切都在演示中正确加上前缀,这不是问题):
@keyframes fadeIn{
0% {
opacity:0;
transform:scale(0.8);
}
100% {opacity:1;}
}
@keyframes fadeInLeft{
0% {
opacity:0;
transform: translate3d(-100%, 0, 0);
}
100% {
opacity:1;
}
}
#main .anim_element{
animation-duration: .25s;
transition-timing-function: ease-in;
animation-fill-mode: both;
}
#main .anim_element--fadeIn{
animation-name: fadeIn;
}
#main .anim_element--fadeInLeft{
animation-name: fadeInLeft;
}
#main.is-exiting .anim_element{
animation-name: fadeIn;
animation-direction: alternate-reverse;
}
Run Code Online (Sandbox Code Playgroud)
这个对我有用。
http://davidwesleymartin.com/animtest/index.html
但 jsfiddle 演示已损坏。我认为您需要指定要加载的页面href。
有用。
<a href='index.html'>Link 3</a>
Run Code Online (Sandbox Code Playgroud)
这不起作用。
<a href='/'>Link 3</a>
Run Code Online (Sandbox Code Playgroud)
重点是链接的页面有#main没有元素。我认为这是 smoothState.js 的一个错误。对我来说,在查找元素时,这一行发生了错误#id。
https://github.com/weblinc/jquery.smoothState.js/blob/master/jquery.smoothState.js#L215
更新
要重新启动 css3 动画,您需要重绘元素。
更多信息:http://css-tricks.com/restart-css-animation/
;(function($) {
'use strict';
$.fn.restartCSSAnimation = function(cls) {
var self = this;
self.removeClass(cls);
$.smoothStateUtility.redraw(self);
setTimeout(function () { self.addClass(cls) });
return self;
};
var $body = $('html, body'),
content = $('#main').smoothState({
// Runs when a link has been activated
onStart: {
duration: 1000, // Duration of our animation
render: function (url, $container) {
// toggleAnimationClass() is a public method
content.toggleAnimationClass('is-exiting');
// restart animation
$('.anim_element--fadeIn').restartCSSAnimation('anim_element--fadeIn');
$('.anim_element--fadeInLeft').restartCSSAnimation('anim_element--fadeInLeft');
// Scroll user to the top
$body.animate({scrollTop: 0});
}
}
}).data('smoothState');
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
CSS
#main .anim_element--fadeIn {
-webkit-animation-name: fadeIn;
animation-name: fadeIn; }
#main .anim_element--fadeInLeft {
-webkit-animation-name: fadeInLeft;
animation-name: fadeInLeft; }
#main.is-exiting .anim_element {
-webkit-animation-direction: alternate-reverse;
animation-direction: alternate-reverse; }
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/jewmmqoa/2/