【Css】动作模糊数字

  • HTML
bash 复制代码
<div class="container">
	<input id="slider" class="slider" type="range" min="0" max="20000" step="5000" list="values" value="20000">

	<datalist id="values">
		<option value="0" label="0"></option>
		<option value="5000" label="5000"></option>
		<option value="10000" label="10000"></option>
		<option value="15000" label="15000"></option>
		<option value="20000" label="20000"></option>
	</datalist>

	<div class="number" id="number">
		<div class="left" id="left"></div>
		<div class="separator" id="separator">,</div>
		<div class="right" id="right">0</div>
	</div>
</div>

<svg class="svgFilter" xmlns="http://www.w3.org/2000/svg" version="1.1">
	<defs>
		<filter id="blurFilter">
			<feGaussianBlur id="blurFilterItem" in="SourceGraphic" stdDeviation="13,0" />
		</filter>
	</defs>
</svg>
  • CSS
css 复制代码
.number {
	display: flex;
	align-items: center;
	font-size: 6rem;
	justify-content: flex-end;

	.animate {
		filter: url("#blurFilter");
	}

	.left,
	.right {
		min-width: 11rem;
		text-align: right;
	}

	.right {
		padding-right: 1rem;
	}

	.separator {
		opacity: 0;
		transition: opacity 0.1s ease;

		&.show {
			opacity: 1;
		}
	}
}

.svgFilter {
	display: block;
	width: 0;
	height: 0;
}

.slider {
	accent-color: black;
	background: red;
	min-width: min(20rem, 60vw);
}

.container {
	display: flex;
	flex-direction: column;
	gap: 2rem;
}

body {
	display: grid;
	place-items: center;
	height: 100vh;
	width: 100vw;
	background: #ffc107;
	font-style: italic;
	padding: 1;
	font-weight: bold;
}

:root {
	--labs-sys-color-on-background: black;
}

* {
	box-sizing: border-box;
}
  • js
javascript 复制代码
const number = document.getElementById("number");
const left = document.getElementById("left");
const rights = document.getElementById("right");
const slider = document.getElementById("slider");
let target = 20000;
let current = 0;
const step = 42;

const start = () => {
	right.classList.add("animate");
	update();
};

slider.addEventListener("input", (event) => {
	target = +event.target.value;
	start();
});

const updateValues = () => {
	const [first, ...rest] = current.toLocaleString("en-US").split(",").reverse();
	thousends = rest.reverse();

	const thousendsString = thousends.join("");
	if (+left.innerText != thousendsString) {
		left.classList.add("animate");
	} else {
		left.classList.remove("animate");
	}
	left.innerText = thousendsString;
	right.innerText = first;
};

const update = () => {
	if (target - current > 0) {
		current += step;
	} else {
		current -= step;
	}
	if (current >= 1000) {
		separator.classList.add("show");
	} else {
		separator.classList.remove("show");
	}
	updateValues();
	if (Math.abs(target - current) > step) {
		requestAnimationFrame(update);
	} else {
		requestAnimationFrame(() => {
			current = target;
			updateValues();
			left.classList.remove("animate");
			right.classList.remove("animate");
		});
	}
};

requestAnimationFrame(start);
相关推荐
Mintopia2 分钟前
Three.js 深度冲突:当像素在 Z 轴上玩起 "挤地铁" 游戏
前端·javascript·three.js
Penk是个码农7 分钟前
web前端面试-- MVC、MVP、MVVM 架构模式对比
前端·面试·mvc
MrSkye10 分钟前
🔥JavaScript 入门必知:代码如何运行、变量提升与 let/const🔥
前端·javascript·面试
白瓷梅子汤14 分钟前
跟着官方示例学习 @tanStack-form --- Linked Fields
前端·react.js
爱学习的茄子18 分钟前
深入理解JavaScript闭包:从入门到精通的实战指南
前端·javascript·面试
zhanshuo1 小时前
不依赖框架,如何用 JS 实现一个完整的前端路由系统
前端·javascript·html
火柴盒zhang1 小时前
websheet在线电子表格(spreadsheet)在集团型企业财务报表中的应用
前端·html·报表·合并·spreadsheet·websheet·集团财务
讨厌吃蛋黄酥1 小时前
智能前端新纪元:语音交互技术与安全实践全解析
javascript
khalil1 小时前
基于 Vue3实现一款简历生成工具
前端·vue.js
拾光拾趣录1 小时前
浏览器对队头阻塞问题的深度优化策略
前端·浏览器