CSS 动画一站式指南目录CSS 动画一站式指南1. CSS 动画1.1 变换 1.1.1 变换属性介绍 1.1.2 变换动画实践1.2 过渡 1.2.1 过渡属性介绍 1.2.2 过渡动画实践1.3
顺晟科技
2021-06-30 17:15:18
310
作者: https://www.likecs.com/xiaxiangx/
列表
li{list-style: url(ohio.gif) square inside;}
生成的内容
插入生成的内容, ::before 和 ::after 伪元素插入文档
指定内容, content, 取值, normal(初始值), <string>, <uri>, <counter>, attr(<identifier>), open-quote, close-quote, no-open-quote, no-close-quote, inherit,
content: attr(href);
如果引用的属性不存在, 所在的位置将显示空字符串计数器
重置和增量, counter-reset, 设置起点, h1{counter-reset: CHapter 4 section -1 subsec fugure 1;}
为了计数, 需要通过一个属性指明通过哪个元素递增计数器. counter-increment, 默认为1, 为负数, 递减, 为正数, 递增
ol{ counter-reset: ordered;} /*默认为0*/
ol li {counter-increment: ordered;} /*默认为1*/
ol li{counter-increment: -1} /*每次减1*/
使用计数器, 需要使用 content 属性及与计数器有关的值, content: counter(ordered);
h1 {counter-reset: section subsec;
counter-increment: chapter;}
h1::before{countent: counter(chpter) "." ;}
h2 {counter-reset: subsec;
counter-increment: section;}
h2::before{content: counter(chapter)"." counter(section)". ";}
h3 {counter-increment: subsec;}
h3::before{content: counter(chapter)"." counter(section)"."counter(subsec)".";}
display 属性的值为 none 的元素不递增计数器, 即便样式规则看似不是这样. visibility 属性的值为 hidden 的元素依然递增计数器
计数器的作用域, counter-reset: ordered
每层嵌套都会新建一个 ordered 计数器. 如果希望嵌套的新计数器追加到现有的计数器上, 实现 1, 1.1, 1.2, 可以使用counters()
ol{counter-reset: ordered; list-style: none;}
ol li:before {content: counters(ordered,".")"."; counter-increment: ordered;}
定义计数模式
@counter-style <name> { ... declarations... } , 目前只有火狐支持
若想交替显示三角形记号
@counter-style triangles{
system: cyclic;
symbols: → △
}
可用的描述符, system, symbols, additive-symbols, prefix, suffix, negative, range, fallback, pad, speak-as
固定计数模式, system: fixed;, 定义的计数器记号数量有限, 用完不重复. 把所有的symbols中的符号都用引号引起来, 可以在 system 描述符中定义起始值.
@counter-style emoji{
system: fixed 5; /*计数从5开始*/
symbols: "#" "$" "%";
}
循环计数模式, system: cyclic; 定义的符号按顺序使用, 一次又一次, 直到计数序列中没有记号为止. 循环模式可以只用一个记号, 这与为 list-style-type 提供一个字符串的效果差不多.
@counter-style wingthinker{
system: cyclic;
symbols: "$";
prefix: "~";
suffix: "~";
}
ul.hmmm {list-style: winghker;}
符号计数式, system: symbolic; 与循环系统类似, 不过在符号系统中国, 每循环一次符号重复多一次.
@counter-style footnotes{
system: symbolic;
symbols: "*" "丿";
suffix: " ";
}
range 描述符, 它的值为一对或多对以空格分开的值, 每一对之间以逗号分隔, 多出的以原始形式展示
不在 range 定义的范围内的计数器, 可以使用 fallback 描述符自定义, 如果主计数系统无法表示计数器中的某个值, 也将使用后备计数格式
@counter-style letters{
system: symbolic;
symbols: A B C D E;
range: 1 14, 100, 105;
fallback: hebrew
}
字母计数模式, system: alphabetic 计数系统与 symbolic 系统十分相似, 重复的方式不同, symbols 描述符的值至少有两个符号.
数字计数模式, system: numeric; 可以定义16进制计数系统
负值使用 negative 描述符处理, negative 只能在支持负值的技术系统中使用, 包括 alphabetic, numeric, symbolic 和 additive
negative 有点像 prefix 和 suffix 的综合体, 只不过仅在计数器为负值的时候起作用
@counter-style accounting{
system: numeric;
symbols: '0' '1' '2';
negative: "(" ")";
prefix: "$";
suffix: "-";
}
ol.kaching{ list-style: accounting;}
数字计数系统的另一个特性是为位数较少的值补全, 让长度保存与位数较多的值相同, 这需要使用 pad 描述符
pad 描述符, 部分是一个真实, 定义每个计数器有几位数, 第二部分是一个字符串, 用于填充位数不足的值. 负号算在记号的长度内
@counter-syle accounting{
system: numeric;
symbols: '0' '1' '2';
pad: 4 "0";
suffix: ".";
}
累加计数模式, system: additive-symbol
@counter-style roman{
system: additive;
additive-symbols:
100 M, 900 CM, 500 D, 400 CD,
100 C, 90 XC, 50 L, 40 XL,
10 X, 9 IX, 5 V, 4 IV, 1 I;
}
典型的罗马计数风格
扩展计数模式, extends , 算是一种计数系统
@counter-style mydecimals{
system: extends decimal;
suffix: ")";
pad: 2 "0";
}
发音计数模式, speak-as 描述符, 取值, auto, bullets, numbers, words, spell-out, <counter-style-name>
利用 CSS 提供的变形功能, 不仅能平移元素, 还能做很多其他的事
在一段时间内把 CSS 属性的初始化变为另一个值
CSS 过渡
transition: color 200ms ease-out 50ms;
定义过渡的属性
过渡使用四个属性定义: transition-property, transition-duration, transition-timing-function 和 transition-delay. 此外, 还有个简写属性 transition, 可一次声明全部四个属性.
始态和终态可以应用不同的过渡至, 但是进入某一状态时一定会使用氮气状态定义的值
限制受过渡影响的属性, transition-property 属性指定想应用过渡效果的 CSS 属性名称, 这样便可以限定只在特定的属性上应用过渡效果, 而其他属性则瞬间完成, transition-property 属性的值是以逗号分隔的属性列表; 或者是none, 表示不过渡任何属性; 还可以是默认值 all, 如果只有
设置过渡持续时间, transition-duration, 单位为 s 或 ms, 设置了多个持续时间, 在谁的状态就用谁的. 值不能为负. 可以设置各个不同属性持续的时间, 如果恰好声明两个持续时间, 奇数位上的属性使用个持续时间, 偶数位上的属性使用第二个持续时间, 持续时间一般在 100 到 200ms 直接效果更好
调整过渡的内部时序, transition-timing-function, 可取值, ease, linear, ease-in, ease-out, ease-in-out, step-start, step-end, steps(n, start), http://easings.net 网站额外提供了很多三次方贝赛尔函数值.
时序函数 | 说明 | 三次方贝赛尔值 | cubic-bezier() 指定一个三次方贝赛尔曲线 cubic-bezier(x1, y1, x2, y2) ease 慢速开始, 加速, 慢, 结束特别慢 cubic-bezier(0.25, 0.1, 0.25, 1) linear 整个过渡过程保持相同的速度 cubic-bezier(0, 0, 1, 1) ease-in 慢速开始, 然后加速 cubic-bezier(0.42, 0, 1, 1) ease-out 快速开始, 然后减速 cubic-bezier(0, 0, 0.58, 1) ease-in-out 与ease类似, 中间快,两边慢 cubic-bezier(0.42, 0, 0.58, 1)
---|
时序函数 | 定义 | step-start 整个过渡都处在最终关键帧上. 等同于 steps(1, start) step-end 整个过渡都处在初始关键帧上, 等同于steps(1, end) steps(n, start) 显示 n 个固定镜头, 其中个固定镜头占整个过渡的百分之 n steps(n, end) 显示 n 个固定镜头, 前百分之 n 的时间出于初始值状态
---|
延迟过渡, transition-delay 属性, 可以为负, 只设置两个值, 和持续时间一样. 我们可以做些数学运算, 为每个过渡的属性设置不同的持续时间和延迟, 但是确保所有过渡在同一时间结束. 在某种程度上, 50ms 的延迟能防止导航菜单意外展开,
transition 简写属性, 一个时间表示持续时间, 两个表示持续时间和延迟时间.
div{
transition-property: color, border-width, border-color, border-radius, transform, opacity, box-shadow, width, padding;
transition-duration: 200ms,180ms, 160ms, 140ms, 120ms, 100ms 1s, 2s, 3;
transition-timing-function: ease, ease-in, ease-out, ease-in-out, linear, step-end, step-start, steps(5, start), steps(3, end);
transition-delay: 0s, 0.2s, 0.4s, 0.6s, 0.8s, 1s, 1.2s, 1.4s, 1.6s;
}
div{
transition:
color 200ms,
border-width 180ms ease-in 200ms,
border-color 160ms ease-out 400ms,
border-radius 140ms ease-in-out 600ms,
transform 120ms linear 800ms,
opacity 100ms step-end 1s,
box-shadow 1s step-start 1.2s,
width 2s steps(5, start) 1.4s,
padding 3s steps(3, end) 1.6s;
}
反向过渡: 退回起点
逆转中断的过渡, 如果过渡还没结束就被打断了, 属性的值将还原为过渡开始前的值
浏览器 | 逆转延迟 | 过渡时间 | 实耗时间 | Chrome 有 200ms 0.200s Safari 有 200ms 0.200s Firefox 有 38ms 0.038s Opera 有 200ms 0.250s Edge 无 38ms 0.038s
---|
若想覆盖这些值, 在始态和终态中都要声明过渡属性, 这对逆向缩减机制虽然没有影响, 但能进一步控制.
支持动画的属性和值
过渡是效果增强
打印过渡
过渡, 属性的始态和终态值由规则中设定的属性值确定, 但是对时间段内值的变化方式没有多少控制权, 动画能做到中间的控制.
定义关键帧
动画的一般结构
@keyframes animation_identifier{
keyframe_selector{
property: value;
property: value;
}
keyframe_selector{
property: value;
property: value;
}
}
keyframe_selector 有两种, 一种是 from 和 to, 一种是 0% 到
设置关键帧动画
关键帧选择器
把动画应用到元素上
在元素上声明动画属性的方式有两种: 一种是单独声明各种属性, 另一种是使用 animation 简写属性一次性声明全部属性.
指定动画的名称, animation-name, div{animation-name: a1, a2, w;}
, 有不存在的会忽略. 如果动画中有重复的属性, 后面的动画会覆盖前面的动画.
定义动画的时长, animation-duration, 单位为秒或毫秒, 有无效的时间, 比如负数, 没带单位, 整个都无效, 一般来说, name 有多少, duration 就有多少. 不对应也没关系, 可以复制和忽略
声明动画的迭代次数, animation-iteration-count, 希望结束后的动作, 动画默认播放一次, 给数字迭代指定次数, 任何数字或关键字 infinite, 设为 0 时, 动画依然播放, 只不过迭代 0 次, 这也会触发 animationstart 和 animationend 事件
设置动画的播放方向, animation-direction, 取值, normal(默认), reverse(逆序), alternate(交替), alternate-reverse(交替, 次是逆序)
延时播放动画, animation-delay, 定义浏览器把动画附加到元素上之后等待多久开始次迭代. 单位为秒或毫秒, 负的延迟是动画在中途立即开始播放
动画事件, animationstart, animationiteration 和 animationend. 每个事件都有三个只读属性: animationName, elapsedTime 和 pseudoElement, 在所有浏览器中都无需使用前缀.
.raibox{
animation-name: red, orange, yellow, blue, green;
animation-duration: 1s, 3s, 5s, 7s, 11s;
animation-delay: 3s, 4s, 7s, 12s, 19s;
}
改变动画的内部时序, animation-timing-function, 取值, ease(默认值), linear, ease-in, ease-out, ease-in-out, step-start, step-end, steps, cubic-bizer;
步进时序函数, step-start, step-end 和 steps(), steps() 时序动画把动画分成一系列等长的步幅. steps() 接受两个参数: 步数和变化点.
以动画形式改变时序函数, animation-timing-function 属性不支持动画, 但是可以放在关键帧中, 修改动画的当前时序. 在某个关键帧中设定的 animation-timing-function, 只应用到那个关键帧中定义的属性上. 模拟小球的摩擦力
@keyframes bounce{
0%{
transform: translateY(0);
animation-timing-function: ease-in;
}
30%{
transform: translateY(100px);
animation-timing-function: ease-in;
}
58%{
transform: translateY(200px);
animation-timing-function: ease-in;
}
80%{
transform: translateY(300px);
animation-timing-function: ease-in;
}
95%{
transform: translateY(360px);
animation-timing-function: ease-in;
}
15%, 45%, 71%, 89%, {
transform: translateY(380px);
animation-timing-function: ease-out;
}
}
设置动画的播放状态, 使用 animation-play-state, 取值 running, paused, 在运行和延迟时暂停, 时间都停止
动画的填充模式, animation-fill-mode 属性定义动画播放结束后是否应用原来的值, 取值, none(默认值), forwards, backwards, both
写为一个属性
animation 简写, 0是ease0是1 normal none running none.
#animated{
animation: 200ms ease-in 50ms 1 normal running forwards slidedown;
}
#animated{
animation-name: slidedown;
animation-duration: 200ms;
animation-timing-function: ease-in;
animation-delay: 50ms;
aniamtion-iteration-count: 1;
animation-fill-mode: forwards;
animation-direction: normal;
animation-play-state: running;
}
动画, 特指度和优先顺序
癫痫和前庭功能失调
动画事件及其前缀
创作人员可以使用视觉滤镜调整元素的外观, 以不同的方式与背后的内容混合
CSS滤镜
合成和混合
与背景混合
裁剪和遮罩
蒙版
蒙版的意思是, 位于形状内部的内容可见, 而在形状外部的内容不可见
定义蒙版, mask-image, 取值, none, <image>, <mask-source>, 使用 img.masked{mask-image: url(theatre-masks.svg);}
定义蒙版
<svg viewbox="0 0 100 100" height="100" width="100"
xmlns="http://www.w3.org/2000/svg" version="1.1">
<mask id="hexlike">
<path fill="#FF0000" d="M 50,0 100,25 100,75 50,100 0,75 0,25"/>
</mask>
</svg>
.masked{mask-image: url(#hexlike);} /*内部应用*/
.masked{mask-image: url(masks.svg#hexlike);}
改变蒙版的模式, mask-mode, 取值, alpha, luminance, match-source(初始值), luminance使用亮度值计算蒙版.. 初始值是 alpha 和 luminance 的结合体
调整蒙版的尺寸和重复方式, mask-size, 取值, cover, contain, <length>, <percentage>, 重复模式, mask-repeat, 取值, repeat-x, repeat-y, repeat, space, round, no-repeat
定位蒙版 mask-position, 定位蒙版, mask-origin, 取值, content-box, padding-box, border-box, margin-box, fill-box, stroke-box, view-box
裁剪和合成蒙版, mask-clip. 对背景而言, 对应的属性是 background-clip, 取值, content-box, padding-box, border-box(默认), margin-box, fill-box, stroke-box, view-box, no-clip
最后一个蒙版属性 mask-composite 相当有趣, 他能从根本上改变多个蒙版的合成方式. mask-composite, 取值, add, subtract, intersect, exclude
写为一个属性
#example{
mask-image: url(circle.svg), url(square.png), url(triangle.gif);
mask-repeat: repeat-y, no-repeat;
mask-position: top right, center, 25% 67%;
mask-composite: subtract, add, add;
mask-size: auto, 50% 33%, contain;
}
#example{
mask:
url(circle.svg) repeat-y top right / auto subtract,
url(square.png) no-repeat center / 50% 33% add,
url(triangle.gif) repeat-y 25% 67% / contain add;
}
蒙版类型, mask-type, 取值, luminance, alpha, 与 mask-mode 十分相似, 但是不支持 match-source值. mask-mode优先级更高
遮罩边框图像, 没有浏览器支持
对象填充和定位
CSS 提供了数种工具, 以便针对特定的媒体或具有特定功能的媒体应用样式
定义针对特定媒体的样式
基本的媒体查询
<link rel="stylesheet" type="text/css" media="screen, speech"
href="visual.css">
@import url(visual.css) screen;
@import url(outloud.css) speech;
@import url(article-print.css) print;
@media screen{
body{}
}
@media print{
body{}
}
复杂的媒体查询
and 把两个或更多的媒体特性连在一起, 每一个特性的结果都必须为 true, 整个查询的结果才是 true
not 对整个查询的结果取反, 如果所有条件都满足, 不应用样式表. not 关键字只能在媒体查询的开头使用.
only 在不支持媒体查询的旧浏览器中隐藏样式表
媒体特性描述符, 而且特性描述符必须放在圆括号中, width, min-width, max-width, height, min-height, max-height, device-width, min-device-width, max-device-width, device-height, min-device-height, max-device-height, aspect-ratio, min-aspect-ratio, max-aspect-ratio, divice-aspect-ratio, min-divice-aspect-ratio, max-divice-aspect-ratio, color, min-color, max-color, color-index, min-color-index, max-color-index, monochrome, min-monochrome, max-monochrome, resolution, min-resolution, max-resolution, orientation, scan, grid.
新值类型, <ratio>, <resolution>
响应式设计
/*通用样式*/
@media (max-width: 400px){
/*这是针对小屏设备的样式*/
}
@media (min-width: 401px) and (max-width: 1000px){
/*这是针对中屏设备的样式*/
}
@media (min-width: 1001px){
/*这是针对大屏设备的样式*/
}
分页媒体
@page{size: 7.5in 10in; margin: 0.5in;}
09
2022-11
09
2022-11
15
2022-09
15
2022-09
15
2022-09
14
2022-09