CSS实现动态效果+小实例

本文介绍了CSS3实现动态效果的各种技巧,包括`html,body{height:100%}`与`body{height:100%}`的区别、`perspective`视距的概念、元素居中方法、`transform-style`属性、`transform`变换方法、`Animation`动画属性和`@keyframes`关键帧、`:nth-child()`选择器的用法,以及`box-shadow`边框阴影的设置。通过实例解析,帮助读者理解和掌握CSS3在创建动态效果中的应用。" 86500998,5738192,Koa-bodyparser详解:从二进制到解析请求体,"['JavaScript', 'NodeJS', 'HTTP', '中间件', '数据解析']

------------------html,body{height:100%;} 与 body{height:100%;}的区别:

body以html标签为高度参照,所以仅仅body 100%,并不能使它的子div100% 占据整个屏幕;

 html 100%使得html标签以页面窗口为参照,能使它的子div100% 占据整个屏幕;
 

------------------perspective

perspective-视距:用来设置用户与3d元素空间Z平面之间的距离

- perspective值越小,用户与3D空间z平面距离越近,3d元素看起来越大

- perspective值越大,用户与3D空间z平面距离越远,3d元素看起来越小

          详细参考:css3 视距-perspective - Qboooogle - 博客园

------------------实现标签居中

.conte{
    position:absolute;
    top:0;
    left:0;
    rightL0;
    bottom:0;
    margin:auto;
}

------------------transform-style:定义元素的2D/3D效果

transform-style属性指定元素如何在空间中呈现,有两个属性值:flat/ preserve-3d

        - flat 为默认属性值,表示元素在空间中以2D呈现

        - preserve-3d 则表示元素在空间中以3D呈现

------------------transform:实现元素的运动效果

transform

        transform属性使元素发生变化,主要有一下属性值:

                - 旋转: rotate

                - 扭曲 :skew

                - 缩放 :scale

                - 移动 :translate

                - 矩阵变形: matrix

        transform属性用法:         

                 transform : none /  <transform-function> 空格 <transform-function>

                        - none表示无变化;

                        - <transform-function>表示单个或多个变化操作,多个时属性值用空格隔开;

        transform属性值用法:

                - rotate(<angle>) 

                         rotate(<angle>) :使元素进行指定角度的2D旋转

                         rotateX(<angle>) :使元素在X轴上进行2D旋转

                         rotateY(<angle>) :使元素在Y轴上进行2D旋转

Animation

        动画属性

                - animation-delay > 定义动画开始之前的延迟

                - animation-name > 定义需要绑定到Animation选择器的 keyframe 名称

                - animation-direction>定义是否轮流反向播放动画

                - animation-duration > 定义完成动画所花费的时间

                - animation-timing-function > 定义动画的速度曲线

                - animation-iteration-count> 定义动画循环次数

        定义动画

        animation + @keyframes(关键帧)

                - animation定义的属性值  <对应>  关键帧施加对象

                - 一般变化时使用关键字“from” 和 “to” ,详细(指定)变化时使用 “%” ;

                        “from,to” ==“0%, 100%”

                - 循环次数属性值 “infinite” 可以直接在animation里定义,效果与

                         “animation-iteration-count:infinite;” 相同

.ball{
    height:200px;
    width:200px;
    animation:rotate 30s linear;      //animation定义rotate,时间30s,方式(轨迹)linear
    animation-iteration-count:infinite;  //效果循环次数定义为无限
    transform-style:preserve-3d;         //3D显示
}

----------animation:rotate 30s linear infinite;
                //#animation定义rotate,时间30s,方式(轨迹)linear,效果循环次数定义为无限


关键帧定义方式1:
@keyframes rotate{                                     //对rotate施加关键帧
    0%{transform:rotatey(0deg) rotatex(0deg);}         //定义0%时状态
    100%{transform:rotatey(360deg) rotatex(360deg);}   //定义100%时状态
}

关键帧定义方式2:
@keyframes rotate{                                     //对rotate施加关键帧
    from{transform:rotatey(0deg) rotatex(0deg);}       //定义0%时状态
    50%{transform:rotatey(180deg) rotatex(180deg);}    //定义50%时状态
    to{transform:rotatey(360deg) rotatex(360deg);}     //定义100%时状态
}

CSS3 :nth-child() 选择器

        CSS选择器 :nth-child(n)  >选择器会找到其自身的父类,匹配父元素中的第 n 个兄弟元素,元素类型没有限制,且 n 可以是数字、关键字、或公式;

.ball .borderline{
    border:1px solid #ff0;
    height:100%;
    width:100%;
    border-radius:50%;
}
.ball .borderline:nth-child(1){        //获取.borderline的父类下兄弟子类的第一个对象
    transform:rotatey(0deg);           //绕y轴转0
}

box-shadow添加边框阴影

        box-shadow > 向框添加一个或者多个阴影;

        语法:box-shadow: offset-x offset-y blur spread color inset

                - box-shadow: x轴偏移量 Y轴偏移量 阴影模糊半径 阴影半径 颜色  外部阴影/内部阴影                - 当需要设置多个阴影时,中间需要将每个阴影用逗号隔开

box-shadow: 10px 10px 20px 10px rgba(255,255,0,0.5), -10px 10px 10px 10px rgba(255,255,255,0.5) 
//右移10  下移10  模糊程度20  阴影半径10 浅黄色
//左移10  下移10  模糊程度10  阴影半径10 白色

                - 参数要求:

                        - offset-x:参数必须,取值:+/-

                        - offset-y:参数必须,取值:+/-

                        - blur:可选,取正值,0即无模糊效果,值越大阴影边缘越模糊

                        - spread:可选,取值:+/- ,正值则阴影扩大,负值阴影缩小。

                       - color:可选,如果不设置,浏览器会取默认颜色,通常是黑色,但各浏览器默认颜色有差异,建议不要省略。

                        - color:可选,外部投影/内部投影

-----eg:行星自转及绕太阳自转+星空效果

pycharm代码:

        HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="test.css" rel="stylesheet"/>
    <link href="t1.css" rel="stylesheet"/>
</head>
<body>
    <div class="box conter">
        <div class="ball conter sun">
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
            <div class="borderline conter"></div>
        </div>
        <div class="ball_path_1 conter">
            <div class="ball conter">
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
            </div>
        </div>
        <div class="ball_path_2 conter">
            <div class="ball conter">
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
            </div>
        </div>
        <div class="ball_path_3 conter">
            <div class="ball conter">
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
                <div class="borderline conter"></div>
            </div>
        </div>
    </div>
    <div class="all_star">
        <div class="star"></div>
        <div class="star"></div>
        <div class="star"></div>
        <div class="star"></div>
        <div class="star"></div>
        <div class="star"></div>
        <div class="star"></div>
        <div class="star"></div>
        <div class="star"></div>
        <div class="star"></div>
    </div>
</body>
</html>

        test.css:

html,body{
    height:100%;
}
body{
    perspective:500px;
    background:#000;
}
.conter{
    position:absolute;
    top:0;
    right:0;
    left:0;
    bottom:0;
    margin:auto;
}
.box{
   // border:1px solid #fff;
    height:500px;
    width:500px;
    transform-style:preserve-3d;
    transform:rotatex(65deg);
}
.ball{
    height:200px;
    width:200px;
    animation:rotate 30s linear infinite;
  //  animation-iteration-count:infinite;
    transform-style:preserve-3d;
}
.ball .borderline{
    border:1px solid #ff0;
    height:100%;
    width:100%;
    border-radius:50%;
}
.ball .borderline:nth-child(1){
    transform:rotatey(0deg);
}
.ball .borderline:nth-child(2){
    transform:rotatey(30deg);
}
.ball .borderline:nth-child(3){
    transform:rotatey(60deg);
}
.ball .borderline:nth-child(4){
    transform:rotatey(90deg);
}
.ball .borderline:nth-child(5){
    transform:rotatey(120deg);
}
.ball .borderline:nth-child(6){
    transform:rotatey(150deg);
}

.ball .borderline:nth-child(7){
    transform:rotatex(0deg);
}
.ball .borderline:nth-child(8){
    transform:rotatex(30deg);
}
.ball .borderline:nth-child(9){
    transform:rotatex(60deg);
}
.ball .borderline:nth-child(10){
    transform:rotatex(90deg);
}
.ball .borderline:nth-child(11){
    transform:rotatex(120deg);
}
.ball .borderline:nth-child(12){
    transform:rotatex(150deg);
}


.sun .borderline{
    box-shadow:0px 0px 20px 5px #ff0;
}


.ball_path_1{
    height:300px;
    width:300px;
    border:2px solid #ccc;
    border-radius:50%;
    transform-style:preserve-3d;
    animation:rotatez 10s linear infinite;
   // animation-iteration-count:infinite;
}
.ball_path_1 .ball{
    width:30px;
    height:30px;
    left:94%;
}
.ball_path_1 .ball .borderline{
    border:1px solid #0ff;
}


.ball_path_2{
    height:450px;
    width:450px;
    border:3px solid #ccc;
    border-radius:50%;
    transform-style:preserve-3d;
    animation:rotatex 20s linear infinite;
}
.ball_path_2 .ball{
    width:60px;
    height:60px;
    left:93%;
}
.ball_path_2 .ball .borderline{
    border:1px solid #85ff00;
}


.ball_path_3{
    height:600px;
    width:600px;
    margin-left:-50px;
    margin-top:-50px;
    border:3px solid #ccc;
    border-radius:50%;
    transform-style:preserve-3d;
    animation:rotatey 50s linear infinite;
}
.ball_path_3 .ball{
    width:90px;
    height:90px;
    left:93%;
}
.ball_path_3 .ball .borderline{
    border:1px solid #b400ff;
}


@keyframes rotate{
    0%{transform:rotatey(0deg) rotatex(0deg);}
    100%{transform:rotatey(360deg) rotatex(360deg);}
}
@keyframes rotatex{
    from{transform:rotatez(0deg)}
    to{transform:rotatez(360deg)}
}
@keyframes rotatey{
    from{transform:rotatez(0deg)}
    to{transform:rotatez(360deg)}
}
@keyframes rotatez{
    from{transform:rotatez(0deg)}
    to{transform:rotatez(360deg)}
}

        t1.css:

html,body{
    height:100%;
}
body{
    perspective:800px;
    background:#000;
}

.star{
    width:10px;
    height:10px;
    position:absolute;
    background:#fff;
    border-radius:50%;
    animation:translate 3s linear infinite;
    transform:translateZ(0px) scale(0);
}
.star:nth-child(1){
    top:25%;
    left:60%;
    animation-delay:2s;
}
.star:nth-child(2){
    top:39%;
    left:30%;
    animation-delay:5s;
}
.star:nth-child(3){
    top:77%;
    left:40%;
    animation-delay:3s;
}
.star:nth-child(4){
    top:55%;
    left:25%;
    animation-delay:4s;
}
.star:nth-child(5){
    top:26%;
    left:62%;
    animation-delay:7s;
}
.star:nth-child(6){
    top:43%;
    left:53%;
    animation-delay:1s;
}
.star:nth-child(7){
    top:24%;
    left:56%;
    animation-delay:3s;
}
.star:nth-child(8){
    top:83%;
    left:40%;
    animation-delay:0.5s;
}
.star:nth-child(9){
    top:10%;
    left:50%;
    animation-delay:2s;
}
.star:nth-child(10){
    top:25%;
    left:20%;
    animation-delay:5s;
}

@keyframes translate{
    0%{transform:translateZ(0px) scale(0);}
    100%{transform:translateZ(1000px) scale(1);}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值