How to implement the barrage component in native JavaScript
Oct 09, 2020 pm 04:32 PMJavaScript column today introduces you to the method of implementing barrage components in native JavaScript.
Preface
Nowadays, almost all video websites have barrage functions, so today we will encapsulate one ourselves using native JavaScript
Barrage type. This class hopes to have the following attributes and instance methods:
Attribute
-
el
The selector of the container node. The container node should be absolutely positioned and the width and height should be set. -
height
The height of each barrage -
mode
In barrage mode, half is half the height of the container, and top is one-third. full is the time it takes for the barrage to cross the screen speed
-
gapWidth
The distance between the next barrage and the previous barrage
Method
-
pushData
Add barrage metadata -
addData
Continue to join barrages -
start
Start scheduling barrage -
stop
Stop barrage -
restart
Restart barrage -
clearData
Clear the barrage -
close
Close -
open
Redisplay the barrage
PS: There are some self-encapsulated tool functions that I won’t post here. You just need to know the meaning.
Initialization
After introducing the JavaScript file, we hope to use it as follows , first adopt the default configuration.
1 |
|
Parameter initialization:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Accept the parameters first and then initialize them. Let’s see how getBoxSize
and initRows
1 2 3 4 5 6 7 |
|
pass getComputedStyle
api calculates the width and height of the box, which is used to calculate the width and height of the container and will be used later.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Calculate how many lines the barrage should have based on the height. The number of lines will be used somewhere below.
Insert data
There are two ways to insert data, one is to add source data, and the other is to continuously add. Let’s first look at the method of adding source data:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
The method of continuously adding data is just to call the method of adding source data and start scheduling
1 2 |
|
Launch barrages
Let’s take a look at the logic of launching barrages
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 |
|
1 2 |
|
The animation css is as follows
1 2 3 4 5 6 7 8 9 10 11 |
|
Other methods
Stop
Use the paused attribute of the animation to stop
1 2 3 4 5 |
|
1 2 |
|
Start again
Remove the pause class
1 2 3 4 5 |
|
Open and close
Just make a show-hidden logic
1 |
|
Clean up the barrages
1 2 3 4 5 6 |
|
Finally, use a timer to clean up the expired barrages:
1 2 3 4 5 |
|
Finally
I feel that the implementation of this is still flawed. If you designed it like this How would you design a class?
Related free learning recommendations: javascript(Video)
The above is the detailed content of How to implement the barrage component in native JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system

Simple JavaScript Tutorial: How to Get HTTP Status Code

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

How to close Kugou music barrage_How to close Kugou music barrage
