最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

碎片時間學(xué)編程「296]:為指定的選擇器創(chuàng)建一個具有指定范圍、步長和持續(xù)時間的計數(shù)器

2023-04-04 12:43 作者:路條編程  | 我要投稿


為指定的選擇器創(chuàng)建一個具有指定范圍、步長和持續(xù)時間的計數(shù)器。

  • 檢查是否step有正確的標(biāo)志并相應(yīng)地更改它。

  • setInterval()與Math.abs()和結(jié)合使用Math.floor()以計算每次繪制新文本之間的時間。

  • 使用Document.querySelector(),Element.innerHTML更新所選元素的值。

  • 省略第四個參數(shù) ,step以使用默認(rèn)步驟1。

  • 省略第五個參數(shù) ,duration以使用默認(rèn)持續(xù)時間2000ms。

JavaScript

const counter = (selector, start, end, step = 1, duration = 2000) => { ?let current = start, ? ?_step = (end - start) * step < 0 ? -step : step, ? ?timer = setInterval(() => { ? ? ?current += _step; ? ? ?document.querySelector(selector).innerHTML = current; ? ? ?if (current >= end) document.querySelector(selector).innerHTML = end; ? ? ?if (current >= end) clearInterval(timer); ? ?}, Math.abs(Math.floor(duration / (end - start)))); ?return timer;};

示例:

counter('#my-id', 1, 1000, 5, 2000);// Creates a 2-second timer for the element with id="my-id"

更多內(nèi)容請訪問我的網(wǎng)站:https://www.icoderoad.com


碎片時間學(xué)編程「296]:為指定的選擇器創(chuàng)建一個具有指定范圍、步長和持續(xù)時間的計數(shù)器的評論 (共 條)

分享到微博請遵守國家法律
儋州市| 盐亭县| 普安县| 瓦房店市| 屯门区| 永和县| 伽师县| 阳山县| 南乐县| 杨浦区| 双峰县| 普陀区| 交城县| 卢氏县| 明星| 内乡县| 富宁县| 自治县| 广河县| 伊宁市| 云南省| 枣阳市| 抚州市| 萝北县| 廊坊市| 九龙城区| 犍为县| 紫云| 昌邑市| 鄯善县| 邵东县| 抚顺市| 贵南县| 林州市| 屯留县| 兴文县| 和政县| 北票市| 田阳县| 外汇| 金川县|