简单实现jQuery弹幕效果

在要写一个弹幕案例的时候,首先要清楚每一步要干什么。
首先搭好框架之后在要发送弹幕时应该准备进行如下步骤:

目前创新互联已为成百上千的企业提供了网站建设、域名、网站空间绵阳服务器托管、企业网站设计、武穴网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

  • 获取到要发送到弹幕上的内容,即获取到文本框输入的内容。通过jquery的var str = $(“#文本框的id”).val();
  • 生成一个元素:利用jQuery的 var createSpan =$(““)生成一个span元素,以便发送。
  • 给刚创建的span赋值,即获取到的文本框中的值 createSpan.text(str );
  • 设置元素的动画效果,是元素动起来。利用jQuery的animate(css样式值,时间, 执行完动画调用的方法)。执行完动画得手动移除刚刚所添加的元素。

里面还有许多细节,仔细看就会有收获!

<!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>
<div class = "boxDom" id = "boxDom">
 <img src="../images/bg_2.jpg"  />
 <div id = "idDom" class = "idDom">
 <div class = "content">
  <p class = "title"> 说点什么:</p>
  <input type = "text" class = "text"/>
  <button type = "button" class = "btn" id = "btn" >发送</button>
 </div>
 </div>
</div>
</body>
</html>

效果图如下:

简单实现jQuery弹幕效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。

网站题目:简单实现jQuery弹幕效果
标题路径:https://www.cdcxhl.com/article20/ihsjco.html

成都网站建设公司_创新互联,为您提供软件开发响应式网站移动网站建设网站维护网站建设电子商务

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营