CSS学习之动画--06

1. 过渡(transition)

过渡(transition)(eg:transition:all 2s; ),通过过渡可以指定一个属性发生变化时的切换方式,通过过渡可以创建一些非常好的效果,提升用户的体验。

  1. transition-property: 指定要执行过渡的属性,多个属性间使用,隔开 ,如果所有属性都需要过渡,则使用all关键字。
transition-property: height , width; 
 transition-property: all; 
  1. transition-duration: 指定过渡效果的持续时间
transition-duration: 100ms, 2s;
transition-duration: 2s;
  1. transition-timing-function: 过渡的时序函数,指定过渡的执行的方式 。可选值:

ease 默认值,慢速开始,先加速,再减速
linear 匀速运动
ease-in 加速运动
ease-out 减速运动
ease-in-out 先加速 后减速
cubic-bezier() 来指定时序函数
steps() 分步执行过渡效果,end --在时间结束时执行过渡(默认值), start–在时间开始时执行过渡

transition-timing-function: cubic-bezier(.24,.95,.82,-0.88);
transition-timing-function: steps(2, start);
  1. transition-delay: 过渡效果的延迟,等待一段时间后在执行过渡 。
transition-delay: 2s;
  1. transition 可以同时设置过渡相关的所有属性,只有一个要求,如果要写延迟,则两个时间中第一个是持续时间,第二个是延迟。
transition:2s margin-left 1s cubic-bezier(.24,.95,.82,-0.88);

例子如下:

.box2{
            background-color: #bfa;
    		transition:2s margin-left 1s cubic-bezier(.24,.95,.82,-0.88);
        }
        
.box3{
            background-color: orange;
            transition-property: all;
            transition-duration: 2s;
        }

        .box1:hover div{
            margin-left: 700px;
        }

<body>
    <div class="box1">
        <div class="box2"></div>
        <div class="box3"></div>
    </div>  
</body>

2. 动画(animation)–盒子左右移动两次

动画和过渡类似,都是可以实现一些动态的效果,不同的是过渡需要在某个属性发生变化时才会触发,动画可以自动触发动态效果。

设置动画效果,必须先要设置一个关键帧,关键帧设置了动画执行每一个步骤

  • animation-name: 要对当前元素生效的关键帧的名字;

    animation-name: test;

  • animation-duration: 动画的执行时间

    animation-duration: 4s;

  • 动画的延时

    animation-delay: 2s;

  • animation-iteration-count 动画执行的次数
    可选值:次数 或 infinite 无限执行

animation-iteration-count: 1;

  • animation-direction 指定动画运行的方向

animation-direction: alternate-reverse;

可选值
normal 默认值 从 from 向 to运行 每次都是这样
reverse 从 to 向 from 运行 每次都是这样
alternate 从 from 向 to运行 重复执行动画时反向执行
alternate-reverse 从 to 向 from运行 重复执行动画时反向执行

  • animation-play-state: 设置动画的执行状态

animation-play-state: paused;

可选值:
running 默认值 动画执行
paused 动画暂停

  • animation-fill-mode: 动画的填充模式

animation-fill-mode: both;

可选值:
none 默认值 动画执行完毕元素回到原来位置
forwards 动画执行完毕元素会停止在动画结束的位置
backwards 动画延时等待时,元素就会处于开始位置
both 结合了forwards 和 backwards

  • animation简写设置动画:

animation: test 2s 2 1s alternate;

设置box2的动画:

.box2{
            background-color: #bfa;
             animation: test 2s 2 1s alternate;
        }
.box1:hover div{
            animation-play-state: paused;
        }
 /* 设置动画碧玺先设置一个关键帧 */       
@keyframes test {
            /* from表示动画的开始位置 也可以使用 0% */
            from{
                margin-left: 0;
                background-color: orange;
            } 

            /* to动画的结束位置 也可以使用100%*/
            to{
                background-color: red;
                margin-left: 700px;
            }
        }

<body>
    <div class="box1">
        <div class="box2"></div>
    </div> 
</body>

3. 动画(animation)–创建小球下落动画

<style>
        .outer{
            height: 500px;
            border-bottom: 10px black solid;
            margin: 50px auto;
            overflow: hidden;
        }
        .outer div{
            float: left;
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background-color: #bfa;
            animation: ball .5s forwards linear infinite alternate;
        }

        div.box2{
            background-color: orange;
            animation-delay: .1s;
        }

        div.box3{
            background-color: yellow;
            animation-delay: .2s;
        }

        div.box4{
            background-color: yellowgreen;
            animation-delay: .3s;
        }

        div.box5{
            background-color: blue;
            animation-delay: .4s;
        }
        div.box6{
            background-color: pink;
            animation-delay: .5s;
        }
        div.box7{
            background-color: tomato;
            animation-delay: .6s;
        }
        div.box8{
            background-color: skyblue;
            animation-delay: .7s;
        }
        div.box9{
            background-color: chocolate;
            animation-delay: .8s;
        }

        /* 关键帧:创建小球下落的动画 */
        @keyframes ball {
            from{
                margin-top: 0;
            }
            
            to{
                margin-top: 400px;
            }
        }
    </style>
<body>
    <div class="outer">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        <div class="box4"></div>
        <div class="box5"></div>
        <div class="box6"></div>
        <div class="box7"></div>
        <div class="box8"></div>
        <div class="box9"></div>
    </div>   
</body>

4. 变形–translate()

变形就是指通过CSS来改变元素的形状或位置,变形不会影响到页面的布局,transform 用来设置元素的变形效果。

平移:
translateX() 沿着x轴方向平移
translateY() 沿着y轴方向平移
translateZ() 沿着z轴方向平移
平移元素,百分比是相对于自身计算的

transform: translateX(100%);
body{
            background-color: rgb(236, 236, 236);
        }
.box1{
            width: 200px;
            height: 200px;
            background-color: #bfa;
            margin: 0 auto;
			transform: translateX(100%);
        }
.box4, .box5{
            width: 220px;
            height: 300px;
            background-color: #fff;
            float: left;
            margin: 0 10px;
            transition:all .3s;
        }
.box4:hover,.box5:hover{
            transform: translateY(-4px);
            box-shadow: 0 0 10px rgba(0, 0, 0, .3)
        }

<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box4"> </div>
    <div class="box5"></div>
</body>

4.1 z轴平移

z轴平移,调整元素在z轴的位置,正常情况就是调整元素和人眼之间的距离。距离越大,元素离人越近。
z轴平移属于立体效果(近大远小),默认情况下网页是不支持透视,如果需要看见效果,必须要设置网页的视距。

html{
			/* 设置当前网页的视距为800px,人眼距离网页的距离 */
            perspective: 800px;
        }
body{
            border: 1px red solid;
            background-color: rgb(241, 241, 241);
        }
.box1{
            width: 200px;
            height: 200px;
            background-color: #bfa;
            margin: 200px auto;
    		transition:2s;
        }
body:hover .box1{
            transform: translateZ(800px);
        }
 
        
<body>
    <div class="box1"></div>
</body>

4.2 旋转

通过旋转可以使元素沿着x y 或 z旋转指定的角度,rotateX(),rotateY(),rotateZ()。

鼠标控制图片旋转:

html{
            perspective: 800px;
        }
body{
            border: 1px red solid;
            background-color: rgb(241, 241, 241);
        }
.box1{
            width: 320px;
            height: 320px;
            margin: 200px auto;
            transition:2s;
        }
body:hover .box1{
			transform: rotateY(180deg);
			/* 是否显示元素的背面 */
			backface-visibility: hidden;
}

<body>
    <div class="box1">
        <img src="an.jpg" alt="">
    </div>
</body>

4.3 缩放

对元素进行缩放的函数:

scaleX() 水平方向缩放
scaleY() 垂直方向缩放
scale() 双方向的缩放

变形的原点 默认值 center

实现鼠标放到元素上,放大;鼠标移开,缩小;

<style>
        html{
            perspective:800px;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: #bfa;
            transition:2s;
            margin: 100px auto;
            /* 变形的原点 默认值 center*/
            /* transform-origin:20px 20px;  */
        }
        .box1:hover{
            /*  对元素进行缩放的函数:  */
            transform:scale(2)
        }
        .img-wrapper{
            width: 200px;
            height: 200px;
            border: 1px red solid;
            overflow: hidden;
        }
        img{
            transition: .2s;
        }
        .img-wrapper:hover img{
            transform:scale(1.2);
        }
</style>

<body>
    <div class="box1"></div>
    <div class="img-wrapper">
        <img src="an.jpg" width="100%">
    </div>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值