CSS

<aside> 💡

CSS Animation可以參考廖建翔老師的書 (12-4)

</aside>

CSS可以做一些基本的動畫效果

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <link href="<https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css>" rel="stylesheet" integrity="sha384-SgOJa3DmI69IUzQ2PVdRZhwQ+dy64/BUtbMJw1MZ8t5HZApcHrRKUc4W0kG879m7" crossorigin="anonymous">
    <link href="<https://cdn.datatables.net/2.3.0/css/dataTables.bootstrap5.css>" rel="stylesheet">
    <style>
      .img-scale:hover {
        transform: scale(1.2);
        transition: all 0.3s ease-in-out;
      }
      .img-rotate:hover {
        transform: rotateY(180deg);
        transition: all 1s ease-in-out;
      }
    </style>
  </head>
  <body>
    <img src="ben.webp" alt="Ben Wepp" class="img-scale rounded-circle" style="width: 200px; height: 200px;">
    <img src="ben.webp" alt="Ben Wepp" class="img-rotate rounded-circle" style="width: 200px; height: 200px;">

    <script src="<https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js>" integrity="sha384-k6d4wzSIapyDyv1kpU366/PK5hCdSbCRGRCMv+eplOQJWyd1fbcAu9OCUj5zNLiq" crossorigin="anonymous"></script>
  </body>
</html>

利用@keyframes創造動畫效果

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  /* background-color: red; */
  position: relative;
  animation: myfirst 5s linear 2s infinite alternate;
}

@keyframes myfirst {
  0%   {left:0px; top:0px; transform: rotate(0deg);}
  25%  {left:200px; top:0px; transform: rotate(10deg);}
  50%  {left:200px; top:200px; transform: rotate(0deg);}
  75%  {left:0px; top:200px; transform: rotate(-10deg);}
  100% {left:0px; top:0px; transform: rotate(0deg);}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>This example uses the shorthand animation property:</p>

<div><img src="ben.webp" alt="Ben Wepp" style="width: 200px; height: 200px;"></div>

</body>
</html>

Javascript

利用圖片的x、y位置來創造移動效果

<!DOCTYPE html>
<html>
<style>
#container {
  width: 400px;
  height: 400px;
  position: relative;
  background: yellow;
}
#animate {
  width: 200px;
  height: 200px;
  position: absolute;
  /* background-color: red; */
}
</style>
<body>

<p><button onclick="myMove()">Click Me</button></p> 

<div id ="container" onclick="myMove()>
  <div id ="animate">
    <img src="ben.webp" alt="Ben Wepp" style="width: 200px; height: 200px;">

  </div>
</div>

<script>
function myMove() {
  let id = null;
  const elem = document.getElementById("animate");   
  let pos = 0;
  clearInterval(id);
  id = setInterval(frame, 5);
  function frame() {
    if (pos == (400-200)) {
      clearInterval(id);
    } else {
      pos++; 
      elem.style.top = pos + "px"; 
      elem.style.left = pos + "px"; 
    }
  }
}
</script>

</body>
</html>

JS套件

參考Animation,改一下:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Anime.js 動畫範例</title>
  <style>
    body {
      background: #222;
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }
    .large {
      font-size: 3rem;
      letter-spacing: 0.2em;
    }
    span {
      display: inline-block;
      margin: 0 0.1em;
    }
  </style>
</head>
  <body>
    <h2 class="large">
      <span>輔</span>
      <span>仁</span>
      <span>大</span>
      <span>學</span>
      <span>資</span>
      <span>訊</span>
      <span>管</span>
      <span>理</span>
      <span>學</span>
      <span>系</span>
    </h2>
    <script src="<https://cdn.jsdelivr.net/npm/animejs/lib/anime.iife.min.js>"></script>

    <script>
      const { animate } = anime;
      animate('span', {
        // Property keyframes
        y: [
          { to: '-2.75rem', ease: 'outExpo', duration: 600 },
          { to: 0, ease: 'outBounce', duration: 800, delay: 100 }
        ],
        // Property specific parameters
        rotate: {
          from: '-1turn',
          delay: 0
        },
        delay: (_, i) => i * 50, // Function based value
        ease: 'inOutCirc',
        loopDelay: 1000,
        loop: true
    });
    </script>
  </body>
</html>

很炫吧~~

20250516-0553-01.4476887.mp4