是否有可能使用CSS3过渡动画页面加载而不使用Javascript?
这是我想要的,但在页面加载:
image-slider.html
到目前为止我发现了什么
CSS3过渡-延迟,一种延迟元素效果的方法。只适用于悬停。 CSS3关键帧,工作在负载上,但非常慢。正因为如此,它并不有用。 CSS3转换足够快,但页面加载时没有动画效果。
是否有可能使用CSS3过渡动画页面加载而不使用Javascript?
这是我想要的,但在页面加载:
image-slider.html
到目前为止我发现了什么
CSS3过渡-延迟,一种延迟元素效果的方法。只适用于悬停。 CSS3关键帧,工作在负载上,但非常慢。正因为如此,它并不有用。 CSS3转换足够快,但页面加载时没有动画效果。
当前回答
不一定,因为CSS是尽快应用的,但元素可能还没有绘制。你可以猜测延迟1或2秒,但这对大多数人来说并不合适,这取决于他们的互联网速度。
此外,如果你想淡入一些东西,例如,它将需要CSS隐藏要交付的内容。如果用户没有CSS3转换,那么他们将永远不会看到它。
我推荐使用jQuery(为了方便使用+你可能希望为其他无人机添加动画)和一些像这样的JS:
$(document).ready(function() {
$('#id_to_fade_in')
.css({"opacity":0}) // Set to 0 as soon as possible – may result in flicker, but it's not hidden for users with no JS (Googlebot for instance!)
.delay(200) // Wait for a bit so the user notices it fade in
.css({"opacity":1}); // Fade it back in. Swap css for animate in legacy browsers if required.
});
随着在CSS中添加的过渡。这样做的好处是,如果需要的话,可以很容易地允许在传统浏览器中使用animate而不是第二个CSS。
其他回答
你也可以使用自定义css类(className)来代替css标签。 不需要外部包装。
import React, { useState, useEffect } from 'react';
import { css } from '@emotion/css'
const Hello = (props) => {
const [loaded, setLoaded] = useState(false);
useEffect(() => {
// For load
setTimeout(function () {
setLoaded(true);
}, 50); // Browser needs some time to change to unload state/style
// For unload
return () => {
setLoaded(false);
};
}, [props.someTrigger]); // Set your trigger
return (
<div
css={[
css`
opacity: 0;
transition: opacity 0s;
`,
loaded &&
css`
transition: opacity 2s;
opacity: 1;
`,
]}
>
hello
</div>
);
};
如果有人同时做两个过渡有问题,这是我做的。我需要文字从上到下的页面加载。
超文本标记语言
<body class="existing-class-name" onload="document.body.classList.add('loaded')">
超文本标记语言
<div class="image-wrapper">
<img src="db-image.jpg" alt="db-image-name">
<span class="text-over-image">DB text</span>
</div>
CSS
.text-over-image {
position: absolute;
background-color: rgba(110, 186, 115, 0.8);
color: #eee;
left: 0;
width: 100%;
padding: 10px;
opacity: 0;
bottom: 100%;
-webkit-transition: opacity 2s, bottom 2s;
-moz-transition: opacity 2s, bottom 2s;
-o-transition: opacity 2s, bottom 2s;
transition: opacity 2s, bottom 2s;
}
body.loaded .text-over-image {
bottom: 0;
opacity: 1;
}
不知道为什么我一直试图在1个选择器中使用2个转换声明,(不是真的)认为它会使用两者。
你可以在页面加载时运行CSS动画,而不使用任何JavaScript;你只需要使用CSS3关键帧。
让我们看一个例子……
下面是只使用CSS3的导航菜单滑动的演示:
@keyframes slideInFromLeft { 0% { transform: translateX(-100%); } 100% { transform: translateX(0); } } header { /* This section calls the slideInFromLeft animation we defined above */ animation: 1s ease-out 0s 1 slideInFromLeft; background: #333; padding: 30px; } /* Added for aesthetics */ body {margin: 0;font-family: "Segoe UI", Arial, Helvetica, Sans Serif;} a {text-decoration: none; display: inline-block; margin-right: 10px; color:#fff;} <header> <a href="#">Home</a> <a href="#">About</a> <a href="#">Products</a> <a href="#">Contact</a> </header>
分解一下……
这里重要的部分是关键帧动画,我们称之为slideInFromLeft…
@keyframes slideInFromLeft {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}
...这基本上是在说“在开始的时候,标题将会在屏幕的左边边缘处全宽,而在结束的时候会在适当的位置”。
第二部分是调用slideInFromLeft动画:
animation: 1s ease-out 0s 1 slideInFromLeft;
以上是简写版本,为了清晰起见,这里是详细版本:
animation-duration: 1s; /* the duration of the animation */
animation-timing-function: ease-out; /* how the animation will behave */
animation-delay: 0s; /* how long to delay the animation from starting */
animation-iteration-count: 1; /* how many times the animation will play */
animation-name: slideInFromLeft; /* the name of the animation we defined above */
你可以做各种有趣的事情,比如滑动内容,或者吸引人们的注意力。
W3C是这么说的。
它将在鼠标第一次在屏幕上移动时开始,这主要是在到达后的一秒钟内,这里的问题是它将在离开屏幕时反转。
html:hover #animateelementid, body:hover #animateelementid {rotate ....}
这是我能想到的最好的东西:http://jsfiddle.net/faVLX/
全屏:http://jsfiddle.net/faVLX/embedded/result/
编辑见下面的评论: 这在任何触屏设备上都行不通,因为没有悬停,所以用户只有点击才会看到内容。——里奇·布拉德肖
只需要很少的Javascript:
window.onload = function() {
document.body.className += " loaded";
}
现在来看CSS:
.fadein {
opacity: 0;
-moz-transition: opacity 1.5s;
-webkit-transition: opacity 1.5s;
-o-transition: opacity 1.5s;
transition: opacity 1.5s;
}
body.loaded .fadein {
opacity: 1;
}
我知道这个问题说的是“不使用Javascript”,但我认为值得指出的是,有一个简单的解决方案,只需要一行Javascript。
它甚至可以是内联Javascript,就像这样:
<body onload="document.body.className += ' loaded';" class="fadein">
这就是所有需要的JavaScript。