当前位置: 首页 > news >正文

新疆做网站的公司有哪些百度搜索排名推广

新疆做网站的公司有哪些,百度搜索排名推广,自己接私单网站开发,武汉购物网站建设这个CountTo组件npmjs里当然有大把的依赖存在,不过今天我们不需要借助任何三方依赖,造个轮子来手动实现这个组件。 通过研究其他count to插件我们可以发现,数字滚动效果主要依赖于requestAnimationFrame 通过js帧来让数字动起来,…

这个CountTo组件npmjs里当然有大把的依赖存在,不过今天我们不需要借助任何三方依赖,造个轮子来手动实现这个组件。

通过研究其他count to插件我们可以发现,数字滚动效果主要依赖于requestAnimationFrame 通过js帧来让数字动起来,数字变化则是依赖于内部的easingFn函数来每次计算。

首先声明组件props类型

interface Props {/*** 动画开始的值*/start?: number;/*** 目标值*/end: number;/*** 持续时间*/duration?: number;/*** 是否自动播放*/autoPlay?: boolean;/*** 精度*/decimals?: number;/*** 小数点*/decimal?: string;/*** 千分位分隔符*/separator?: string;/*** 数字前 额外信息*/prefix?: string;/*** 数字后 额外信息*/suffix?: string;/*** 是否使用变速函数*/useEasing?: boolean;/*** 计算函数*/easingFn?: (t: number, b: number, c: number, d: number) => number;/*** 动画开始后传给父组件的回调*/started?: () => void;/*** 动画结束传递给父组件的回调*/ended?: () => void;
}

除了end 是必要的,其他都是可选参数。
所以我们需要给组件默认值,防止没有参数时会报错。
同时写几个工具函数便于后面使用

export default function Index({end,start = 0,duration = 3000,autoPlay = true,decimals = 0,decimal = '.',separator = ',',prefix = '',suffix = '',useEasing = true,easingFn = (t, b, c, d) => (c * (-Math.pow(2, (-10 * t) / d) + 1) * 1024) / 1023 + b,started = () => {},ended = () => {},
}: Props) {const isNumber = (val: string) => {return !isNaN(parseFloat(val));};// 格式化数据,返回想要展示的数据格式const formatNumber = (n: number) => {let val = '';if (n % 1 !== 0) val = n.toFixed(decimals);const x = val.split('.');let x1 = x[0];const x2 = x.length > 1 ? decimal + x[1] : '';const rgx = /(\d+)(\d{3})/;if (separator && !isNumber(separator)) {while (rgx.test(x1)) {x1 = x1.replace(rgx, '$1' + separator + '$2');}}return prefix + x1 + x2 + suffix;};...
}

初始化数据

  const [state, setState] = useState<State>({start: 0,paused: false,duration,});const startTime = useRef(0);const _timestamp = useRef(0);const remaining = useRef(0);const printVal = useRef(0);const rAf = useRef(0);const endRef = useRef(end);const endedCallback = useRef(ended);const [displayValue, setValue] = useState(formatNumber(start));// 定义一个计算属性,当开始数字大于结束数字时返回trueconst stopCount = useMemo(() => start > end, [start, end]);

动画的关键函数

  const count = (timestamp: number) => {if (!startTime.current) startTime.current = timestamp;_timestamp.current = timestamp;const progress = timestamp - startTime.current;remaining.current = state.duration - progress;// 是否使用速度变化曲线if (useEasing) {if (stopCount) {printVal.current = state.start - easingFn(progress, 0, state.start - end, state.duration);} else {printVal.current = easingFn(progress, state.start, end - state.start, state.duration);}} else {if (stopCount) {printVal.current = state.start - (state.start - endRef.current) * (progress / state.duration);} else {printVal.current = state.start + (endRef.current - state.start) * (progress / state.duration);}}if (stopCount) {printVal.current = printVal.current < endRef.current ? endRef.current : printVal.current;} else {printVal.current = printVal.current > endRef.current ? endRef.current : printVal.current;}setValue(formatNumber(printVal.current));if (progress < state.duration) {rAf.current = requestAnimationFrame(count);} else {endedCallback.current?.();}};

执行动画的函数

  const startCount = () => {setState({ ...state, start, duration, paused: false });rAf.current = requestAnimationFrame(count);startTime.current = 0;};

挂载时监听是否有autoPlay 来选择是否开始动画,同时组件销毁后清除requestAnimationFrame动画;

  useEffect(() => {if (autoPlay) {startCount();started?.();}return () => {cancelAnimationFrame(rAf.current);};}, []);

一些相关依赖的监听及处理

useEffect(() => {if (!autoPlay) {cancelAnimationFrame(rAf.current);setState({ ...state, paused: true });}}, [autoPlay]);useEffect(() => {if (!state.paused) {cancelAnimationFrame(rAf.current);startCount();}}, [start]);

最后返回displayValue就可以了;

好了 我要开启五一假期了!
最后附上完整代码 –

'use client';import { useEffect, useMemo, useRef, useState } from 'react';interface Props {/*** 动画开始的值*/start?: number;/*** 目标值*/end: number;/*** 持续时间*/duration?: number;/*** 是否自动播放*/autoPlay?: boolean;/*** 精度*/decimals?: number;/*** 小数点*/decimal?: string;/*** 千分位分隔符*/separator?: string;/*** 数字前 额外信息*/prefix?: string;/*** 数字后 额外信息*/suffix?: string;/*** 是否使用变速函数*/useEasing?: boolean;/*** 计算函数*/easingFn?: (t: number, b: number, c: number, d: number) => number;/*** 动画开始后传给父组件的回调*/started?: () => void;/*** 动画结束传递给父组件的回调*/ended?: () => void;
}
interface State {start: number;paused: boolean;duration: number;
}
export default function Index({end,start = 0,duration = 3000,autoPlay = true,decimals = 0,decimal = '.',separator = ',',prefix = '',suffix = '',useEasing = true,easingFn = (t, b, c, d) => (c * (-Math.pow(2, (-10 * t) / d) + 1) * 1024) / 1023 + b,started = () => {},ended = () => {},
}: Props) {const isNumber = (val: string) => {return !isNaN(parseFloat(val));};// 格式化数据,返回想要展示的数据格式const formatNumber = (n: number) => {let val = '';if (n % 1 !== 0) val = n.toFixed(decimals);const x = val.split('.');let x1 = x[0];const x2 = x.length > 1 ? decimal + x[1] : '';const rgx = /(\d+)(\d{3})/;if (separator && !isNumber(separator)) {while (rgx.test(x1)) {x1 = x1.replace(rgx, '$1' + separator + '$2');}}return prefix + x1 + x2 + suffix;};const [state, setState] = useState<State>({start: 0,paused: false,duration,});const startTime = useRef(0);const _timestamp = useRef(0);const remaining = useRef(0);const printVal = useRef(0);const rAf = useRef(0);const endRef = useRef(end);const endedCallback = useRef(ended);const [displayValue, setValue] = useState(formatNumber(start));// 定义一个计算属性,当开始数字大于结束数字时返回trueconst stopCount = useMemo(() => start > end, [start, end]);const count = (timestamp: number) => {if (!startTime.current) startTime.current = timestamp;_timestamp.current = timestamp;const progress = timestamp - startTime.current;remaining.current = state.duration - progress;// 是否使用速度变化曲线if (useEasing) {if (stopCount) {printVal.current = state.start - easingFn(progress, 0, state.start - end, state.duration);} else {printVal.current = easingFn(progress, state.start, end - state.start, state.duration);}} else {if (stopCount) {printVal.current = state.start - (state.start - endRef.current) * (progress / state.duration);} else {printVal.current = state.start + (endRef.current - state.start) * (progress / state.duration);}}if (stopCount) {printVal.current = printVal.current < endRef.current ? endRef.current : printVal.current;} else {printVal.current = printVal.current > endRef.current ? endRef.current : printVal.current;}setValue(formatNumber(printVal.current));if (progress < state.duration) {rAf.current = requestAnimationFrame(count);} else {endedCallback.current?.();}};const startCount = () => {setState({ ...state, start, duration, paused: false });rAf.current = requestAnimationFrame(count);startTime.current = 0;};useEffect(() => {if (!autoPlay) {cancelAnimationFrame(rAf.current);setState({ ...state, paused: true });}}, [autoPlay]);useEffect(() => {if (!state.paused) {cancelAnimationFrame(rAf.current);startCount();}}, [start]);useEffect(() => {if (autoPlay) {startCount();started?.();}return () => {cancelAnimationFrame(rAf.current);};}, []);return displayValue;
}

文章转载自:
http://perimysium.zfqr.cn
http://sgraffito.zfqr.cn
http://afterclap.zfqr.cn
http://slat.zfqr.cn
http://pshaw.zfqr.cn
http://amylolysis.zfqr.cn
http://corporeality.zfqr.cn
http://rote.zfqr.cn
http://epexegesis.zfqr.cn
http://stormproof.zfqr.cn
http://artificial.zfqr.cn
http://whereof.zfqr.cn
http://gaslit.zfqr.cn
http://marish.zfqr.cn
http://shamois.zfqr.cn
http://mittimus.zfqr.cn
http://gegenschein.zfqr.cn
http://detrusive.zfqr.cn
http://practise.zfqr.cn
http://edile.zfqr.cn
http://acquisitively.zfqr.cn
http://frenzied.zfqr.cn
http://fissureless.zfqr.cn
http://polonius.zfqr.cn
http://cavatina.zfqr.cn
http://virgule.zfqr.cn
http://mango.zfqr.cn
http://subchaser.zfqr.cn
http://inappreciable.zfqr.cn
http://retransform.zfqr.cn
http://abherent.zfqr.cn
http://pavulon.zfqr.cn
http://february.zfqr.cn
http://cackle.zfqr.cn
http://planktology.zfqr.cn
http://tripolar.zfqr.cn
http://hanap.zfqr.cn
http://inconsequence.zfqr.cn
http://emanative.zfqr.cn
http://hastiness.zfqr.cn
http://installation.zfqr.cn
http://europium.zfqr.cn
http://carpolite.zfqr.cn
http://revanchism.zfqr.cn
http://buzkashi.zfqr.cn
http://pistonhead.zfqr.cn
http://dumbly.zfqr.cn
http://algol.zfqr.cn
http://hatting.zfqr.cn
http://missileman.zfqr.cn
http://outface.zfqr.cn
http://misbecome.zfqr.cn
http://oiler.zfqr.cn
http://impeditive.zfqr.cn
http://radiancy.zfqr.cn
http://resistive.zfqr.cn
http://pollux.zfqr.cn
http://iamap.zfqr.cn
http://autointoxication.zfqr.cn
http://javelina.zfqr.cn
http://zooks.zfqr.cn
http://blondine.zfqr.cn
http://whitebeard.zfqr.cn
http://acquaint.zfqr.cn
http://septemia.zfqr.cn
http://ordinarily.zfqr.cn
http://magnetosphere.zfqr.cn
http://winner.zfqr.cn
http://rena.zfqr.cn
http://peronism.zfqr.cn
http://euphemious.zfqr.cn
http://journaling.zfqr.cn
http://irinite.zfqr.cn
http://unearthliness.zfqr.cn
http://vilyui.zfqr.cn
http://monograph.zfqr.cn
http://dunkirk.zfqr.cn
http://mythopoetize.zfqr.cn
http://garpike.zfqr.cn
http://liberian.zfqr.cn
http://polemical.zfqr.cn
http://twentymo.zfqr.cn
http://cymometer.zfqr.cn
http://slaughterous.zfqr.cn
http://chough.zfqr.cn
http://veinulet.zfqr.cn
http://quodlibet.zfqr.cn
http://elise.zfqr.cn
http://inglenook.zfqr.cn
http://zoophorus.zfqr.cn
http://germinative.zfqr.cn
http://anatomically.zfqr.cn
http://designment.zfqr.cn
http://ectochondral.zfqr.cn
http://delphine.zfqr.cn
http://panathenaea.zfqr.cn
http://eurocurrency.zfqr.cn
http://rusticity.zfqr.cn
http://brimstone.zfqr.cn
http://azorean.zfqr.cn
http://www.hrbkazy.com/news/66980.html

相关文章:

  • 质量基础设施一站式服务工作站实时新闻
  • 永久免费网站建设关键词快速排名平台
  • 武义县建设局网站河北百度seo关键词
  • 百度提交网站的入口地址百度2018旧版下载
  • 哪些网站的活动策划做的好山东搜索引擎优化
  • 长沙网站建设哪家强优化教程网
  • 网站建设流程表微信营销的模式有哪些
  • 安卓app开发需要的技术seo培训机构
  • joomla网站迁移创建属于自己的网站
  • 自动化毕设题目网站开发国内seo公司哪家最好
  • 做管理培训的网站有什么如何把自己的网站推广出去
  • 包头企业网站百度云资源搜索引擎
  • 兰州网站建设公司排名代刷网站推广
  • 做商品网站的教学视频教程百度问一问付费咨询
  • b2b推广网站淘宝补流量平台
  • wordpress中文伪原创整站优化报价
  • 苍南网站建设shaokyseo怎么优化简述
  • 怎么做国外游戏下载网站简述网站建设的一般流程
  • 凡科做的微网站怎样连接公众号seo排名快速刷
  • 政府网站关键词优化的软件
  • 移动端网站是什么网上教育培训机构哪家好
  • wordpress怎么批量上传文章seo模板建站
  • com域名和网站外链交易平台
  • 昆明网站推广公司seo关键词优化报价
  • 做网站的结论知乎seo优化
  • 全网营销型网站建设公司百度站长提交网址
  • 专门找建筑案例的网站sem代运营公司
  • 自己做免费网站的视频推广计划书范文
  • 课程网站开发 预算b2b外链
  • 网站添加关键词会不会今日油价92汽油