【jQuery】チェックボックスにチェックを入れるとボタンが活性化する

デザオ
利用規約に同意(チェック)するとー
デザコト
申し込みボタンが活性化しますー

こんな感じの依頼内容だったら

  1. 同意する(利用規約とか申し込み規約とか)
  2. 複数のボタンが活性化される(応募ボタンとか申し込みボタンとか)
  3. 活性化前の申し込みボタンはグレーデザイン(押せないということがわかるように)
  4. Googleアナリティクスのオンクリックタグ(onclick)を入れたい
  5. チェックマークはブラウザ共通デザインで
  6. しっかりレスポンシブ対応

実装してみましょう

コードを見る(HTML・CSS・jQuery)

HTML

<section>

  <div class="notes_required">
    <div class="checkBtn_area">
      <label for="checkBtn" class="checkBtn"><input id="checkBtn" type="checkbox"><span class="label_inner">同意する</span></label>
    </div>
    <p class="check_text_b">チェックを入れると「ボタン」が有効となります。</p>
  </div>

  <div class="btnArea">
    <input type="submit" value="">
    <a href="https://125naroom.com/web/3191" target="_blank" class="btn_one _a">ボタン</a>
  </div>

  <div class="btnArea">
    <input type="submit" value="">
    <a href="https://125naroom.com/web/3191" target="_blank" class="btn_one _b">ボタン</a>
  </div>

</section>

CSS

/*------------------------------------------------------------
.notes_required
------------------------------------------------------------*/

.notes_required {
  text-align: center;
  background: #fff;
  border: 2px solid #e8dc6a;
  padding: 30px 20px;
}
.notes_required .check_text_b {
  font-size: 16px;
  margin: 0;
}
.notes_required .checkBtn_area {
  max-width: 400px;
  margin: 15px auto 10px;
  border-radius: 12px;
  background: #f3efe6;
  padding: 20px 0;
  transition: .2s;
}
.notes_required._check .checkBtn_area {
  background: #e8dc6a;
}
.notes_required label {
  color: #2b2a27;
  font-size: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0);
  cursor: pointer;
  transition: .3s;
  position: relative;
}
.notes_required label .label_inner {
  padding-left: 22px;
}
.notes_required label .label_inner:before {
  content: "";
  width: 24px;
  height: 24px;
  background: #fff;
  border: 4px solid #2b2a27;
  box-sizing: border-box;
  position: absolute;
  top: 50%;
  left: 0;
  margin-top: -13px;
}
.notes_required._check label .label_inner:after {
  content: '';
  width: 20px;
  height: 12px;
  border-top: 6px solid #2b2a27;
  border-right: 6px solid #2b2a27;
  transform: rotate(130deg);
  position: absolute;
  top: 50%;
  left: 0;
  margin-top: -.9em;
}
.notes_required label input {
  position: relative;
  z-index: -1;
}
@media screen and (min-width: 768px) {
  .notes_required label:hover {
    border-bottom: 2px solid #2b2a27;
  }
}
@media screen and (max-width: 1024px) {
  .notes_required label {
    font-size: 18px;
  }
  .notes_required .check_text_b {
    font-size: 14px;
  }
  .notes_required label .label_inner {
    padding-left: 10px;
  }
  .notes_required label .label_inner:before {
    width: 20px;
    height: 20px;
    margin-top: -10px;
  }
  .notes_required._check label .label_inner:after {
    width: 16px;
    height: 8px;
    margin-top: -0.8em;
  }
}
@media screen and (max-width: 767px) {
  .notes_required {
    padding: 20px 15px;
  }
  .notes_required .check_text_b {
    font-size: 13px;
  }
}

/*------------------------------------------------------------
.btnArea
------------------------------------------------------------*/

.btnArea {
  max-width: 400px;
  margin: 30px auto 0;
  position: relative;
}
/* 押せないあいだは、透明の input をボタンの上にかぶせる */
.btnArea input[type="submit"] {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  margin: 0 auto;
  width: 100%;
  height: 100%;
  border: none;
  border-radius: 0;
  background: rgba(255, 255, 255, 0);
  padding: 0;
  cursor: default;
  z-index: 1;
  outline: none;
  transition: .3s;
  box-sizing: border-box;
  appearance: none;
}
.btnArea._check input[type="submit"] {
  z-index: -1;
}
a.btn_one {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #e8e3d8;
  border: 2px solid #e8e3d8;
  border-radius: 12px;
  box-sizing: border-box;
  box-shadow: 0px 0px 0px 0px #6b675f;
  width: 100%;
  height: 80px;
  padding: 0 10% 0 6%;
  color: #6b675f;
  font-size: 24px;
  font-weight: bold;
  text-align: left;
  text-decoration: none;
  position: relative;
  transition: .2s;
}
a.btn_one:before {
  content: "";
  display: inline-block;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 10px 0 10px 12px;
  border-color: transparent transparent transparent currentColor;
  position: absolute;
  top: 50%;
  right: 8%;
  margin-top: -11px;
}
._check a.btn_one._a {
  background: #2b2a27;
  border-color: #2b2a27;
  color: #fff;
  box-shadow: 0px 5px 0px 0px #6b675f;
}
._check a.btn_one._a:hover {
  background: #fff;
  color: #2b2a27;
}
._check a.btn_one._b {
  background: #e8dc6a;
  border-color: #e8dc6a;
  color: #2b2a27;
  box-shadow: 0px 5px 0px 0px #2b2a27;
}
._check a.btn_one._b:hover {
  background: #2b2a27;
  border-color: #2b2a27;
  color: #fff;
}
@media screen and (max-width: 1024px) {
  a.btn_one {
    height: 60px;
    font-size: 20px;
  }
}
@media screen and (max-width: 767px) {
  a.btn_one {
    font-size: 16px;
  }
}

/* 動きを減らす設定のとき */
@media (prefers-reduced-motion: reduce) {
  .notes_required .checkBtn_area,
  .notes_required label,
  a.btn_one { transition: none; }
}

jQuery

//チェックが入ったら、ボタンを押せる
$(function () {
  $('#checkBtn').on('change', function () {
    $('.notes_required, .btnArea').toggleClass('_check', this.checked);
  });
});

メモ

jQueryのことで何かわからないことがあればjQueryの日本語リファレンスサイトがあるので一度チェックしてみるといろいろ解決できたりしますよ。

さいごに

デザオ
CSSが複雑ですねー
デザコト
しっかりレスポンシブって大変だー

ふたりの小言

デザコトauthor

あ、いいな、と思うWebデザインを紹介しています。デザインの参考に。やさしいデザインが多いです。Webデザインギャラリー『デザインのこと - Web design gallery』を運営しています。