一个文字轮播效果
2017-04-08 00:00
387 查看
摘要: es6文字轮播
<div class="container"> <div class="text"></div> </div> <style> html, body{ background:#212121; height:100%;} .container{ height:100%; width:100%; justify-content:center; align-items:center; display:flex;} .text{ font-weight:100; font-size:50px; color:#FAFAFA;} .dud{ color:#757575;} </style> <script> // —————————————————————————————————————————————————— // TextScramble // —————————————————————————————————————————————————— class TextScramble { constructor(el) { this.el = el this.chars = '!<>-_\\/[]{}—=+*^?#________' this.update = this.update.bind(this) } setText(newText) { const oldText = this.el.innerText const length = Math.max(oldText.length, newText.length) const promise = new Promise((resolve) => this.resolve = resolve) this.queue = [] for (let i = 0; i < length; i++) { const from = oldText[i] || '' const to = newText[i] || '' const start = Math.floor(Math.random() * 40) const end = start + Math.floor(Math.random() * 40) this.queue.push({ from, to, start, end }) } cancelAnimationFrame(this.frameRequest) this.frame = 0 this.update() return promise } update() { let output = '' let complete = 0 for (let i = 0, n = this.queue.length; i < n; i++) { let { from, to, start, end, char } = this.queue[i] if (this.frame >= end) { complete++ output += to } else if (this.frame >= start) { if (!char || Math.random() < 0.28) { char = this.randomChar() this.queue[i].char = char } output += `<span class="dud">${char}</span>` } else { output += from } } this.el.innerHTML = output if (complete === this.queue.length) { this.resolve() } else { this.frameRequest = requestAnimationFrame(this.update) this.frame++ } } randomChar() { return this.chars[Math.floor(Math.random() * this.chars.length)] } } const phrases = [ '今天', '没吃药', '感觉', '自己', '萌萌哒', '喵( ̄y▽ ̄)~' ] const el = document.querySelector('.text') const fx = new TextScramble(el) let counter = 0 const next = () => { fx.setText(phrases[counter]).then(() => { setTimeout(next, 800) }) counter = (counter + 1) % phrases.length } next() </script>
相关文章推荐
- 一个不错的渐隐文字效果第1/2页
- 一个闪烁文字鼠标跟随效果
- (转)css中通常会用到浮动与清除,也是一个必须掌握的知识点,概念性的东西不多说,下面举几个例子,来说明它的用法:1.文字环绕效果 2.多个div并排显示 3.清除浮动(默认显示)
- 问题:关于坛友的一个js轮播效果的实现
- 问题:关于坛友的一个js轮播效果的实现
- 想怎么描,就怎么描.一个非常棒的文字效果!
- 发布一个文字界面库 解放文字显示 几分钟使你的对话框OR视图文字呈现多彩效果
- 弹出一个遮罩层有正在加载效果的文字
- 用jquery做一个简单的图片轮播效果
- 一个jquery实现的不错的多行文字图片滚动效果
- 又一个jquery轮播效果,焦点图,带数字显示序号,这个可以添加对应标题在图片上,支持主流浏览器
- 分享一个HTML5画布实现的超酷文字弹跳球效果
- qq的一个js轮播效果
- 仿照京东做的一个鼠标移上去的图片文字说明效果
- 怎么用javascript做一个文字滚动效果?
- 一个js实现任务栏文字滚动效果
- 又一个jquery轮播效果,焦点图,带数字显示序号,这个可以添加对应标题在图片上,支持主流浏览器
- 分享一个HTML5画布实现的超酷文字弹跳球效果
- 简单的一个轮播效果
- 一个jquery实现的不错的多行文字图片滚动效果