JS實(shí)現(xiàn)彈幕小案例
本文實(shí)例為大家分享了JS實(shí)現(xiàn)彈幕小案例的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
步驟分析:
1、收集用戶輸入內(nèi)容,根據(jù)內(nèi)容創(chuàng)建一個(gè)標(biāo)簽--span,追加到某個(gè)容器中
2、為元素設(shè)置位置
- left:右側(cè)--大容器外面
- top:上半?yún)^(qū)
3、通過樣式設(shè)置來實(shí)現(xiàn)元素的動(dòng)畫,也可以通過定時(shí)器的方式實(shí)現(xiàn)動(dòng)畫
4、細(xì)節(jié)
- 文本顏色隨機(jī)
- span動(dòng)畫結(jié)束之后應(yīng)該進(jìn)行自動(dòng)的清除
html代碼:
<div class="boxDom" id="boxDom"> ? ? ? <div class="idDom" id="idDom"> ? ? ? ? <div class="content"> ? ? ? ? ? <p class="title">吐槽:</p> ? ? ? ? ? <input type="text" class="text" id="text" /> ? ? ? ? ? <button type="button" class="btn" id="btn">發(fā)射</button> ? ? ? ? </div> ? ? ? </div> </div>
css代碼:
<style type="text/css"> ? ? ? html, ? ? ? body { ? ? ? ? margin: 0px; ? ? ? ? padding: 0px; ? ? ? ? width: 100%; ? ? ? ? height: 100%; ? ? ? ? font-family: '微軟雅黑'; ? ? ? ? font-size: 62.5%; ? ? ? } ? ? ? ? .boxDom { ? ? ? ? width: 100%; ? ? ? ? height: 100%; ? ? ? ? position: relative; ? ? ? ? overflow: hidden; ? ? ? } ? ? ? ? .idDom { ? ? ? ? width: 100%; ? ? ? ? height: 100px; ? ? ? ? background: #666; ? ? ? ? position: fixed; ? ? ? ? bottom: 0px; ? ? ? } ? ? ? ? .content { ? ? ? ? display: inline-block; ? ? ? ? width: 430px; ? ? ? ? height: 40px; ? ? ? ? position: absolute; ? ? ? ? left: 0px; ? ? ? ? right: 0px; ? ? ? ? top: 0px; ? ? ? ? bottom: 0px; ? ? ? ? margin: auto; ? ? ? } ? ? ? ? .title { ? ? ? ? display: inline; ? ? ? ? font-size: 4em; ? ? ? ? vertical-align: bottom; ? ? ? ? color: #fff; ? ? ? } ? ? ? ? .text { ? ? ? ? border: none; ? ? ? ? width: 300px; ? ? ? ? height: 30px; ? ? ? ? border-radius: 5px; ? ? ? ? font-size: 2.4em; ? ? ? } ? ? ? ? .btn { ? ? ? ? width: 60px; ? ? ? ? height: 30px; ? ? ? ? background: #f90000; ? ? ? ? border: none; ? ? ? ? color: #fff; ? ? ? ? font-size: 2.4em; ? ? ? } ? ? ? ? span { ? ? ? ? /* width: 300px; */ ? ? ? ? height: 40px; ? ? ? ? position: absolute; ? ? ? ? overflow: hidden; ? ? ? ? color: #000; ? ? ? ? font-size: 4em; ? ? ? ? line-height: 1.5em; ? ? ? ? cursor: pointer; ? ? ? ? white-space: nowrap; ? ? ? } </style>
JS代碼:
// 獲取元素 let btn = document.querySelector('#btn') let text = document.querySelector('#text') let boxDom = document.querySelector('#boxDom') ? // 為按鈕綁定事件 btn.addEventListener('click', function() { ? ? // 獲取用戶輸入內(nèi)容 ? ? // 表單元素input的值的獲取是使用value ? ? let content = text.value ? ? // trim:去除左右空格 ? ? if (content.trim().length == 0) { ? ? ? ? alert('請輸入一個(gè)內(nèi)容再發(fā)彈幕') ? ? ? ? return ? ? } ? ? // 創(chuàng)建一個(gè)元素 ? ? // createElement:創(chuàng)建元素 ? ? let span = document.createElement('span') ? ? span.innerText = content ? ? ? // 為元素設(shè)置樣式 ? ? // clientWidth:獲取元素的實(shí)際寬度 ? ? // 設(shè)置left值為元素右側(cè)外 ? ? span.style.left = boxDom.clientWidth + 'px' ? ? // 設(shè)置top為上半?yún)^(qū)隨機(jī)位置 ? ? span.style.top = ? ? ? ? parseInt((Math.random() * boxDom.clientHeight) / 2) + 'px' ? ? // span.style.color = setColor() ? ? //設(shè)置字體的隨機(jī)顏色 ? ? span.style.color = `rgb(${Math.random() * 255},${Math.random() * ? ? ? ? 255},${Math.random() * 255})` ? ? ? // 讓元素動(dòng)起來 -- 配合過渡樣式 ? ? // setTimeout(() => { ? ? // ? span.style.left = -span.clientWidth + 'px' ? ? // }, 200) ? ? // 距停止位置的距離 ? ? let dis = boxDom.clientWidth ? ? // setInterval(需要執(zhí)行的函數(shù),時(shí)間間隔) ? ? let tid = setInterval(function() { ? ? ? ? dis -= 1 ? ? ? ? span.style.left = dis + 'px' ? ? ? ? // 移動(dòng)到目標(biāo)位置,清除定時(shí)器 ? ? ? ? if (dis < -span.clientWidth) { ? ? ? ? ? ? clearInterval(tid) ? ? ? ? ? ? // 將當(dāng)前的span移除 ? ? ? ? ? ? span.remove() ? ? ? ? } ? ? }, 4) ? ? ? // 添加到指定容器中 ? ? // insertBefore:將指定的元素插入到參照元素的前面:父容器.insertBefore(子元素,參照元素) ? ? // appendChild:將元素追加到所有子元素的最后: 父容器.appendChild(子元素) ? ? // insertBefore:一定傳入兩個(gè)參數(shù) ? ? boxDom.insertBefore(span, boxDom.children[0]) })
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js arguments,jcallee caller用法總結(jié)
這篇文章主要介紹了js中arguments, jcallee caller用法。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11JavaScript設(shè)計(jì)模式之單例模式簡單實(shí)例教程
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之單例模式,結(jié)合簡單實(shí)例形式分析了單例模式的概念、功能及javascript定義與使用單例模式相關(guān)操作技巧,需要的朋友可以參考下2018-07-07js實(shí)現(xiàn)類似于add(1)(2)(3)調(diào)用方式的方法
這篇文章主要介紹了js實(shí)現(xiàn)類似于add(1)(2)(3)調(diào)用方式的方法,需要的朋友可以參考下2015-03-03JavaScript使用replace函數(shù)替換字符串的方法
這篇文章主要介紹了JavaScript使用replace函數(shù)替換字符串的方法,涉及javascript中replace函數(shù)的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04Postman如何實(shí)現(xiàn)參數(shù)化執(zhí)行及斷言處理
這篇文章主要介紹了Postman如何實(shí)現(xiàn)參數(shù)化執(zhí)行及斷言處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07如何用js實(shí)現(xiàn)鼠標(biāo)向上滾動(dòng)時(shí)浮動(dòng)導(dǎo)航
今天給大家介紹一下使用JavaScript判斷鼠標(biāo)滑輪是不是向上滾動(dòng),當(dāng)向上滾動(dòng)的時(shí)候,導(dǎo)航條浮動(dòng)在頂部位置。示例代碼如下。2016-07-07bootstrap table支持高度百分比的實(shí)例代碼
這篇文章給大家介紹了bootstrap table支持高度百分比的實(shí)例代碼,通過更改BootstrapTable.prototype.resetView 方法,以支持高度百分比定義,適應(yīng)不同高度屏幕,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-02-02