弾幕サイト開発(フロントエンドのみ)

21000 ワード

  • index.html
  • danmu.css
  • danmu.js

  • 以前はHTML、cssを勉強していましたが、最近bootstrap、jQuery、JavaScriptを簡単に見て、弾幕サイトで慣れた感じを練習しました.
    バックエンドがあると思っていたので、LeanCloudを見て使えそうな気がしましたが、先にジャンプして、フロントエンドを打ってからにしましょう.
    初めてWebを開発し、主にネット上のコードを参考にして、各弾幕サイトのインタフェーススタイルを見ました.この様子はやはりざらざらしていて、ゆっくりとこのコードに機能を追加します.
    プロジェクトのアドレス:https://github.com/Joovo/Joovo.github.io
    Webサイトのアドレス:https://joovo.xyz
    最終結果図
    index.html
    bootstrapを使ったというのは、12グリッドとbtnのcssを使ったにすぎない.
    詳細:
  • cdnを導入したが、ローカル速度が速く、サーバに掛ける速度が遅すぎた.
  • jsとjQueryコードはもともと前に置いてありましたが、ラベルがないのでコードを実行できませんでした.

  • コード:
    
    
    <html lang="en">
    
    <head>
      <meta charset="utf-8">
      <title>   title>
      
      <link rel="stylesheet" href="./bootstrap/bootstrap.min.css">
      
      <script src="./bootstrap/jquery.min.js">script>
      
      <script src="./bootstrap/bootstrap.min.js">script>
      <link rel="stylesheet" type="text/css" href="danmu.css">
    head>
    
    <body>
      <div class="container">
        <div class="panel">
          <div class="dm">
            <div class="dm_screen" id="dm-screen">
              <div class="dm_mask">div>
              <div class="dm_show" id="dm-show">
                
                <div>2333div>
                <div>6666666666div>
                <div>2333div>
                <div>2333div>
                <div>2333div>
                <div>2333div>
                <div>2333div>
                <div>   NB!div>
                <div>2333div>
                <div>2333div>
                <div>   NB!div>
              div>
            div>
            <br>
            <div class="send">
              <div class="s_fiter">
                <div class="s_con">
                  <div class="row">
                    <div class="col-md-8 col-xs-8">
                      <input id="dm-txt" placeholder="      ~" type="text" class="s_txt" />
                    div>
                    <div class="col-md-2 col-xs-2">
                      <button type="button" id="btn-send" class="btn btn-block btn-info" style="margin-right:10px;"> >   button>
                    div>
                    <div class="col-md-2 col-xs-2">
                      <button id="btn-erase" class="btn btn-block s_sub" style="margin-right:10px;"> >   button>
                    div>
                  div>
                div>
              div>
            div>
          div>
        div>
      div>
      <script src="danmu.js">script>
    body>
    
    html>

    danmu.css
    书いたのは少し乱れて、ある地方ははっきりしないで直接后ろに!importantをプラスしました.
    .s_txt {
      width: 100%;
      height: 34px;
      border-radius: 3px;
      border: 1px solid rgb(204, 204, 204);
      padding-left: 10px;
    }
    
    .s_sub {
      border: 1px solid rgb(230, 80, 30) !important;
      color: rgb(230, 80, 0);
      border-radius: 3px;
      text-align: center;
      padding: 0;
      height: 35px;
      line-height: 35px;
      font-size: 14px;
      width: 100%;
      background-color: white;
    }
    
    .s_del {
      border: 1px solid rgb(176, 168, 165);
      color: rgb(176, 168, 165);
      border-radius: 3px;
      text-align: center;
      padding: 0;
      height: 35px;
      line-height: 35px;
      font-size: 14px;
      width: 159px;
      background-color: white;
    }
    
    .dm {
      margin: 20px;
      text-align: center;
    }
    
    .dm_screen {
      border: 1px solid rgb(229, 229, 229);
    }
    
    .dm .dm_screen .dm_mask {
      width: 100%;
      height: 380px;
    }
    
    .dm .dm_screen .dm_show div {
      font-size: 22px;
      line-height: 36px;
      font-weight: 500;
      color: #fff;
      position: absolute;
      left: 0;
      top: 0;
    }
    
    .panel {
      margin: 50px;
      text-align: center;
    }
    
    body {
      background-image: url('ss.jpg') !important;
      background-size: cover; //   IE9+
      -webkit-background-size: cover; //webkit  
      -moz-background-size: cover; //FF  
      -o-background-size: cover; //Opera  
    }
    

    danmu.js
    ボタンのリスニング、ランダムカラー生成、アニメーションなど.jQueryのラベルを追加し、ラベルを削除する機能はjQuery animated()の方法でアニメーションを書き、現在のcssから設定されたターゲットcssに変形し、以前遊んだflashに似ている.選択できるパラメータはたくさんあります.公式文書を直接見に行きます.
    詳細:
  • 弾幕を左側に付けてコールバック関数を付け、このラベル(この弾幕)を直接削除しました
  • 送信ボタンをクリックするとテキストボックスがクリアされ、$("#xxx").val('')を直接設定すればクリアの効果が得られます.
  • jQueryでoffsetTop,offsetLeftなどの使い方で、絶対的な画素値が得られます.
  • $(function() {
      $("#btn-send").click(function() {
        var txt = $("#dm-txt").val();
        var div = " 
    " + txt + "
    "
    ; $(".dm_show").append(div); $("#dm-txt").val(""); init_screen(); }); $("#btn-erase").click(function(){ $("#dm-show > div:nth-child(n)").remove(); }); init_screen(); }); function init_screen() { var obj = document.getElementById("dm-screen"); var _top = obj.offsetTop; $(".dm_show").find("div").show().each(function() { var _left = obj.offsetLeft + obj.offsetWidth - $(this).width(); // $(this).css("color")=#FFF; $(this).css({ left: _left, top: _top, color: getRandomColor() }); _top = _top + $(this).height(); if (_top > obj.offsetHeight + obj.offsetTop - $(this).width()) { _top = obj.offsetTop; } var time = 5000; if ($(this).index() % 2 == 0) time = 7000; $(this).animate({ left: obj.offsetLeft + 'px' }, time, "linear", function() { $(this).remove(); }); }); } var getRandomColor = function() { return '#' + (function(color) { return (color += '0123456789abcdef' [Math.floor(Math.random() * 16)]) && (color.length == 6) ? color : arguments.callee(color); })(''); }