Commit f553889c authored by wu.hui's avatar wu.hui

feat: 动画改成js控制

parent c48e51c7
...@@ -48,9 +48,9 @@ ...@@ -48,9 +48,9 @@
margin-left: 10px; margin-left: 10px;
} }
.stat-bar .stat-item .scroll-num { .stat-bar .stat-item .scroll-num {
width: var(--width, 39px); width: 39px;
height: var(--height, 50px); height: 50px;
line-height: var(--height, 50px); line-height: 50px;
text-align: center; text-align: center;
font-size: 41px; font-size: 41px;
color: #01fff6; color: #01fff6;
...@@ -66,96 +66,3 @@ ...@@ -66,96 +66,3 @@
color: #758094; color: #758094;
font-family: 'Source Han Sans CN'; font-family: 'Source Han Sans CN';
} }
.stat-bar .animate {
-webkit-animation: move calc(var(--duration) * 1s) calc(var(--delay) * 1s) linear, bounce-in-down 1s calc((var(--duration) + var(--delay)) * 1s) forwards;
animation: move calc(var(--duration) * 1s) calc(var(--delay) * 1s) linear, bounce-in-down 1s calc((var(--duration) + var(--delay)) * 1s) forwards;
}
.stat-bar .init {
-webkit-transform: translateY(calc(var(--i) * -9.09%));
-ms-transform: translateY(calc(var(--i) * -9.09%));
transform: translateY(calc(var(--i) * -9.09%));
}
@-webkit-keyframes move {
from {
-webkit-transform: translateY(calc(var(--i) * -9.09%));
transform: translateY(calc(var(--i) * -9.09%));
-webkit-filter: url(#blur);
filter: url(#blur);
}
to {
-webkit-transform: translateY(calc(var(--j) * -9.09%));
transform: translateY(calc(var(--j) * -9.09%));
-webkit-filter: url(#blur);
filter: url(#blur);
}
}
@keyframes move {
from {
-webkit-transform: translateY(calc(var(--i) * -9.09%));
transform: translateY(calc(var(--i) * -9.09%));
-webkit-filter: url(#blur);
filter: url(#blur);
}
to {
-webkit-transform: translateY(calc(var(--j) * -9.09%));
transform: translateY(calc(var(--j) * -9.09%));
-webkit-filter: url(#blur);
filter: url(#blur);
}
}
@-webkit-keyframes bounce-in-down {
from {
-webkit-transform: translateY(calc(var(--j) * -9.09% - 1%));
transform: translateY(calc(var(--j) * -9.09% - 1%));
-webkit-filter: none;
filter: none;
}
25% {
-webkit-transform: translateY(calc(var(--j) * -9.09% + 0.8%));
transform: translateY(calc(var(--j) * -9.09% + 0.8%));
}
50% {
-webkit-transform: translateY(calc(var(--j) * -9.09% - 1%));
transform: translateY(calc(var(--j) * -9.09% - 1%));
}
70% {
-webkit-transform: translateY(calc(var(--j) * -9.09% + 0.6%));
transform: translateY(calc(var(--j) * -9.09% + 0.6%));
}
85% {
-webkit-transform: translateY(calc(var(--j) * -9.09% - 0.3%));
transform: translateY(calc(var(--j) * -9.09% - 0.3%));
}
to {
-webkit-transform: translateY(calc(var(--j) * -9.09%));
transform: translateY(calc(var(--j) * -9.09%));
}
}
@keyframes bounce-in-down {
from {
-webkit-transform: translateY(calc(var(--j) * -9.09% - 1%));
transform: translateY(calc(var(--j) * -9.09% - 1%));
-webkit-filter: none;
filter: none;
}
25% {
-webkit-transform: translateY(calc(var(--j) * -9.09% + 0.8%));
transform: translateY(calc(var(--j) * -9.09% + 0.8%));
}
50% {
-webkit-transform: translateY(calc(var(--j) * -9.09% - 1%));
transform: translateY(calc(var(--j) * -9.09% - 1%));
}
70% {
-webkit-transform: translateY(calc(var(--j) * -9.09% + 0.6%));
transform: translateY(calc(var(--j) * -9.09% + 0.6%));
}
85% {
-webkit-transform: translateY(calc(var(--j) * -9.09% - 0.3%));
transform: translateY(calc(var(--j) * -9.09% - 0.3%));
}
to {
-webkit-transform: translateY(calc(var(--j) * -9.09%));
transform: translateY(calc(var(--j) * -9.09%));
}
}
...@@ -254,5 +254,6 @@ ...@@ -254,5 +254,6 @@
</body> </body>
<script src="https://cdn.ucyber.cn/common/echarts.js"></script> <script src="https://cdn.ucyber.cn/common/echarts.js"></script>
<script src="./js/tween.umd.js"></script>
<script src="./js/index.js"></script> <script src="./js/index.js"></script>
</html> </html>
\ No newline at end of file
...@@ -219,13 +219,11 @@ const lineChartOption = (xData, yData, interval) => { ...@@ -219,13 +219,11 @@ const lineChartOption = (xData, yData, interval) => {
} }
// 创建多组数字 // 创建多组数字
function createMultiSrollNum(numArr, width, height) { function createMultiSrollNum(numArr) {
const scrollBox = document.getElementsByClassName('scroll-box') const scrollBox = document.getElementsByClassName('scroll-box')
for (let i = 0; i < scrollBox.length; i++) { for (let i = 0; i < scrollBox.length; i++) {
const scrollNode = scrollBox[i] const scrollNode = scrollBox[i]
const childNumArr = numArr[i].split('') const childNumArr = numArr[i].split('')
scrollNode.style.setProperty('--width', width + 'px')
scrollNode.style.setProperty('--height', height + 'px')
const docFrag = document.createDocumentFragment() const docFrag = document.createDocumentFragment()
childNumArr.forEach((num, i) => { childNumArr.forEach((num, i) => {
docFrag.appendChild(createSrollNum(num)) docFrag.appendChild(createSrollNum(num))
...@@ -250,63 +248,95 @@ function createSrollNum(num) { ...@@ -250,63 +248,95 @@ function createSrollNum(num) {
<li>8</li> <li>8</li>
<li>9</li> <li>9</li>
<li>0</li> <li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
</ul>` </ul>`
const ulNode = scrollNumNode.childNodes[0] const ulNode = scrollNumNode.childNodes[0]
ulNode.setAttribute( ulNode.setAttribute('style', 'transform: translateY(' + num * -5 + '%)')
'style',
'transform: translateY(' + num * -9.09 + '%)'
)
return scrollNumNode return scrollNumNode
} }
// 创建多组数字动画 // 创建多组数字动画
function addMultiDigitAnimate(beforeNumArr, afterNumArr, delays, numArr) { function addMultiDigitAnimate(beforeNumArr, afterNumArr) {
TWEEN.removeAll()
console.log(beforeNumArr, afterNumArr)
const arr = []
const scrollBox = document.getElementsByClassName('scroll-box') const scrollBox = document.getElementsByClassName('scroll-box')
for (let i = 0; i < scrollBox.length; i++) { for (let i = 0; i < scrollBox.length; i++) {
const scrollBoxNode = scrollBox[i] const scrollNode = scrollBox[i]
const scrollNum = scrollBoxNode.childNodes const beforeNum = beforeNumArr[i]
addDigitAnimate(scrollNum, beforeNumArr[i], afterNumArr[i]) const afterNum = afterNumArr[i]
} const isUp = afterNum - beforeNum >= 0
}
// 创建单个数字动画
function addDigitAnimate(scrollNum, beforeNumArr, afterNumArr) {
let delay = 0
for (let i = scrollNum.length - 1; i >= 0; i--) { const beforeChildNumArr = beforeNum.split('')
const scrollNumNode = scrollNum[i] const afterChildNumArr = afterNum.split('')
scrollNumNode.style.setProperty('--i', beforeNumArr[i])
scrollNumNode.style.setProperty('--j', afterNumArr[i])
const d = Math.abs(beforeNumArr[i] - afterNumArr[i]) * 0.3 let delay = 0
scrollNumNode.style.setProperty('--duration', d) const scrollNum = scrollNode.childNodes
scrollNumNode.style.setProperty('--delay', delay) for (let j = beforeChildNumArr.length - 1; j >= 0; j--) {
delay += d * 0.9 const num = beforeChildNumArr[j]
const num2 = afterChildNumArr[j]
const beforePercent = isUp ? num * -5 : -50 - num * 5
let afterPercent
let duration = 0
const ulNode = scrollNumNode.childNodes[0] const ulNode = scrollNum[j].childNodes[0]
ulNode.classList.remove('animate')
ulNode.setAttribute( ulNode.setAttribute(
'style', 'style',
'transform: translateY(' + beforeNumArr[i] * -9.09 + '%)' 'transform: translateY(' + beforePercent + '%)'
) )
if (isUp) {
if (num2 > num) {
afterPercent = num2 * -5
duration = num2 - num
} else {
afterPercent = num2 * -5 - 50
duration = 10 - (num - num2)
}
} else {
if (num2 < num) {
afterPercent = beforePercent + (num - num2) * 5
duration = num - num2
} else {
afterPercent = num2 * -5
duration = 10 - (num2 - num)
}
}
setTimeout(() => { duration = duration * 500
ulNode.classList.add('animate')
}, 0)
if (isSafari || isWeChat) { arr.push({
let timer = setTimeout(() => { beforePercent,
ulNode.setAttribute( afterPercent,
'style', duration,
'animation: none;transform: translateY(' + delay,
afterNumArr[i] * -9.09 + ulNode: scrollNum[j].childNodes[0],
'%)' })
) delay = delay + duration * 0.5
timer = null
}, delay * 1000)
} }
} }
arr.forEach((sObject) => {
const { beforePercent, afterPercent, duration, delay, ulNode } = sObject
new TWEEN.Tween({ y: beforePercent })
.to({ y: afterPercent }, duration)
.delay(delay)
.easing(TWEEN.Easing.Quintic.Out)
.onUpdate((object) => {
ulNode.setAttribute('style', 'transform: translateY(' + object.y + '%)')
})
.start()
})
animate()
} }
function createRandom() { function createRandom() {
...@@ -322,6 +352,12 @@ function createRandom() { ...@@ -322,6 +352,12 @@ function createRandom() {
return arr return arr
} }
function animate(time) {
let id = requestAnimationFrame(animate)
var result = TWEEN.update(time)
if (!result) cancelAnimationFrame(id)
}
window.onload = () => { window.onload = () => {
// echart渲染 // echart渲染
const lineChart1 = echarts.init(document.getElementById('line_chart_1')) const lineChart1 = echarts.init(document.getElementById('line_chart_1'))
...@@ -446,9 +482,9 @@ window.onload = () => { ...@@ -446,9 +482,9 @@ window.onload = () => {
let afterNumArr = ['35345', '83460', '36765', '36465'] let afterNumArr = ['35345', '83460', '36765', '36465']
// 初始化 // 初始化
createMultiSrollNum(beforeNumArr, width, height) createMultiSrollNum(beforeNumArr)
// 开始动画 // 添加动画
addMultiDigitAnimate(beforeNumArr, afterNumArr) addMultiDigitAnimate(beforeNumArr, afterNumArr)
// 测试,点击第一列数字 // 测试,点击第一列数字
......
This diff is collapsed.
...@@ -36,9 +36,9 @@ ...@@ -36,9 +36,9 @@
margin-left: 10px; margin-left: 10px;
} }
.scroll-num { .scroll-num {
width: var(--width, 39px); width: 39px;
height: var(--height, 50px); height: 50px;
line-height: var(--height, 50px); line-height: 50px;
text-align: center; text-align: center;
font-size: 41px; font-size: 41px;
color: #01fff6; color: #01fff6;
...@@ -55,73 +55,5 @@ ...@@ -55,73 +55,5 @@
font-family: 'Source Han Sans CN'; font-family: 'Source Han Sans CN';
} }
} }
// .border-animate {
// animation: enhance-bounce-in-down 1s calc(var(--delay) * 1s) forwards;
// }
.animate {
animation: move calc(var(--duration) * 1s) calc(var(--delay) * 1s) linear,
bounce-in-down 1s calc((var(--duration) + var(--delay)) * 1s) forwards;
}
.init {
transform: translateY(calc(var(--i) * -9.09%));
}
}
@keyframes move {
from {
transform: translateY(calc(var(--i) * -9.09%));
filter: url(#blur);
}
to {
transform: translateY(calc(var(--j) * -9.09%));
filter: url(#blur);
}
}
@keyframes bounce-in-down {
from {
transform: translateY(calc(var(--j) * -9.09% - 1.0%));
filter: none;
}
25% {
transform: translateY(calc(var(--j) * -9.09% + 0.8%));
}
50% {
transform: translateY(calc(var(--j) * -9.09% - 1%));
}
70% {
transform: translateY(calc(var(--j) * -9.09% + 0.6%));
}
85% {
transform: translateY(calc(var(--j) * -9.09% - 0.3%));
}
to {
transform: translateY(calc(var(--j) * -9.09%));
}
} }
// @keyframes enhance-bounce-in-down {
// 25% {
// transform: translateY(8%);
// }
// 50% {
// transform: translateY(-4%);
// }
// 70% {
// transform: translateY(2%);
// }
// 85% {
// transform: translateY(-1%);
// }
// to {
// transform: translateY(0);
// }
// }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment