使用提示框发弹幕

新的改变

首先,在HTML中写几个标签

    <article>        
             <aside id="aside">我是视频</aside>      
             input type="button" id=" input" value="发送弹幕" onclick="main()">    
     </article>

CSS添加样式

   <style>       
    article {           
        position: relative;         
        width: 1000px;            
        margin-top: 100px;           
        text-align: center;   
        left: 200px;            
        height: 600px;     
        overflow: hidden;
        }
        
     article aside {          
        width: 1000px;           
        height: 500px;          
        background-color: green;        
        font-size: 50px;          
        color: #fff;    
        text-align: center;    
        line-height: 500px;   
         }
     article p {        
        height: 40px;   
        line-height: 40px;      
        position: absolute;    
        left: 500px;    
        margin: 0;     
        text-align: left;   
        width: 1000px;   
        }
     input {
        position: relative;
        top: 20px       
        }    
    </style>

然后在js内添加移动效果

<script>   
    //随机函数    
    function getRandom(minNum, maxNum) {
         return parseInt((Math.random() * (maxNum - minNum + 1)) + minNum);
    }
             
    //主函数    
    function main() {
        let input = window.prompt("");
        let article = document.querySelector("article")
        let pEle = document.createElement('p');//创建节点
        pEle.style.position = (`absolute`) 
        pEle.style.left = (`1000px`);
        
       //字体随机大小
        let daxiao = getRandom(12, 46);
        pEle.style.fontSize = (`${daxiao}px`)
        
        //随机颜色
        let a = getRandom(0, 255);
        let b = getRandom(0, 255);
        let c = getRandom(0, 255);
        pEle.style.color = "rgb(" + a + "," + b + "," + c + ")"
        
        //随机位置
        let suiji = getRandom(0, 210);
        pEle.style.top = (`${suiji}px`)
        pEle.innerHTML = (input);//获取提示框输入的内容
        article.appendChild(pEle)
        move(pEle, article)  
      }


    //向左移动
        function move(ppp, articleEle) {
            let mover = setInterval(f68, 10)    
            let i = articleEle.clientWidth;     
            function f68() {     
                ppp.style.left = (`${i--}px`)   //p标签的位置向左移动
                if (ppp.offsetLeft <= -articleEle.clientWidth) {
                        clearInterval(mover);
                }  
             }  
          }
</script>

效果如下:在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值