Javascript おみくじ

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>おみくじ</title>
    <link rel="stylesheet" href="styles.css">
    <style>
      body {
        background : #e0e0e0;
        text-align: center;
        font-size: 16px;
        color: #fff;
        font-family: Arial, sans-serif;
      }
      #result {
        margin: 30px auto;
        width: 180px;
        height: 180px;
        border-radius: 50%;
        line-height: 180px;
        font-size: 48px;
        font-weight: bold;
        background: #f44336;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
      }
      #btn {
        margin: 0 auto;
        width: 200px;
        padding: 5px;
        border-radius: 5px;
        background: #00aaff;
        box-shadow: 0 4px 0 #0088cc;
        cursor: pointer;
      }
      #btn:hover{
        opacity: 0.8;
      }
      #btn.pushed{
        margin-top: 32px;
        box-shadow: 0 2px 0 #0088cc;
      }
    </style>
  </head>
  <body>
      <div id="result">?</div>
      <div id="btn">あなたの運勢は?</div>
      <script>
        (function(){
          "use strict";
          document.getElementById('btn').addEventListener('click', function(){
            var results = ['大吉','中吉','小吉','凶'];
            var result = Math.floor(Math.random() * results.length);
            document.getElementById('result').innerHTML = results[result];
          });
          document.getElementById('btn').addEventListener('mousedown', function(){
            this.className = 'pushed';
          });
          document.getElementById('btn').addEventListener('mouseup', function(){
            this.className = '';
          });
        })();
      </script>
  </body>
</html>

%e7%84%a1%e9%a1%8c

JavaScript 基礎

JavaScriptのドキュメントはMDNのサイトでよく参照することができます。
MDN JavaScript

即時関数:直ぐに実行されます。

<script>
       (function(name){
        console.log("hello" + name);
      })("tom");
      </script>
      <script>
      (function()){
        var x = 10;
        y = 20;
        console.log(x + y);
      }();
      </script>

setInterval

var i = 0;
      function show(){
        console.log(i++)
      }
      setInterval(function(){
        show();
      }, 1000);

setTimeout

var i = 0;
      function show(){
        console.log(i++);
        setTimeout(function(){
          show();
        }, 1000);
      }show();

オブジェクト

var user = {
        email: "yamada@gmail.com",
        score: 80
      };
      console.log(user.email)

オブジェクト2

var user = {
        email: "yamada@gmail.com",
        score: 80,
        greet: function(name){
          console.log("hello, " + name);
        }
      };
      user.greet("sato");

Arrayオブジェクト

var a = new Array(100, 300, 200);
      console.log(a.length);

Math

      console.log(Math.PI);
      console.log(Math.ceil(5.3));
      console.log(Math.floor(5.3));
      console.log(Math.round(5.3));

三項演算子
a = (条件) ? b : c;

よく使うもの

console.log(window.outerHeight);
window.location.href = "http://google.com"
document(DOM)
var e = document.getElementById('msg');
      e.textContent = 'Hello';
      e.style.color = 'red';