css动画 --- transition

前言

页面效果是很重要的一环,好的页面效果,可以让用户感觉很舒服,进而能吸引更多的用户。
既然是页面效果,那么动画肯定不可或缺,那么这篇先说下 transition 这个 css3 属性。

初探 transition

transitions 提供了一种在更改 CSS 属性时控制动画速度的方法。其可以让属性变化成为一个持续一段时间的过程,而不是立即生效 的。(摘自 MDN

用法

  • 其写法(为了兼容,要加厂商前缀,这里不写咯):

    transition: <property> <duration> <timing-function> <delay>
  • 也可以用逗号隔开,多写几个作用于不用的 property(属性)

    transition: <property> <duration> <timing-function> <delay>, <property> <duration> <timing-function> <delay>

各项作用

@默认值作用
propertynone / all / property all作用于需要过渡的属性
durationtime0过渡所需时间
timing-functionlinear / ease / ease-in / ease-out / ease-in-out / cubic-bezier(n,n,n,n) ease规定过渡效果的速度曲线
delaytime (同duration)0延迟多久才开始过渡
  1. property:可过渡的 CSS 属性,有些属性则不行。
  2. time:以秒(如1s)或毫秒(如1000)计。
  3. cubic-bezier(x1,y1,x2,y2)三阶贝塞尔曲线linearease 等等都是由它计算出来的。x轴 的取值范围:[0,1] 区间内, y轴 可以任意值。可在线自定义

动手使用下

基本操作

// css
#box {
  width: 100px;
  height: 100px;
  background-color: aqua;
  transition: all 1s;
  -webkit-transition: all 1s;
  -moz-transition: all 1s;
  -ms-transition: all 1s;
  -o-transition: all 1s;
}

#box:hover {
  width: 500px;
  margin-top: 100px;
}

// html
<div id="box"></div>

图片描述

  • #boxstyle 里有transition,要触发它,则需要改变上面所说的 property
  • 当鼠标移到 div 时,widthmarin-top 发生了变化,触发了 transition
  • all : 所有可过渡的属性都可以的意思。
  • 1s : 在第二项,即 duration 。过渡所需时间。

简单的类似图片无缝滑动

// css
* {
  margin: 0;
  padding: 0;
}

#box {
  margin: 0 auto;
  width: 100px;
  height: 100px;
  overflow: hidden;
  
}

#img_container {
  position: relative;
  width: 400px;
  transition: all .7s linear;
  -webkit-transition: all .7s linear;
  -moz-transition: all .7s linear;
  -ms-transition: all .7s linear;
  -o-transition: all .7s linear;
}

#img_container span {
  display: inline-block;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  background-color: yellow;
}

#img_container span:nth-of-type(even) {
  background-color: red;
}

ul {
  text-align: center;
}

li {
  padding: 10px;
  cursor: pointer;
  list-style: none;
  display: inline-block;
}

// html
<div id="box">
  <section id="img_container">
     <span>1</span><span>2</span><span>3</span><span>4</span>
  </section>
</div>
<ul>
  <li>图一</li>
  <li>图二</li>
  <li>图三</li>
  <li>图四</li>
</ul>

// js
~function () {
    let lis    = document.getElementsByTagName('li');
    let imgBox = document.getElementById('img_container');
    let width  = +document.querySelector('span').clientWidth;
    
    for (let j = 0;j < lis.length;j++) {
      
      lis[j].addEventListener('click',function() {
        imgBox.style.marginLeft = `-${ width * j }px`;
      });

    }

}()

图片描述

  • 这个 demo 是用 js 改变了 property ,从而触发 transition

滚动到某个位置,动画才出现

// css
* {
  margin: 0;
  padding: 0;
}

body {
  height: 1500px;
}


#box {
  height: 1200px;
  text-align: center;
}

#animation {
  width: 100px;
  height: 100px;
  background-color: red;
  transition: transform 1s cubic-bezier(.1,1.92,.71,.53);
  -webkit-transition: transform 1s cubic-bezier(.1,1.92,.71,.53);
  -moz-transition: transform 1s cubic-bezier(.1,1.92,.71,.53);
  -ms-transition: transform 1s cubic-bezier(.1,1.92,.71,.53);
  -o-transition: transform 1s cubic-bezier(.1,1.92,.71,.53);
}

// html
<div id="box">
  请往下滚动到有动画的地方,或者点击 <a href="#here">这里</a>
</div>
<span id="here"></span>
<div id="animation"></div>

// js 
let animation = function () {
    let animationBox = document.getElementById('animation');

    function show () {
      animationBox.style.transform = 'translateX(600px) scale(3,3) rotate(360deg)';
    }

    function init () {
      animationBox.style.transform = 'translateX(0) scale(1,1) rotate(-360deg)';
    }

    return { show, init };
  }()

  window.onscroll = function () {
    let scrollTop = +window.scrollY;

    if (scrollTop > 650) {
      animation.show();
    } else {
      animation.init();
    }
  }

图片描述

  • 因为主要演示动画,所以没写滚动的节流或防抖了 。
  • 当滚动或点击到动画的位置,动画才开始。
  • css 代码可以看到,transition 要通过 transform 的变化来触发。同时我也用到了 cubic-bezier

最后

这里不深入讲解 cubic-bezier ,因为我看原理也是头晕。。。。
transition 还可以实现很多功能,淡出淡入手风琴效果 等等。等着你们去发现。
其实动画不是特别难的,只要你有颗 骚动(好奇)的心。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值