Use native js to simulate the scrolling effect of live bullet screen

Use native js to simulate the scrolling effect of live bullet screen

1. Basic principles

First, divide the live broadcast area into ten parts (I personally divide it into ten parts for the convenience of calculation), randomly put the input content into the ten divided areas, insert it outside the view on the right side of the ten divided areas, and then call the animation to move from right to left at a random speed. When it moves outside the view of the left area, remove this scrolling element.

2. Specific code

<div class="move_video_content">
    <div class="video_content">
        <div class="video_div" id="video_view"></div>
        <div class="scroll_content">
            <ul class="scroll_ul" id="scroll_ul_id"></ul> 
        </div>
    </div>
    <div class="input_content">
            <input type="text" class="input_text" maxlength="30" placeholder="Please enter the barrage you want to send" id="input_text_id">
            <button type="button" class="button_btn" id="send_btn">Send</button>
    </div>
</div>

The specific effects are as follows:

The js code is as follows

let inputText = document.getElementById("input_text_id");//input input boxlet scrollContent = document.getElementById("scroll_ul_id");//Side chat barlet videoView = document.getElementById("video_view");//Video arealet videoWidth = videoView.offsetWidth;//Total width of the live broadcast arealet listHeight = videoView.offsetHeight/10;//Height of each layer of live broadcast arealet listTopNum = [0,1,2,3,4,5,6,7,8,9];//Divide the height of the live broadcast area into 10 layersdocument.getElementById("send_btn").addEventListener("click",function(){//Listen for the send buttonlet value = inputText.value;//Get the value of the input boxif(!value) return;
    appendDom(value); //Insert the value of the input box into the scroll chat createVideoBulletChatDom(value); //Insert the value of the input box into the bullet screen inputText.value = ''; //Clear the input box scrollContent.scrollTop = scrollContent.scrollHeight; //Automatically scroll to the bottom })
function appendDom(value){//Insert the value of the input box into the scroll chat let li = document.createElement("li");
    li.setAttribute("class","scroll_li");
    li.innerHTML = value;
    scrollContent.appendChild(li);
}
let speedArr = ['normal','fast','faster'];
function createVideoBulletChatDom(value){//Insert the value of the input box into the bullet screen let num = listTopNum[Math.floor((Math.random()*listTopNum.length))];
    let p = document.createElement("p");
    p.setAttribute("class","video_p");
    p.style.top = (num * 60) + "px";
    p.style.left = videoWidth + "px";
    p.innerHTML = value;
    videoView.appendChild(p);
    let speed = speedArr[Math.floor((Math.random()*speedArr.length))];
    Animate(p,speed); //Scrolling animation}
let animateType = {
    'normal':5,
    'fast':10,
    'faster':15
}
function Animate(dom,speed){//Scrolling animation let domWidth = dom.offsetWidth;//The width of the current bullet screen element let distance = videoWidth;//The total width of the live broadcast area speed ? speed : 'normal';
    let interval = animateType[speed]
    let timer = setInterval(function(){
            distance -= interval;
            dom.style.left = distance + 'px';
            if(distance <= -domWidth){
                clearInterval(timer);
                videoView.removeChild(dom); //Clear the label that has scrolled out of the screen}
    },50)
}

According to the ten parts (listTopNum) that the live broadcast area is divided into, get the height of each layer (listHeight), and then change the top of the scroll label to insert it into different areas of the ten parts.

When you create a scroll label, you create a scroll animation (function Animate). The default speed is normal. Each time you create an animation, a random speed type (normal, fast, faster) is passed in randomly. The distance subtracted for each scroll is changed according to the speed type passed in to achieve different scroll speeds.

Summarize

This is a live scrolling animation that I wrote on a whim when I was bored. If WebSocket is added, multi-person synchronous communication can be achieved. I will improve it later when I have nothing to do.

For the specific code, please visit [:github.com/liqc-wwl/bu…] and download it to see the effect directly.

This is the end of this article about using native js to simulate the scrolling effect of live barrage. For more relevant js simulation of live barrage scrolling content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Simple implementation of JavaScript bullet screen effect
  • Realizing bullet screen special effects based on JavaScript
  • Example of video bullet screen effect implemented by JS
  • JavaScript live comment barrage cut picture function point collection effect code
  • JavaScript to achieve bullet screen wall effect
  • JavaScript to achieve video barrage effect (two versions)
  • Native js to achieve barrage effect
  • js cavans realizes static scrolling bullet screen
  • A simple guide to implementing bullet screen effects with native JS
  • js to achieve bullet screen aircraft effect

<<:  How to create a basic image of the Python runtime environment using Docker

>>:  Specific implementation methods of MySQL table sharding and partitioning

Recommend

jQuery canvas generates a poster with a QR code

This article shares the specific code for using j...

Introduction to the use of common Dockerfile commands

Table of contents 01 CMD 02 ENTRYPOINT 03 WORKDIR...

Analyzing ab performance test results under Apache

I have always used Loadrunner to do performance t...

Two ways to enable firewall in Linux service

There are two ways: 1. Service method Check the f...

Vue Getting Started with Weather Forecast

This article example shares the specific code of ...

Nginx cache configuration example

When developing and debugging a web application, ...

Display ellipsis effect when table cell content exceeds (implementation code)

illustrate In front-end development, you often en...

MySQL 5.7 installation and configuration tutorial

This article shares the MySQL installation and co...

Detailed explanation of the usage and differences of MySQL views and indexes

MySQL Views Simply put, a MySQL view is a shortcu...

MySQL 8.0.18 deployment and installation tutorial under Windows 7

1. Preliminary preparation (windows7+mysql-8.0.18...

How to view nginx configuration file path and resource file path

View the nginx configuration file path Through ng...

Pitfalls and solutions encountered in MySQL timestamp comparison query

Table of contents Pitfalls encountered in timesta...

How to use binlog for data recovery in MySQL

Preface Recently, a data was operated incorrectly...