0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

7/12 今日の学習内容

Last updated at Posted at 2025-07-12

JavaScript/HTML/CSSを使用した簡単な電卓を作ろう

参考にするサイトの模写をします

JavaScriptを使用した簡単な電卓の作り方

新しく覚えた要素(HTML編)

< form >

ユーザーから情報を入力して送信するためのフォーム作成に必要

colspan=" "

セルを横に何個増やすか

get_calc

電卓アプリや数値入力UIで使われる関数名、ユーザーがクリックしたボタンの値を読み取って表示エリアに追加する処理などを行います

get_calc(this)

この要素自身、電卓ならクリックされたボタンそのもの

忘れていた要素(HTML編)

< table >

表を作成するのに必要

< tr >

表の横一列を作成

< td >

表のマスを作成

disabled

ユーザーが操作できないようにする

onclick=" "

ユーザーが要素をクリックしたときに発生するイベントを作成

模写した内容

<body>
  <form name="dentaku">
    <table>
      <!-- 液晶画面 -->
      <tr>
        <td colspan="4">
          <input type="text"  class="display"  value=""  disabled>
        </td>
      </tr>

      <!-- 1段目 -->
      <tr>
        <td><input type="button" value="7" onclick="get_calc(this)"></td>
        <td><input type="button" value="8" onclick="get_calc(this)"></td>
        <td><input type="button" value="9" onclick="get_calc(this)"></td>
        <td><input type="button" value="÷" class='operator' name="div_btn" onclick="get_calc(this)"></td>
      </tr>

      <!-- 2段目 -->
      <tr>
        <td><input type="button" value="4" onclick="get_calc(this)"></td>
        <td><input type="button" value="5" onclick="get_calc(this)"></td>
        <td><input type="button" value="6" onclick="get_calc(this)"></td>
        <td><input type="button" value="×" class='operator' name="multi_btn" onclick="get_calc(this)"></td>
      </tr>

      <!-- 3段目 -->
      <tr>
        <td><input type="button" value="1" onclick="get_calc(this)"></td>
        <td><input type="button" value="2" onclick="get_calc(this)"></td>
        <td><input type="button" value="3" onclick="get_calc(this)"></td>
        <td><input type="button" value="-" class='operator' name="div_btn" onclick="get_calc(this)"></td>
      </tr>

      <!-- 4段目 -->
      <td><input type="button" value="0" onclick="get_calc(this)"></td>
      <td><input type="button" value="C" onclick="get_calc(this)"></td>
      <td><input type="button" value="=" class='equal' onclick="get_calc(this)"></td>
      <td><input type="button" value="+" class='operator' onclick="get_calc(this)"></td>

    </table>
  </form>

</body>

次はCSSでスタイリングします

模写したコード

table {
  /* 電卓のサイズ */
  width: 300px;
  height: 400px;

  /* 電卓に影をつける */
  border: solid 1px #dcdcdca4;
  border-right: solid 4px #dcdcdca4;
  border-bottom: solid 4px #dcdcdca4;
  border-radius: 10px;

  text-align: center;

  /* 余白 */
  padding: 8px;
  margin: 20px;
}

input {
  /* ボタンのサイズ */
  width: 70px;
  height: 70px;

  /* ボタンの文字サイズ */
  font-size: x-large;

  /* 数字の背景色 */
  background-color: #dcdcdca4;

  /* ボタンの詳細設定 */
  border: none;
  border-radius: 20px;

  /* クリック時の黒枠を消す */
  outline: none;
}

/* ディスプレイの詳細設定 */
.display {
  width: 250px;
  text-align: right;

  /* 見た目の詳細設定 */
  background: #ffffff;
  border-top: solid #dcdcdca4 5px;
  border-bottom: solid #dcdcdca4 5px;
  border-right: solid #b6b6b6 6px;
  border-left: solid #b6b6b6 6px;
  border-radius: 5px;
}

/* 演算子の背景色を上書きで設定 */
.operator {
  background-color: #87cefa;
}

/* 記号=の部分の背景色を上書きで設定 */
.equal {
  background-color: #b6b6b6;
}

/* カーソルを上に乗せた時に色を濃くする */
input:hover {
  background: #747373b9;
}

.display:hover {
  background: #ffffff;
  /* ディスプレイ部分は無効化 */
}

.operator:hover {
  background: #339cdd;
}

/* クリック時に色を濃くする */
input:active {
  background: #5a5a5a;
}

.operator:active {
  background: #2c80b4;
}

途中まで完成した電卓

2025-07-13 0.49の画像.jpeg

次回はJSのコードを模写していきます

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?