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

JS implements user registration interface function

This article example shares the specific code of ...

JavaScript removes unnecessary properties of an object

Table of contents Example Method 1: delete Method...

An article to deal with Mysql date and time functions

Table of contents Preface 1. Get the current time...

MySQL sorting using index scan

Table of contents Install sakila Index Scan Sort ...

MySQL Series 11 Logging

Tutorial Series MySQL series: Basic concepts of M...

Write a publish-subscribe model with JS

Table of contents 1. Scene introduction 2 Code Op...

Use PSSH to batch manage Linux servers

pssh is an open source software implemented in Py...

An example of how to optimize a project after the Vue project is completed

Table of contents 1. Specify different packaging ...

Table paging function implemented by Vue2.0+ElementUI+PageHelper

Preface I have been working on some front-end pro...

MySQL 8.0.21 installation steps and problem solutions

Download the official website First go to the off...

Install MySQL 5.7.18 using rpm package under CentOS 7

I have been using MySQL recently. The article mys...

Detailed explanation of JS memory space

Table of contents Overview 1. Stack and Heap 2. V...

How to allow remote access to open ports in Linux

1. Modify the firewall configuration file # vi /e...

MySQL query sorting and paging related

Overview It is usually not what we want to presen...