FLIP动画,让元素动起来
Posted undefined
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FLIP动画,让元素动起来相关的知识,希望对你有一定的参考价值。
FLIP动画 概述
什么是FLIP动画技术?FLIP其实是一种思路
, 一种方法论
, 并不是具体指代某个技术.
FLIP, 具体意义如下:
- F first 参加过渡元素的初始状态
- L last 元素的终止状态
- I invert flip的核心, 通过first和last计算, 然后翻转这个过程
- P play 启用tansition, 移除你invert的改变,这时候动画会按预期完成
实践操作,利用FLIP思路完成过渡动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.row span {
padding: 0 5px;
display: inline-block;
}
</style>
</head>
<body>
<div class="row" id="row">
</div>
<button onclick="randomFLIP()">点击打乱</button>
</body>
<script>
const render = (arr) => {
document.getElementById(\'row\').innerHTML = arr.map(v => `<span id="k${v}">${v}</span>`).join(\'\')
}
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
render(arr)
// 随机打乱
function shuffle(arr) {
for (var i = arr.length - 1; i >= 0; i--) {
var randomIndex = Math.floor(Math.random() * (i + 1));
var itemAtIndex = arr[randomIndex];
arr[randomIndex] = arr[i];
arr[i] = itemAtIndex;
}
return arr;
}
// FLIP动画函数
function randomFLIP() {
const randomArr = shuffle(arr)
// First
const firstRect = (() => {
const result = {}
arr.map(v => {
result[`k${v}`] = document.getElementById(`k${v}`).getBoundingClientRect()
})
return result
})()
render(randomArr)
// Last
const lastRect = (() => {
const result = {}
randomArr.map(v => {
result[`k${v}`] = document.getElementById(`k${v}`).getBoundingClientRect()
})
return result
})()
// invert
arr.forEach(v => {
const first = firstRect[`k${v}`]
const last = lastRect[`k${v}`]
const deltaX = first.left - last.left;
const deltaY = first.top - last.top;
document.getElementById(`k${v}`).style = `transform: translate(${deltaX}px, ${deltaY}px);`
requestAnimationFrame(function () {
// Play 去掉translate, 加上动画
document.getElementById(`k${v}`).style = \'transition: all 1s\'
document.getElementById(`k${v}`).style.transform = \'\'
})
})
}
</script>
</html>
在线预览
http://jsrun.net/ZIVKp
以上是关于FLIP动画,让元素动起来的主要内容,如果未能解决你的问题,请参考以下文章