JS實現彈幕小案例
更新時間:2022年08月25日 09:38:27 作者:歐歐呀
這篇文章主要為大家詳細介紹了JS實現彈幕小案例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JS實現彈幕小案例的具體代碼,供大家參考,具體內容如下
效果圖:
步驟分析:
1、收集用戶輸入內容,根據內容創(chuàng)建一個標簽--span,追加到某個容器中
2、為元素設置位置
- left:右側--大容器外面
- top:上半區(qū)
3、通過樣式設置來實現元素的動畫,也可以通過定時器的方式實現動畫
4、細節(jié)
- 文本顏色隨機
- span動畫結束之后應該進行自動的清除
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() { ? ? // 獲取用戶輸入內容 ? ? // 表單元素input的值的獲取是使用value ? ? let content = text.value ? ? // trim:去除左右空格 ? ? if (content.trim().length == 0) { ? ? ? ? alert('請輸入一個內容再發(fā)彈幕') ? ? ? ? return ? ? } ? ? // 創(chuàng)建一個元素 ? ? // createElement:創(chuàng)建元素 ? ? let span = document.createElement('span') ? ? span.innerText = content ? ? ? // 為元素設置樣式 ? ? // clientWidth:獲取元素的實際寬度 ? ? // 設置left值為元素右側外 ? ? span.style.left = boxDom.clientWidth + 'px' ? ? // 設置top為上半區(qū)隨機位置 ? ? span.style.top = ? ? ? ? parseInt((Math.random() * boxDom.clientHeight) / 2) + 'px' ? ? // span.style.color = setColor() ? ? //設置字體的隨機顏色 ? ? span.style.color = `rgb(${Math.random() * 255},${Math.random() * ? ? ? ? 255},${Math.random() * 255})` ? ? ? // 讓元素動起來 -- 配合過渡樣式 ? ? // setTimeout(() => { ? ? // ? span.style.left = -span.clientWidth + 'px' ? ? // }, 200) ? ? // 距停止位置的距離 ? ? let dis = boxDom.clientWidth ? ? // setInterval(需要執(zhí)行的函數,時間間隔) ? ? let tid = setInterval(function() { ? ? ? ? dis -= 1 ? ? ? ? span.style.left = dis + 'px' ? ? ? ? // 移動到目標位置,清除定時器 ? ? ? ? if (dis < -span.clientWidth) { ? ? ? ? ? ? clearInterval(tid) ? ? ? ? ? ? // 將當前的span移除 ? ? ? ? ? ? span.remove() ? ? ? ? } ? ? }, 4) ? ? ? // 添加到指定容器中 ? ? // insertBefore:將指定的元素插入到參照元素的前面:父容器.insertBefore(子元素,參照元素) ? ? // appendChild:將元素追加到所有子元素的最后: 父容器.appendChild(子元素) ? ? // insertBefore:一定傳入兩個參數 ? ? boxDom.insertBefore(span, boxDom.children[0]) })
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
js arguments,jcallee caller用法總結
這篇文章主要介紹了js中arguments, jcallee caller用法。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11