【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);
相关推荐
DEMO派几秒前
CSS优先级规则以及如何提升优先级方案详解
前端·javascript·css·vue.js·reactjs·html5·angular.js
hhcccchh3 分钟前
学习vue第十一天 Vue3组件化开发指南:搭积木的艺术
前端·vue.js·学习
AntoineGriezmann5 分钟前
基于 Unocss 的后台系统 SVG 图标方案实践
前端
小夏卷编程6 分钟前
ant-design-vue 2.0 a-table 中实现特殊行样式,选中样式,鼠标悬浮样式不一样
前端·javascript·vue.js
wulijuan8886666 分钟前
前端性能优化之图片webp
前端
一颗烂土豆8 分钟前
ECharts 水球图不够炫?试试 RayChart 的创意可视化玩法
前端·vue.js·数据可视化
天才熊猫君9 分钟前
Vue 3 命令式弹窗组件
前端
NEXT0610 分钟前
CSS基础-标准盒模型与怪异盒模型
前端·css
DaMu11 分钟前
Dreamcore3D ARPG IDE “手搓”游戏引擎,轻量级实时3D创作工具,丝滑操作,即使小白也能轻松愉快的创作出属于你自己的游戏世界!
前端·架构·three.js
代码猎人12 分钟前
什么是尾调用,使用尾调用有什么好处?
前端