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

HTML+CSS to achieve text folding special effects example

This article mainly introduces the example of rea...

Analysis of Apache's common virtual host configuration methods

1. Apache server installation and configuration y...

Implementation of Docker Compose multi-container deployment

Table of contents 1. WordPress deployment 1. Prep...

Detailed explanation of the solution to image deformation under flex layout

Flex layout is a commonly used layout method nowa...

Vue implements a simple timer component

When doing a project, it is inevitable to encount...

Solution to the routing highlighting problem of Vue components

Preface Before, I used cache to highlight the rou...

Web Design Summary

<br />From the birth of my first personal pa...

Summary of Mysql update multi-table joint update method

Next, I will create two tables and execute a seri...

How to build a deep learning environment running Python in Docker container

Check virtualization in Task Manager, if it is en...

js to implement add and delete table operations

This article example shares the specific code of ...

Vue implements an example of pulling down and scrolling to load data

Table of contents Step 1: Installation Step 2: Ci...

Summary of MySQL log related knowledge

Table of contents SQL execution order bin log Wha...

Simple writing of MYSQL stored procedures and functions

What is a stored procedure Simply put, it is a se...

In-depth understanding of umask in new linux file permission settings

Preface The origin is a question 1: If your umask...