js实现随着手指滑动,5个元素围绕y轴进行圆周运动

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Circular Motion with Touch</title>
    <style>
      .circle-container {
        position: relative;
        height: 300px;
        width: 300px;
        margin: 50px;
        overflow: hidden;
      }

      .circle-item {
        position: absolute;
        width: 50px;
        height: 50px;
        background-color: red;
        border-radius: 50%;
        touch-action: none; /* Prevent scrolling when touching */
      }
    </style>
  </head>
  <body>
    <div class="circle-container" id="circleContainer">
      <!-- <div class="circle-item" id="circleItem1"></div> -->
    </div>

    <script>
      const container = document.getElementById("circleContainer");
      const items = Array.from({ length: 5 }, (_, i) =>
        document.createElement("div")
      );
      const itemIds = [
        "circleItem1",
        "circleItem2",
        "circleItem3",
        "circleItem4",
        "circleItem5",
      ];
      const radius = 100; // Adjust this based on your needs
      let angle = 0;
      let touchStartX = 0;

      items.forEach((item, index) => {
        item.className = "circle-item";
        item.id = itemIds[index];
        item.style.left = `${radius + radius * Math.cos(angle)}px`;
        item.style.top = `${radius + radius * Math.sin(angle)}px`;
        container.appendChild(item);
        angle += (2 * Math.PI) / items.length;
      });

      container.addEventListener("touchstart", (e) => {
        touchStartX = e.touches[0].clientX;
      });

      container.addEventListener("touchmove", (e) => {
        const touchMoveX = e.touches[0].clientX;
        const deltaX = touchMoveX - touchStartX;

        angle -= deltaX * 0.01; // Adjust this coefficient for speed
        touchStartX = touchMoveX;

        updatePositions();
      });

      function updatePositions() {
        items.forEach((item, index) => {
          const itemAngle = angle + (2 * Math.PI * index) / items.length;
          item.style.left = `${radius + radius * Math.cos(itemAngle)}px`;
          item.style.top = `${radius + radius * Math.sin(itemAngle)}px`;
        });
      }
    </script>
  </body>
</html>

实现效果:

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-21 07:36:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-21 07:36:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-21 07:36:02       87 阅读
  4. Python语言-面向对象

    2024-02-21 07:36:02       96 阅读

热门阅读

  1. 【gpt】中文写作论文prompt

    2024-02-21 07:36:02       52 阅读
  2. 修改SAP默认编辑器(MS WORD改为SAP TEXTEDITOR)

    2024-02-21 07:36:02       45 阅读
  3. 大语言模型LLM中Transformer模型的调用过程与步骤

    2024-02-21 07:36:02       46 阅读
  4. 【Hadoop】如何给hadoop集群增加一个队列

    2024-02-21 07:36:02       51 阅读
  5. 使用apoc将数据从数据库导入neo4j

    2024-02-21 07:36:02       47 阅读
  6. 小脑萎缩患者平时生活中应该注意哪些?

    2024-02-21 07:36:02       49 阅读
  7. 【clickhouse笔记】 查询表或列的磁盘占用大小

    2024-02-21 07:36:02       55 阅读