黑馬pink JavaScript學習筆記_Web APIs Day2

growingbambi發表於2024-07-23

事件監聽(繫結)

什麼是事件?

事件是系統內發生的動作或者發生的事情。比如:使用者點選頁面上的一個按鈕。

什麼是事件監聽?

就是讓程式檢測是否有事件產生,一旦有事件觸發,就立即呼叫一個函式做出響應,也稱為註冊事件

比如:滑鼠經過的時候,彈出一個alert“滑鼠經過了~”

語法

元素物件.addEventListener('事件型別', 要執行的函式)

事件監聽三要素:

  • 事件源:哪個DOM元素被觸發了
  • 事件型別:什麼方式觸發?比如:點選click, 滑鼠經過mouseenter, 滑鼠離開 mouseleave
  • 事件呼叫的函式:要做什麼事情
//滑鼠離開,開啟定時器
bigBox.addEventListener('mouseleave', function () {
    //先關閉之前的定時器
    clearInterval(timerId);
    //再開啟定時器
    timerId = setInterval(function () {
        next.click();
    }, 1000)
})

案例:隨機點名案例

image-20240723214550319

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        h2 {
            text-align: center;
        }

        .box {
            width: 600px;
            margin: 50px auto;
            display: flex;
            font-size: 25px;
            line-height: 40px;
        }

        .qs {

            width: 450px;
            height: 40px;
            color: red;

        }

        .btns {
            text-align: center;
        }

        .btns button {
            width: 120px;
            height: 35px;
            margin: 0 50px;
        }
    </style>
</head>

<body>
    <h2>隨機點名</h2>
    <div class="box">
        <span>名字是:</span>
        <div class="qs">這裡顯示姓名</div>
    </div>
    <div class="btns">
        <button class="start">開始</button>
        <button class="end">結束</button>
    </div>

    <script>
        // 資料陣列
        let arr = ['馬超', '黃忠', '趙雲', '關羽', '張飛']
        let timerId = 0;
        let random = 0;

        //開始按鈕模組
        const start = document.querySelector(".start");
        const qs = document.querySelector(".qs");

        start.addEventListener("click", function () {
            timerId = setInterval(function () {
                random = Math.floor(Math.random() * arr.length);
                qs.innerHTML = arr[random];
            }, 50)

            if (arr.length === 1) {
                start.disabled = true;
                end.disabled = true;
            }
        })

        //結束按鈕模組
        const end = document.querySelector(".end");
        end.addEventListener('click', function () {
            clearInterval(timerId);

            arr.splice(random, 1);
            console.log(arr);

        })
    </script>
</body>

</html>

image-20240723202945723

image-20240723203021562

事件型別

image-20240723214654888

案例:輪播圖點選切換

image-20240723214740602

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>輪播圖點選切換</title>
  <style>
    * {
      box-sizing: border-box;
    }

    .slider {
      width: 560px;
      height: 400px;
      overflow: hidden;
    }

    .slider-wrapper {
      width: 100%;
      height: 320px;
    }

    .slider-wrapper img {
      width: 100%;
      height: 100%;
      display: block;
    }

    .slider-footer {
      height: 80px;
      background-color: rgb(100, 67, 68);
      padding: 12px 12px 0 12px;
      position: relative;
    }

    .slider-footer .toggle {
      position: absolute;
      right: 0;
      top: 12px;
      display: flex;
    }

    .slider-footer .toggle button {
      margin-right: 12px;
      width: 28px;
      height: 28px;
      appearance: none;
      border: none;
      background: rgba(255, 255, 255, 0.1);
      color: #fff;
      border-radius: 4px;
      cursor: pointer;
    }

    .slider-footer .toggle button:hover {
      background: rgba(255, 255, 255, 0.2);
    }

    .slider-footer p {
      margin: 0;
      color: #fff;
      font-size: 18px;
      margin-bottom: 10px;
    }

    .slider-indicator {
      margin: 0;
      padding: 0;
      list-style: none;
      display: flex;
      align-items: center;
    }

    .slider-indicator li {
      width: 8px;
      height: 8px;
      margin: 4px;
      border-radius: 50%;
      background: #fff;
      opacity: 0.4;
      cursor: pointer;
    }

    .slider-indicator li.active {
      width: 12px;
      height: 12px;
      opacity: 1;
    }
  </style>
</head>

<body>
  <div class="slider">
    <div class="slider-wrapper">
      <img src="./images/slider01.jpg" alt="" />
    </div>
    <div class="slider-footer">
      <p>對人類來說會不會太超前了?</p>
      <ul class="slider-indicator">
        <li class="active"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
      <div class="toggle">
        <button class="prev">&lt;</button>
        <button class="next">&gt;</button>
      </div>
    </div>
  </div>
  <script>
    // 1. 初始資料
    const sliderData = [
      { url: './images/slider01.jpg', title: '對人類來說會不會太超前了?', color: 'rgb(100, 67, 68)' },
      { url: './images/slider02.jpg', title: '開啟劍與雪的黑暗傳說!', color: 'rgb(43, 35, 26)' },
      { url: './images/slider03.jpg', title: '真正的jo廚出現了!', color: 'rgb(36, 31, 33)' },
      { url: './images/slider04.jpg', title: '李玉剛:讓世界透過B站看到東方大國文化', color: 'rgb(139, 98, 66)' },
      { url: './images/slider05.jpg', title: '快來分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
      { url: './images/slider06.jpg', title: '嗶哩嗶哩小年YEAH', color: 'rgb(166, 131, 143)' },
      { url: './images/slider07.jpg', title: '一站式解決你的電腦配置問題!!!', color: 'rgb(53, 29, 25)' },
      { url: './images/slider08.jpg', title: '誰不想和小貓咪貼貼呢!', color: 'rgb(99, 72, 114)' },
    ]

    const img = document.querySelector(".slider-wrapper img");
    const p = document.querySelector('.slider-footer p');
    const prev = document.querySelector(".prev");
    const next = document.querySelector(".next");

    //下一頁按鈕
    let i = 0;
    next.addEventListener('click', function () {
      i++;

      //重置
      if (i >= sliderData.length) {
        i = 0;
      }

      toggle();
    })


    //上一頁按鈕
    prev.addEventListener('click', function () {
      i--;

      //重置
      if (i < 0) {
        i = 7;
      }

      toggle();
    })



    //自動播放
    let timerId = setInterval(function () {
      next.click();
    }, 1000)


    //滑鼠經過,停止定時器
    const bigBox = document.querySelector('.slider');
    bigBox.addEventListener('mouseenter', function () {
      clearInterval(timerId);
    })


    //滑鼠離開,開啟定時器
    bigBox.addEventListener('mouseleave', function () {
      //先關閉之前的定時器
      clearInterval(timerId);
      //再開啟定時器
      timerId = setInterval(function () {
        next.click();
      }, 1000)
    })



    //抽取公共方法
    function toggle() {
      //設定圖片及標題
      img.src = sliderData[i].url;
      p.innerHTML = sliderData[i].title;

      //設定radio
      document.querySelector('.active').classList.remove('active');
      document.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active');//這裡注意一下,也是i+1

      //設定底部背景色
      document.querySelector('.slider-footer').style.backgroundColor = sliderData[i].color;
    }

  </script>
</body>

</html>

相關文章