1. Basic principlesFirst, 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:
|
<<: How to create a basic image of the Python runtime environment using Docker
>>: Specific implementation methods of MySQL table sharding and partitioning
This article shares the specific code for using j...
Table of contents 01 CMD 02 ENTRYPOINT 03 WORKDIR...
In actual work, JavaScript regular expressions ar...
I have always used Loadrunner to do performance t...
There are two ways: 1. Service method Check the f...
This article example shares the specific code of ...
1. Download https://dev.mysql.com/downloads/mysql...
When developing and debugging a web application, ...
illustrate In front-end development, you often en...
This article shares the MySQL installation and co...
MySQL Views Simply put, a MySQL view is a shortcu...
1. Preliminary preparation (windows7+mysql-8.0.18...
View the nginx configuration file path Through ng...
Table of contents Pitfalls encountered in timesta...
Preface Recently, a data was operated incorrectly...