1
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?

More than 1 year has passed since last update.

marqueeが非推奨なので代替物の雛形をanimateメソッドで再現

Last updated at Posted at 2024-03-20

marqueeタグが非推奨になったので、代わりになるものを再現してみた。

CSSのキーフレームでも再現はできるんだろうけど、「アニメーションにかける時間」は文字数に応じてやりたいので、JavaScriptのanimateメソッドを使ってみた。

以下サンプル。

<html>
<style>
  html,
  body {
    margin: 0;
    padding: 0;
    background-color: black;
    overflow: hidden;
  }

  #main_pane {
    margin: 0;
    padding: 0;
    line-height: 100vh;
    height: 100vh;
    color: white;
    font-size: 80vh;
    width: max-content;
    font-weight: bold;
  }
</style>
<div id="main_pane">こっちみて!😍😍😍</div>
<script>
  const textLength = document.getElementById('main_pane').innerText.length;
  document.getElementById('main_pane').animate(
    [
      {
        // ギリ画面の外に出しておく(長3変形)
        transform: "translateX(100vw) scale(0.7,1)", 
      },
      {
        // divの幅すべてを左に出す(長3変形)
        transform: "translateX(-100%) scale(0.7,1)", 
      },
    ],
    {
      // 文字数 x 0.8秒かけて移動させる
      duration: textLength * 800, 
      iterations: Infinity,
    }
  )
</script>
</html>

用途

スマホをデジタルサイネージ的に使って、案内文を出したい時を想定。

デジタルサイネージにするには、スマホのスリープを防ぐ仕組みが必要。WakeLock APIを使えばできるみたいだけど、まだ試してない。

1
0
1

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
1
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?