This article mainly introduces in detail the relevant information for simply realizing the jQuery barrage effect. I hope it can help you.
When you want to write a barrage case, you must first know what to do at each step.
After setting up the framework first, you should be prepared to perform the following steps when sending a barrage:
Get the content to be sent to the barrage, that is, get the content entered in the text box content. Generate an element through jquery's var str = $("#ID of the text box").val();
: Generate an element using jQuery's var createSpan =$("") span element for sending.
Assign a value to the span just created, that is, the value in the text box obtained createSpan.text(str);
Set the element Animation effects make elements move. Use jQuery's animate (css style value, time, method to call after executing animation). After executing the animation, you have to manually remove the elements you just added.
There are many details in it, you will be rewarded if you look carefully!
<!DOCTYPE html> <html> <head> <meta charset = "utf-8" /> <title>弹幕案例</title> <script src = "http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> $(function(){ var boxDom = $("#boxDom"); //var domContent = $("#domContent"); var top, right; var pageWidth = parseInt($(document).width()); var pageHeight =parseInt($(document).height()); //点击按钮 $("#btn").bind("click",auto);//按钮绑定方法 //按下回车 document.onkeydown = function(){ if(event.keyCode == 13){ auto(); } } function auto(){ //1.获取输入的字符串 var str = $(".text").val(); //2.生成一个元素 var createSpan = $("<span class = 'string' ></span>"); //3.给生成的元素赋值 createSpan.text(str); //为了页面友好,清空刚刚输入的值 $(".text").val(""); //生成元素一个随机的位置,为了使每条弹幕都出现在屏幕上不同的位置 top = Math.floor(Math.random()*pageHeight); createSpan.css({"top":top, "right": -400, "color": getRandomColor()}); boxDom.append(createSpan); //4.设置元素的动画效果,animate(css样式值,时间, 执行完动画调用的方法) //页面上有N个span,只让最后一个动起来 var spandom = $("#boxDom>span:last-child");//找到最后一个span spandom.animate({"right":pageWidth+300},10000,function(){ //移除元素 $(this).remove(); }); } //定义一个可以生成随机颜色的方法,可以使每条弹幕的颜色不同 function getRandomColor(){ var colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']; var color = ""; for(var i = 0; i < 6; i++){ color += colorArr[Math.floor(Math.random()*16)]; } return "#"+color; } }); </script> <style type="text/css"> html,body{ margin: 0px; padding: 0px; width: 100%; height: 100%; font-family: "微软雅黑"; background: #ccc; } .boxDom{ width: 100%; height: 100%; position: relative; overflow: hidden; } .idDom{ width: 100%; height: 60px; background:#666; position: fixed; bottom: 0px; } .contet{ width: 500px; height: 40px; position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; margin: auto; } .title{ display: inline; font-size: 24px; vertical-align: bottom; color: #ffffff; padding-left: 300px; } .text{ width: 300px; height: 30px; border:none; border-radius:5px; font-size: 20px; margin-left:60px; } .btn{ width: 60px; height: 30px; color: #ffffff; background-color: red; border:none; font-size:16px; margin-left:60px; margin-top: 20px; } .string { width: 300px; height: 40px; margin-top: 20px; position: absolute; color: #000; font-size: 20px; font-family: "微软雅黑"; } </style> </head> <body> <p class = "boxDom" id = "boxDom"> <img src="/static/imghwm/default1.png" data-src="../images/bg_2.jpg" class="lazy" style="max-width:90%" / alt="Simple implementation method of jQuery barrage effect" > <p id = "idDom" class = "idDom"> <p class = "content"> <p class = "title"> 说点什么:</p> <input type = "text" class = "text"/> <button type = "button" class = "btn" id = "btn" >发送</button> </p> </p> </p> </body> </html>
The renderings are as follows:
JavaScript implementation of simple barrage effect example analysis
Code implementation of WeChat applet barrage
The above is the detailed content of Simple implementation method of jQuery barrage effect. For more information, please follow other related articles on the PHP Chinese website!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
