温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jQuery实现弹幕特效

发布时间:2020-08-24 19:08:19 来源:脚本之家 阅读:110 作者:有一个进大厂的梦 栏目:web开发

案例简介

jQuery实现弹幕效果,代码如下。

案例目录

jQuery实现弹幕特效

HTML部分

<!DOCTYPE html>


<html>
 <head>
 <meta charset="utf-8">
 <title>jQuery弹幕案例</title>
 <link rel="stylesheet" type="text/css" href="style.css" rel="external nofollow" />
 </head>
 <body>
 <div class="box">
 <div class="top"></div>
 <div class="bot">
 <input type="text" id="txt" placeholder="我来说两句。。。" />
 <button type="button" id="btn">发送</button>
 </div>
 </div>
 <script src="jquery-1.12.4.js"></script>
 <script>
 $(function() {
 var colors = ["red", "green", "hotpink", "pink", "cyan", "yellowgreen", "purple", "deepskyblue"];
 $("#btn").on("click", function() {
  var randomColor = parseInt(Math.random() * colors.length);
  var randomY = parseInt(Math.random() * 400);
  $("<span></span>") //创建span
  .text($("#txt").val()) //设置内容
  .css("color", colors[randomColor]) //设置字体颜色
  .css("left", "1400px") //设置left值
  .css("top", randomY) //设置top值
  .animate({
  left: -500
  }, 10000, "linear", function() {
  //到了终点,需要删除
  $(this).remove();
  }) //添加动画
  .appendTo(".top");

  $("#txt").val("");
 });
 });
 </script>
 </body>
</html>

CSS部分

* {
 margin: 0;
 padding: 0;
}

.box {
 width: 1600px;
 height: 757px;
}

.top {
 width: 100%;
 height: 660px;
 position: relative;
}

.bot {
 width: 100%;
 height: 97px;
 background-color: #666666;
 position: relative;
}

#txt {
 width: 300px;
 height: 30px;
 border-radius: 5px;
 position: absolute;
 left: 50%;
 margin-left: -150px;
 top: 50%;
 margin-top: -15px;
 border: none;
}

#btn {
 width: 60px;
 height: 30px;
 color: white;
 background-color: red;
 position: absolute;
 left: 955px;
 top: 34px;
 border: none;
}

span {
 position: absolute;
 color: #000;
 font-size: 50px;
 line-height: 1.5em;
 cursor: pointer;
 white-space: nowrap;
}

效果展示

jQuery实现弹幕特效

以上就是jQuery实现弹幕效果的代码,希望对您有帮助!

源码下载:jQuery弹幕

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI