文字弹幕效果

简介: 文字弹幕效果

效果预览

文字弹幕效果

http部分

<body>
    <div class="box" id="box">
      <div class="bot-nav" id="bot-nav">
        <div class="content">
          <p class="title">吐槽:</p>
          <input type="text" class="text" id="text" />
          <button type="button" class="btn" id="btn">发射</button>
        </div>
      </div>
    </div>
  </body>

css部分

html,
    body {
      margin: 0px;
      padding: 0px;
      width: 100%;
      height: 100%;
      font-family: "微软雅黑";
      font-size: 62.5%;
    }
    .box {
      width: 100%;
      height: 100%;
      position: relative;
      overflow: hidden;
    }
    .bot-nav {
      width: 100%;
      height: 100px;
      background: #666;
      position: fixed;
      bottom: 0px;
    }
    .content {
      display: inline-block;
      width: 450px;
      height: 40px;
      position: absolute;
      left: 0px;
      right: 0px;
      top: 0px;
      bottom: 0px;
      margin: auto;
    }
    .title {
      display: inline;
      font-size: 4em;
      vertical-align: bottom;
      color: #fff;
    }
    .text {
      outline: none;
      border: none;
      width: 300px;
      height: 30px;
      border-radius: 5px;
      font-size: 2.4em;
      padding: 0px 10px;
    }
    .btn {
      outline: none;
      width: 60px;
      height: 30px;
      background: #f90000;
      border: none;
      color: #fff;
      font-size: 2.4em;
      cursor: pointer;
    }
    span {
      width: 300px;
      height: 40px;
      position: absolute;
      overflow: hidden;
      color: #000;
      font-size: 4em;
      line-height: 1.5em;
      cursor: pointer;
      white-space: nowrap;
    }

js部分

$(document).ready(function() {
      // 随机颜色
      var colors = ["red", "green", "hotpink", "pink", "cyan", "yellowgreen", "purple", "deepskyblue"];
      // 注册按钮点击事件
      $("#btn").click(function() {
        // 获取随机颜色
        var randomColor = parseInt(Math.random() * colors.length);
        // 获取随机坐标
        var randomY = parseInt(Math.random() * 700);
        $("<span></span>")            // 创建span
          .text($("#text").val())       // 设置内容
          .css("color", colors[randomColor])  // 设置字体颜色
          .css("left", "1920px")        // 设置left值
          .css("top", randomY)        // 设置top值
          .animate({
            left: -500
          }, 10000, "linear", function() {
            $(this).remove();         // 到达终点,删除span元素
          })
          .appendTo("#box");          // 添加动画效果
        $("#text").val("");           // 输入完成,清空文本框内容
      });
      $("#text").keyup(function(e) {        // 注册键盘抬起事件,检测回车键
        if(e.keyCode == 13) {
          $("#btn").click();
        }
      });
    });

整体代码

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Sample040 - 文字弹幕效果</title>
    <link rel="stylesheet" type="text/css" href="css/basic.css" />
    <script src="js/jquery-1.12.4.js" type="text/javascript" charset="utf-8"></script>
  </head>
  <style type="text/css">
    html,
    body {
      margin: 0px;
      padding: 0px;
      width: 100%;
      height: 100%;
      font-family: "微软雅黑";
      font-size: 62.5%;
    }
    .box {
      width: 100%;
      height: 100%;
      position: relative;
      overflow: hidden;
    }
    .bot-nav {
      width: 100%;
      height: 100px;
      background: #666;
      position: fixed;
      bottom: 0px;
    }
    .content {
      display: inline-block;
      width: 450px;
      height: 40px;
      position: absolute;
      left: 0px;
      right: 0px;
      top: 0px;
      bottom: 0px;
      margin: auto;
    }
    .title {
      display: inline;
      font-size: 4em;
      vertical-align: bottom;
      color: #fff;
    }
    .text {
      outline: none;
      border: none;
      width: 300px;
      height: 30px;
      border-radius: 5px;
      font-size: 2.4em;
      padding: 0px 10px;
    }
    .btn {
      outline: none;
      width: 60px;
      height: 30px;
      background: #f90000;
      border: none;
      color: #fff;
      font-size: 2.4em;
      cursor: pointer;
    }
    span {
      width: 300px;
      height: 40px;
      position: absolute;
      overflow: hidden;
      color: #000;
      font-size: 4em;
      line-height: 1.5em;
      cursor: pointer;
      white-space: nowrap;
    }
  </style>
  <body>
    <div class="box" id="box">
      <div class="bot-nav" id="bot-nav">
        <div class="content">
          <p class="title">吐槽:</p>
          <input type="text" class="text" id="text" />
          <button type="button" class="btn" id="btn">发射</button>
        </div>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    $(document).ready(function() {
      // 随机颜色
      var colors = ["red", "green", "hotpink", "pink", "cyan", "yellowgreen", "purple", "deepskyblue"];
      // 注册按钮点击事件
      $("#btn").click(function() {
        // 获取随机颜色
        var randomColor = parseInt(Math.random() * colors.length);
        // 获取随机坐标
        var randomY = parseInt(Math.random() * 700);
        $("<span></span>")            // 创建span
          .text($("#text").val())       // 设置内容
          .css("color", colors[randomColor])  // 设置字体颜色
          .css("left", "1920px")        // 设置left值
          .css("top", randomY)        // 设置top值
          .animate({
            left: -500
          }, 10000, "linear", function() {
            $(this).remove();         // 到达终点,删除span元素
          })
          .appendTo("#box");          // 添加动画效果
        $("#text").val("");           // 输入完成,清空文本框内容
      });
      $("#text").keyup(function(e) {        // 注册键盘抬起事件,检测回车键
        if(e.keyCode == 13) {
          $("#btn").click();
        }
      });
    });
  </script>
</html>

网页效果

相关文章
|
4天前
|
JavaScript Java 关系型数据库
视频弹幕网站设计01-我爱发弹幕
视频弹幕网站设计01-我爱发弹幕
|
3天前
|
存储 前端开发 JavaScript
视频弹幕设计网站11-----绘制弹幕
视频弹幕设计网站11-----绘制弹幕
|
3天前
|
存储 前端开发
弹幕视频设计网站12------绘制弹幕02
弹幕视频设计网站12------绘制弹幕02
|
3天前
|
前端开发 JavaScript
弹幕视频设计网站13---------绘制弹幕03
弹幕视频设计网站13---------绘制弹幕03
弹幕视频设计网站16------------弹幕暂停与调节进度条
弹幕视频设计网站16------------弹幕暂停与调节进度条
|
11月前
|
机器人 语音技术
神器 | 文本转语音,直接可以合成多人多风格音频了!
为了适应更多的音频使用场景和需求,近期我们将文本转语音服务进行了升级,全新的功能将成为你配音工具的不二之选。
350 1
|
12月前
|
人工智能 自然语言处理 前端开发
效果:mask-image 哔哩哔哩弹幕不遮挡人物
效果:mask-image 哔哩哔哩弹幕不遮挡人物
136 0
效果:mask-image 哔哩哔哩弹幕不遮挡人物
|
小程序 开发者
微信小程序开发之视频播放器带弹幕Video弹幕颜色自定义(有图有源码)
微信小程序开发之视频播放器带弹幕Video弹幕颜色自定义(有图有源码)
137 0
|
机器学习/深度学习 人工智能 自然语言处理
文字、图片一键生成逼真音效,作者亲自揭秘音频AIGC模型
文字、图片一键生成逼真音效,作者亲自揭秘音频AIGC模型
191 0
|
编解码 Windows
用文字制作成图片
最近呢,我一个朋友要过生日,我像给她一个硬核的生日祝福。然后就想到了用文字拼成她的QQ头像
125 0
用文字制作成图片