欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)彈幕小案例

 更新時(shí)間:2022年08月25日 09:38:27   作者:歐歐呀  
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)彈幕小案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

最新評(píng)論