列表加载动画 列表增加移除的动画

2020-12-01 16:59:05

参考地址 Animating List Items

When parts of a web page change, adding some animation is a good way to help your viewers understand what’s going on. Animations can announce the arrival of new content, or draw attention to content that’s in the process of being removed. In this article we’ll look at how this can be used to help introduce new content, by showing and hiding items in a list.

  • List item

  • List item

Introducing content

Animation can be useful when helping visitors understand when things change on your site. When content is added or removed without any animation, they can miss sudden changes and be confused. Adding subtle animations can avoid this and help by “announcing” that something is going to leave the page or be introduced to it.

One example of adding or removing content is managing the content of a list. Most of the animations can be used for other sorts of content. If you find them useful, or have other ideas to add, do get in touch, we love to hear your thoughts.

Setting up the HTML

To get started we’ll use a pre-filled list and a button to add new items to the list.

<ul id="list">
  <li class="show">List item</li>
  <li class="show">List item</li></ul><button id="add-to-list">Add a list item</button>

A few things to note. Firstly we have two IDs in the HTML. Generally we don’t use IDs for styling, as they can introduce problems with their specificity. However they’re useful when using JavaScript.

The initial items have the class “show”, as this is a class we’ll use later to add the animation effect.

A little JavaScript

For the purpose of the demo we’ll create a little JavaScript to add a new item to the list, then add the “show” class so that the animation can take place. There’s a reason for using this two-step process. If the list items were added in a visible state, there wouldn’t be any time for the transition to take place.

We could get around this by using an animation on the li elements, but this would be more difficult to override when removing the elements with another animation.

/*
 * Add items to a list - from cssanimation.rocks/list-items/
 */document.getElementById('add-to-list').onclick = function() {  var list = document.getElementById('list');  var newLI = document.createElement('li');
  newLI.innerHTML = 'A new item';
  list.appendChild(newLI);
  setTimeout(function() {
    newLI.className = newLI.className + " show";
  }, 10);
}

No animation

At it’s most basic we can write some CSS to show the list items. We’re using the show class as a way of setting them as visible, so removing the show class should also cause them to disappear.

li {  list-style: none;  background: #d1703c;  color: #fff;  height: 0;  line-height: 2em;  margin: 0;  padding: 0 0.5em;  overflow: hidden;  width: 10em;}li.show {  height: 2em;  margin: 2px 0;}

In these styles we’re setting up the li elements to look like rectangles, without the bullet points and giving them a height of 0, a margin of 0 and set overflow to hidden. This is so that they will appear invisible until we apply a show class.

The show class applies a height and margin. Since we’re not using animation yet, the items should appear suddenly on the page, like so. Also try pressing the list items to see them disappear.

  • List item

  • List item

Fade

As a first animation we’ll add a simple fade effect. The list items appear a little more gradually than before. Visually this still looks a little clunky but has the benefit of giving viewers longer to notice that something is happening.

  • List item

  • List item

To add the effect I’ve created a separate snippet of CSS. To have this apply to the list, apply the class fade to a container surrounding your list.

.fade li {  transition: all 0.4s ease-out;  opacity: 0;  height: 2em;}.fade li.show {  opacity: 1;}

Slide down & Fade

The sudden jump each time an item is added or removed is a litle jarring. Let’s have the height adjust as well, to create a smoother sliding effect.

  • List item

  • List item

The difference between this and the fade class above is only that the height: 2em has been removed. Since the show class contains a set height (inherited from the first CSS snippet), it will transition the height automatically.

.slide-fade li {  transition: all 0.4s ease-out;  opacity: 0;}.slide-fade li.show {  opacity: 1;}

Swinging in

Along with fading and sliding, we can go further by adding a little 3D effect. Browser can transform elements in more than the X or Y directions, useful for adding depth to scenes.

  • List item

  • List item

To set this up, we need to define the containing section as a stage within which the 3D transitions take place. We do this by giving it a perspective value.

Perspective in CSS is the depth of the scene. A lower number means a more shallow perspective, with more extreme angles. It’s worth playing with this value to find a look that works for you.

.swing {  perspective: 100px;}

Next we set up the li elements to transform into place. We’ll use opacity to create a fade effect as before, but add in a transform to rotate the li into place.

.swing li {  opacity: 0;  transform: rotateX(-90deg);  transition: all 0.5s cubic-bezier(.36,-0.64,.34,1.76);}.swing li.show {  opacity: 1;  transform: none;  transition: all 0.5s cubic-bezier(.36,-0.64,.34,1.76);}

In this example we’re beginning with the li rotated back by 90 degrees. When the show class it added, this transform is set to none, allowing it to transition into place. To give it a swinging effect I’ve used the cubic-bezier timing function.

Swinging from side

We can tweak this effect to create different styles quite easily. Here’s an example where the items swing in from the side.

  • List item

  • List item

To create this effect we only need change the axis of rotation.

.swing li {  opacity: 0;  transform: rotateY(-90deg);  transition: all 0.5s cubic-bezier(.36,-0.64,.34,1.76);}

All we’ve changed is rotateX to rotateY.

Prefixes and browser testing

The code included above does not include any prefixes, for readability. It’s strongly recommended to add prefixes to support browsers that need the -webkit prefix or others. I use Autoprefixer to save worrying about these things.

As these animations are layered on top of the basic show / hide mechanism, they should degrade gracefully on browsers that don’t support the animations. Testing on various devices and browsers is important but most modern browsers should be able to support these animations.


  • 2020-03-14 23:39:59

    vuetify和@nuxt/vuetify icon 之我见

    vuetify中v-icon,貌似默认支持 Material Design Icons, Material Icons, Font Awesome 4 and Font Awesome 5, 我自己单独引入了vuetify 用哪一个图标都没有问题。但是用了@nuxt/vuetify只能用mdi-home这样的。不知道因为啥。肯定是封装后,封装成一个了。 但是我修改vuetify的设置,哪一个图标也都能用。哎,不过多研究了。

  • 2020-03-16 15:57:53

    nuxtjs中单独引入Message组件的问题

    // 引入elementUIimport { Message } from 'element-ui';//由于Message组件并没有install 方法供Vue来操作的,是直接返回的,因此按照官方文档单独引入的方法是//会报错的,需要给 Message 添加 install 方法Message.install = function (Vue, options) {Vue.prototype.$message = Message}Vue.use(Message )//消息提示

  • 2020-03-16 16:03:20

    css的var()函数

     随着sass,less预编译的流行,css也随即推出了变量定义var函数。var()函数,就如同sass和less等预编译软件一样,可以定义变量并且进行对应的使用。

  • 2020-03-16 16:52:05

    对icomoon的误解,以及最快速的使用

    此时需要注意顶部第一个选项,Quick Usage,一定要打开,Enable Quick Usage,谁让咱英语不好呢,这个时候会出现一个css连接,直接引用就好了,就可以随意使用图标了,引入这一个css就能实现我们的功能,省区引入太多文件的烦恼,你可以在浏览器打开这个css,可以看到里面把我们所用的文件整成base64了。所以挺好用的。

  • 2020-03-17 09:47:05

    video标签视频不自动播放的问题

    添加 muted 属性,就可以通过地址栏进入网页的时候自动播放了,手机端还是有的有限制的,比如iphone浏览器,就不行,苹果手机为了保护用户的流量和用户的意愿,是禁止自动播放的,必须有手动触发。

  • 2020-03-17 14:21:31

    nuxt+pm2 自动化部署及打包后文件自动上传阿里云 oss(精华)

    部署nuxtjs,这一篇文章就够了,pm2 代码自动发布依赖于 git 工具,先将 ssh 密钥配置再你的代码仓库(github 或者 gitLab),具体操作自行 google 或者点击github 配置 ssh。 使用 ssh 密钥链接服务器 s $ ssh-copy-id root@1.2.3.4 # 把本机的 SSH 秘钥添加至服务器,配置成功后,以后就不需要再执行这条 SSH 命令了