CSS3学习笔记(五)

2019-08-27  本文已影响0人  dev_winner
具有过渡的CSS属性 过渡函数
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>变形与动画</title>
    <style type="text/css">
    .test1 {
        width: 100px;
        height: 100px;
        background: pink;
        margin: 20px auto;
        /*过渡的属性为宽度*/
        -webkit-transition: width;
        transition: width;
        /*完成过渡所需时间为0.5s*/
        -webkit-transition-duration: .5s;
        transition-duration: .5s;
        -webkit-transition-timing-function: ease-in;
        transition-timing-function: ease-in;
        -webkit-transition-delay: .18s;
        transition-delay: .18s;
    }
    .test1:hover {
        width: 300px;
    }
    .test2 {
        width: 100px;
        height: 100px;
        background-color: red;
        margin: 20px auto;
        -webkit-transition: background-color .5s ease .1s;
        transition: background-color .5s ease .1s;
    }
    .test2:hover {
        background-color: green;
    }
    /*在鼠标悬停(hover)状态下,让容器从直角慢慢过渡到圆角,并让整个动画持续0.5s。*/
    .test3 {
        width: 150px;
        height: 100px;
        background-color: orange;
        margin: 20px auto;
        -webkit-transition-property: -webkit-border-radius;
        transition-property: border-radius;
        -webkit-transition-duration: .5s;
        transition-duration: .5s;
        -webkit-transition-timing-function: ease-out;
        transition-timing-function: ease-out;
        -webkit-transition-delay: .2s;
        transition-delay: .2s;
    }
    .test3:hover {
        border-radius: 20px;
    }
    /*在hover状态下,让容器从一个正方形慢慢过渡到一个圆形,而整个过渡是先加速再减速,也就是运用ease-in-out函数。*/
    .test4 {
        width: 100px;
        height: 100px;
        background: blue;
        margin: 20px auto;
        -webkit-transition-property: -webkit-border-radius;
        transition-property: border-radius;
        -webkit-transition-duration: .5s;
        transition-duration: .5s;
        -webkit-transition-timing-function: ease-in-out;
        transition-timing-function: ease-in-out;
        -webkit-transition-delay: .2s;
        transition-delay: .2s;
    }
    .test4:hover {
        border-radius: 100%;
    }
    </style>
</head>
<body>
    <div class="test1"></div>
    <div class="test2"></div>
    <div class="test3"></div>
    <div class="test4"></div>
</body>
</html>
过渡效果展示
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>变形与动画</title>
    <style type="text/css">
    .wrapper {
        width: 400px;
        height: 200px;
        margin: 20px auto;
        border: 2px dotted red;
        position: relative;
    }
    .wrapper div {
        padding: 15px 20px;
        color: #fff;
        background-color: orange;
        position: absolute;
        top: 50%;
        left: 50%;
        -webkit-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
        /*all设置所有过渡属性都起作用*/
        -webkit-transition: all .5s ease-in .2s;
        transition: all .5s ease-in .2s;
    }
    .wrapper div:hover {
        background-color: red;
        border-radius: 10px;
    }
    </style>
</head>
<body>
    <div class="wrapper">
        <div>鼠标放到我的身上来</div>
    </div>
</body>
</html>
过渡多个CSS属性
@keyframes changecolor{
  0%{
   background: red;
  }
  100%{
    background: green;
  }
}
属性值 效果
none 默认值,表示动画在延迟开始时间内显示背景颜色(而不是初始帧!);开始后按预期进行和结束,在动画完成其最后一帧时,动画会回到背景颜色的状态
forwards 表示动画在结束后继续应用最后的关键帧的位置(即保持末态关键帧)
backwards 会在向元素应用动画样式时迅速应用动画的初始帧,即若与背景颜色不同,在动画的开始延迟时间内显示的每一帧为初始帧状态,在动画完成其最后一帧时显示为背景颜色的状态
both 元素动画同时具有forwards和backwards效果,即动画在第一关键帧上等待动画开始,在动画结束时停在最后一个关键帧上而不回到动画的第一帧上。
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>变形与动画</title>
    <style type="text/css">
    @keyframes changecolor {
        0% {
            background: red;
        }
        20% {
            background: blue;
        }
        40% {
            background: orange;
        }
        60% {
            background: green;
        }
        80% {
            background: yellow;
        }
        100% {
            background: red;
        }
    }
    div {
        width: 300px;
        height: 200px;
        background: red;
        color: #fff;
        margin: 20px auto;
        text-align: center;
    }
    div:hover {
        /*animation 属性是一个简写属性,将动画与 div 元素绑定*/
        animation: changecolor 5s ease-out .2s;
    }
    </style>
</head>
<body>
    <div>鼠标放在我身上</div>
</body>
</html>
设置动画播放效果
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>变形与动画</title>
    <style type="text/css">
    @keyframes move {
        0% {
            transform: translateY(90px);
        }
        15% {
            transform: translate(90px, 90px);
        }
        30% {
            transform: translate(180px, 90px);
        }
        45% {
            transform: translate(90px, 90px);
        }
        60% {
            transform: translate(90px, 0);
        }
        75% {
            transform: translate(90px, 90px);
        }
        90% {
            transform: translate(90px, 180px);
        }
        100% {
            transform: translate(90px, 90px);
        }
    }
    div {
        width: 200px;
        height: 200px;
        border: 1px solid red;
        margin: 20px auto;
    }
    span {
        display: inline-block;
        width: 20px;
        height: 20px;
        background: orange;
        transform: translateY(90px);
        animation-name: move;
        animation-duration: 5s;
        animation-timing-function: ease-in;
        animation-delay: .2s;
        animation-iteration-count: infinite;
        animation-direction: alternate;
        animation-play-state: paused;
    }
    div:hover span {
        /*设置从暂停的位置继续播放*/
        animation-play-state: running;
    }
    </style>
</head>
<body>
    <div><span></span></div>
</body>
</html>
设置动画暂停后继续播放状态
上一篇 下一篇

猜你喜欢

热点阅读