新学一个JavaScript 的 classList API

一、语法
javascript 复制代码
element.classList.toggle(className);
二、场景用法:

点击一张图片放大再次点击的时候缩小

3、demo代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>图片放大缩小 Demo</title>
  <style>
    .image-container {
      width: 200px;
      height: 200px;
      display: flex;
      justify-content: center;
      align-items: center;
      overflow: hidden; /* 超过容器尺寸的部分隐藏 */
      border: 1px solid #ddd;
    }

    .image-container img {
      width: 100%; /* 图片填充容器 */
      transition: transform 0.3s ease-in-out; /* 动画效果 */
    }

    /* 放大后的样式 */
    .image-container.enlarged img {
      transform: scale(1.8); /* 放大1.8倍 */
    }
  </style>
</head>
<body>

  <div class="image-container" id="imageContainer">
    <img src="https://via.placeholder.com/200" alt="点击放大缩小" id="image">
  </div>

  <script>
    // 获取元素
    const imageContainer = document.getElementById('imageContainer');

    // 点击事件
    imageContainer.addEventListener('click', () => {
      // 切换放大缩小效果
      imageContainer.classList.toggle('enlarged');
    });
  </script>

</body>
</html>
相关推荐
油丶酸萝卜别吃4 小时前
utils.js 说明文档
开发语言·javascript·ecmascript
zyplayer-doc7 小时前
VuePress类静态文档站和动态知识库怎么选:两种技术路线的适用场景
javascript·人工智能·后端·安全·智能手机
leisoo80977 小时前
100GBA股股票数据怎么存ClickHouseRedisMySQLJSON完整对比
大数据·linux·服务器·开发语言·python
不会代码的小猴7 小时前
21. 泛型编程上
开发语言·c++·笔记·算法
一只旭宝8 小时前
细讲C加加【9】C++ std::function与std::bind详解|仿函数、绑定器、类成员绑定、占位符、成员偏移指针
开发语言·c++·算法
coder!mq10 小时前
说几个常见的语法糖?
java·开发语言·算法
gugucoding10 小时前
38. 【Java】Stream API(下):高级操作与性能
java·开发语言
Lhan.zzZ10 小时前
在 Visual Studio 2022 中打造可扩展的动态链接库模块:从零搭建到原理解析
开发语言·c++·visual studio
心平气和量大福大11 小时前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言
码哥DFS11 小时前
构造函数、实例对象、对象原型 ----三者关系
开发语言·javascript·原型模式