【CSS】过渡属性

过渡

过渡transition

  • 要发生过渡的css属性
    • 默认值all
    • transition-property: width, background-color
  • 过渡的持续时间
    • s ms 1s=1000ms
    • 过度的持续时间必须写
    • 默认是0s
    • transition-duration: 4s
  • 触发过渡的延迟时间
    • s ms 1s= 1000ms
    • transition-delay: 2s
<style>
    div {
      width: 500px;
      height: 400px;
      background-color: pink;
      /* 过渡 */
      /* 要发生过渡的css属性 */
      /* transition-property: width, background-color; */
      /* 过渡的持续时间 */
      transition-duration: 4s;
      /* 过渡的延迟时间 */
      /* transition-delay: 2s; */
    }
    div:hover {
      width: 600px;
      background-color: yellow;
    }
</style>

💡执行结果截图
请添加图片描述

过度的中间值

display:none和visibility:hidden不可用
原因:这两个是极端值,只有消失和出现两种状态,不能过度
解决:使用opcity:0 这个有中间值

<style>
    .baba {
      width: 400px;
      height: 500px;
      background-color: green;
      margin: 50px;
    }


    .son {
      width: 200px;
      height: 200px;
      background-color: blue;
      transition-duration: 2s;
    }

    .baba:hover .son {
      /* 是极端值 只有消失和出现两个状态 不能过渡 */
      /* display: none; */
      /* visibility: hidden; */


      /* 有中间值  */
      opacity: 0;
    }
  </style>
  <!-- 鼠标放到baba  son慢慢消失 -->
  <div class="baba">
    <div class="son"></div>
  </div>

💡执行结果截图
请添加图片描述

过度属性的运动状态

transition-timing-function
先块后慢
transition-timing-function:ease;

匀速
transition-timing-function:linear;

<style>
    .father {
      width: 800px;
      height: 300px;
      border: 1px solid #000;
      margin: 30px auto;
    }


    .father div {
      width: 100px;
      height: 100px;
      margin: 20px;
      background-color: pink;
      transition-duration: 2s;
    }


    .father:hover div {
      width: 500px;
    }


    .ease {
      /* 默认  先快后慢*/
      transition-timing-function: ease;
    }


    .linear {
      /* 匀速 */
      transition-timing-function: linear;
    }
  </style>

💡执行结果截图
请添加图片描述

小结:
过渡是主动触发的
一般过渡只有鼠标移入才有效
所以过渡属性要写在元素的默认状态中

过度的简写
transition:要发生过度的属性 持续的时间 延迟时间 运动状态

  • 属性顺序不强调
  • 持续时间和延迟时间有先后顺序 持续时间在前
  • 持续时间必须写

最简单的过度写法
transition:2s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芒果Cake

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值