An article teaches you how to use js to achieve the barrage effect

An article teaches you how to use js to achieve the barrage effect

Here is the barrage effect:

Barrage Effect

I believe everyone has seen it, so what is the principle of its implementation, and how do we use our web technology to implement it on the front end? ?

Create a new html file:

Hahahaha, don’t name it in Chinese like me.

Chinese naming is not in compliance with the standards. When you are out there in the world, the big guys will laugh at you when they see your Chinese naming.

In the above picture, we introduced the jquery plug-in. Yes, we wrote it in jq and returned to the original (friends who can’t find the CDN link can Baidu bootcdn and search for jquery in it). And gave it a title from the barrage website.

Create an initial template

I have to mention here that I recommend front-end developers to use vscode for development, it is very easy to use.

A little trick: Enter in a blank HTML file! It will automatically initialize the HTML template for you

The template has been created, and after jQuery is introduced, the main text begins:

HTML Additions

<body>
    <div class="con">
        <div id="screen">
            <div class="dm_show">
                <!-- <div>text message</div> -->
            </div>
        </div>
        <div class="edit">
            <p class="msg">
                <input placeholder="Say something?" class="content" type="text" />
            </p>
            <p class="btns">
                <input type="button" class="send" value="Send" />
                <input type="button" class="clear" value="Clear screen" />
            </p>
        </div>
    </div>
</body>

A simple HTML.

Let’s write the css:

CSS Padding

<style>
    .con {
        background-color: floralwhite;
        padding: 40px 80px 80px;
    }
 
    #screen {
        background-color: #fff;
        width: 100%;
        height: 380px;
        border: 1px solid rgb(229, 229, 229);
        font-size: 14px;
    }
 
    .content {
        border: 1px solid rgb(204, 204, 204);
        border-radius: 3px;
        width: 320px;
        height: 35px;
        font-size: 14px;
        margin-top: 30px;
    }
 
    .send {
        border: 1px solid rgb(230, 80, 30);
        color: rgb(230, 80, 0);
        border-radius: 3px;
        text-align: center;
        padding: 0;
        height: 35px;
        line-height: 35px;
        font-size: 14px;
        width: 159px;
        background-color: white;
    }
 
    .clear {
        border: 1px solid;
        color: darkgray;
        border-radius: 3px;
        text-align: center;
        padding: 0;
        height: 35px;
        line-height: 35px;
        font-size: 14px;
        width: 159px;
        background-color: white;
    }
 
    .edit {
        margin: 20px;
        text-align: center;
    }
 
    .edit .btns{
        margin-top: 20px;
    }
 
    .edit .msg input{
        padding-left: 5px;
    }
 
    .text {
        position: absolute;
    }
 
    * {
        margin: 0;
        padding: 0;
    }
 
    .dm_show {
        margin: 30px;
    }
</style>

See the effect:

The overall structure has come out, the following is the js of the true 28 classics:

js logic code

<script>
    $(function () {
        //Set the "Send" button click event to display the bullet screen on the bullet screen wall$('.send').click(function () {
            //Get the content of the text input box var val = $('.content').val();
            //After assigning the content of the text box to val, clear the content of the text box so that the user can enter it next time$('.content').val('');
            //Wrap the text box content with div for subsequent processing var $content = $('<div class="text">' + val + '</div>');
            //Get the bullet screen object $screen = $(document.getElementById("screen"));
            //Set the top margin when the bullet screen appears to any value var top = Math.random() * $screen.height() + 40;
            //Set the top and left margins of the bullet screen $content.css({
                top: top + "px",
                left: 80
            });
            //Add the bullet screen body to the bullet screen wall$('.dm_show').append($content);
            //The bullet screen moves from the left to the right for 8 seconds, then delete the element directly$content.animate({
                left: $screen.width() + 80 - $content.width()
            }, 8000, function () {
                $(this).remove();
            });
        });
        //Set the "clear screen" click event to clear all contents in the bullet screen wall$('.clear').click(function () {
            $('.dm_show').empty();
        });
    });
</script>

Is it a surprise? Just a few lines, hahahaha.

The comments are very detailed, you can take a good look at them, here I will show you a demonstration:

Animation effects

Please forgive my poor picture quality, but it does not affect the viewing effect. Here I just simply implemented the effect of a barrage, which cannot reach the enterprise-level application. If you have the need, you can improve it by yourself. That's the reason, hehe.

If you want enterprise-level video barrage, I also recommend the dplayer player, which is a very perfect player.

This is almost the end of the discussion on front-end barrage. The above is an article that teaches you how to use js to implement the barrage effect in detail. For more information about js to implement barrage, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Native js to achieve barrage effect
  • Simple implementation of JavaScript bullet screen effect
  • Realizing bullet screen special effects based on JavaScript
  • JavaScript to achieve bullet screen wall effect

<<:  MySQL 8.0.19 supports locking an account after entering an incorrect password three times (example)

>>:  Create a code example of zabbix monitoring system based on Dockerfile

Recommend

How many ports can a Linux server open at most?

Table of contents Port-related concepts: Relation...

How to use Vue3 mixin

Table of contents 1. How to use mixin? 2. Notes o...

Examples of using Docker and Docker-Compose

Docker is an open source container engine that he...

The principle and application of ES6 deconstruction assignment

Table of contents Array destructuring assignment ...

Analyze the working principle of Tomcat

SpringBoot is like a giant python, slowly winding...

How to use mysqladmin to get the current TPS and QPS of a MySQL instance

mysqladmin is an official mysql client program th...

Share 12 commonly used Loaders in Webpack (Summary)

Table of contents Preface style-loader css-loader...

Detailed explanation of CSS label mode display property

The code looks like this: <!DOCTYPE html> &...

CSS3 realizes text relief effect, engraving effect, flame text

To achieve this effect, you must first know a pro...

Detailed explanation of Tomcat core components and application architecture

Table of contents What is a web container? The Na...

Detailed explanation of the pitfalls of Apache domain name configuration

I have never used apache. After I started working...

Examples and comparison of 3 methods for deduplication of JS object arrays

Table of contents 1. Comparison of data before an...

Detailed tutorial on installing Ubuntu 19.10 on Raspberry Pi 4

Because some dependencies of opencv could not be ...

Example of Vue uploading files using formData format type

In Vue, we generally have front-end and back-end ...