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

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.


  • 2019-10-14 21:18:57

    Comparable 的 使用

    要做这个呢,我们也是用到了Arrays.sort 这个排序的方法!但不同的是,我们之前用的是int数组,现在我们用的是这个UserBean数组。如果你想对这个UserBean数组进行排序,你要多做一件事,就是让这个 UserBean类去 实现Comparable 的接口,并重写 里面  comparaTo 的方法。注意,这个接口是可以提供泛型的 ———————————————— 版权声明:本文为CSDN博主「sdn_bt496」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明

  • 2019-10-15 05:53:20

    xUtils 里的DbUtils使用心得

    使用xUtils做Android数据库开发非常简便和得心应手,而且它本身还支持很多查询功能,比如一对多,select count和自定义sql查询等,并且支持事务(默认关闭) 下面是官方sample给的代码和我的一些使用心得 首先是两个实体类,对应两张表,这两张表中有一对多的关系

  • 2019-10-15 09:18:48

    腾讯 Android 面试笔试总结

    Activity中的几种启动模式 Android消息机制 IntentService 事件分发 Android性能优化、内存优化 内存优化 View的绘制 Eventbus原理 Rxjava的操作符有哪些,说说他们的作用 线程锁 锁方法和类对象啥的有啥区别 AsyncTask原理 说说MVP和MVVM的特点 Android中用到的观察者模式有哪些地方 说说google新出的Lifecycle框架 okhttp原理 Retrofit原理 RecyclerView源码、缓存分析 Binder机制 Android Jetpack Kotlin Activity中的几种启动模式

  • 2019-10-15 09:20:49

    SpringBoot注解梳理

    @SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。 @Configuration 等同于spring的XML配置文件;使用Java代码可以检查类型安全。 @EnableAutoConfiguration 自动配置。 @ComponentScan 组件扫描,可自动发现和装配一些Bean。 @Component可配合CommandLineRunner使用,在程序启动后执行一些基础任务。 @RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。 @Autowired自动导入。 @PathVariable获取参数。 @JsonBackReference解决嵌套外链问题。 @RepositoryRestResourcepublic配合spring-boot-starter-data-rest使用。

  • 2019-10-15 09:52:00

    动图解释递归,按值传递和按引用传递的区别,线性查找和二分查找,二叉查找树

    对于大部分人,数据结构一直是一个短板,当然我也是,不是学不会,而是容易忘,就拿最简单的排序来说吧,当时学习的时候明明已经弄得很清楚了,过了一段时间不用又忘记了,还要重新再看一遍,不知道有多少小伙伴和我有一样的烦恼。今天让我们用用动图的方式学习一下数据结构中的递归和二分查找吧,这种讲解方式非常生动,而且非常容易记住和理解。