輪郭を走る光でボタンを際立たせる

ボタンの縁を細い光が巡るデモ。光が通る面積と速度で、操作にどれだけ視線が集まるかを見比べたい。

Jhey · CSS · 元投稿作者のデモ

ボタンの縁を一度だけ光らせる

元の実例の着眼点を試すための独自のWebサンプルです。

文字を入力し、消してからもう一度入力してください。端末で「動きを減らす」を有効にすると、縁は静止します。

実装の手順

  1. 対象を決める。ここでは入力欄に文字が入り、送信できるようになった瞬間を合図にする。
  2. ボタンの内側と外側を別の面にする。外側のグラデーションを回すと、文字を動かさずに縁だけを光らせられる。
  3. 無効から有効へ切り替わったときだけ動かす。動きを減らす設定では、静止した縁と色の変化で状態を伝える。

動作確認

入力中に何度も光らず、空欄では送信できない。動きを減らした状態でも有効・無効が分かる。

サンプルコードを見る
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ボタンの縁を一度だけ光らせる</title>
<style>
  * { box-sizing: border-box; }
  body {
    margin: 0;
    padding: 16px;
    background: #23232a;
    color: #f4f0ea;
    font: 16px/1.6 system-ui, sans-serif;
  }
  label, output, strong, audio, progress { display: block; margin: 6px 0; }
  input, select, button { max-width: 100%; font: inherit; }
  input, select { padding: 4px 7px; }
  button {
    margin: 4px 0;
    padding: 5px 12px;
    cursor: pointer;
    border: 1px solid #b5a399;
    border-radius: 6px;
  }
  :focus-visible { outline: 3px solid #ffad73; outline-offset: 2px; }
  audio { width: 100%; }
  [hidden] { display: none !important; }
#send {
  position: relative;
  isolation: isolate;
  overflow: hidden;
  background: #31313a;
  border: 0;
  border-radius: 10px;
  color: white;
  padding: 10px 24px;
}
#send::before, #send::after {
  content: "";
  position: absolute;
  z-index: -1;
  pointer-events: none;
}
#send::before {
  inset: -100%;
  background: conic-gradient(#e5aa78, #795436 65%, white 80%, #d89659);
}
#send::after {
  inset: 2px;
  border-radius: 8px;
  background: #514133;
}
#send:disabled { opacity: .45; }
@media (prefers-reduced-motion: no-preference) {
  #send.ready::before { animation: sweep .7s ease-out; }
}
@keyframes sweep { to { transform: rotate(1turn); } }
</style>
</head>
<body>
<label>メッセージ <input id="message"></label>
<button id="send" disabled>送信</button>
<output id="result"></output>
<script>
const input = document.querySelector('#message');
const send = document.querySelector('#send');
function update() {
  const ready = input.value.trim().length > 0;
  send.disabled = !ready;
  send.classList.toggle('ready', ready);
}
input.addEventListener('input', update);
send.onclick = () => {
  document.querySelector('#result').textContent = '送信を受け付けました(デモ)';
  input.value = '';
  update();
};
</script>
</body>
</html>

index.html に保存し、ブラウザで開くと試せます。

この光を自分の画面で試す実例一覧