如何在页面上实现一个圆形的可点击区域?

方法1 使用border-radius

将radius设置为50%,则在元素高度和宽度相等的情况下,可以得到一个圆

<!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>Document</title>
    <style>
      #div {
        width: 100px;
        height: 100px;
        background-color: #bfa;
        border-radius: 50%;
        color: #000;
        font-size: 17px;
        margin: 100px auto;
        line-height: 100px;
        text-align: center;
      }
    </style>
    <script>
      window.onload = function () {
        var div = document.querySelector("#div");
        div.addEventListener("click", function () {
          alert("lalalalla");
        });
      };
    </script>
  </head>
  <body>
    <div id="div">可点击区域</div>
  </body>
</html>

canvas实现的两种方法:

方法1:

使用canvas画一个填充了制定颜色的圆,并且根据单像素判断的方法,如果发现当前点击的像素的颜色是我们指定的颜色,则触发回调事件

<!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;
      }
      #canvas {
        margin: 100px 0 0 200px;
      }
    </style>
    <script>
      window.onload = function () {
        // 1 获取画笔
        var canvas = document.querySelector("#canvas");
        canvas.style.backgroundColor = "#bfa";
        var ctx = canvas.getContext("2d");
        // 2 画圆
        ctx.fillStyle = "red";
        ctx.beginPath();
        ctx.arc(50, 50, 50, 0 * (Math.PI / 180), 360 * (Math.PI / 180));
        ctx.closePath();
        ctx.fill();

        // 3 添加点击事件,如果所点击的像素的颜色是红色,则表明点到了该元素上
        var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
        canvas.onclick = function (e) {
          e = e || window.e;
          let x = e.clientX - canvas.offsetLeft;
          let y = e.clientY - canvas.offsetTop;
          console.log(x, y);
          var color = getPxInfo(imageData, x, y);
          var targetColor = [255, 0, 0, 255];
          if (color[0] === 255) {
            alert("yes");
          }
        };

        // 获取相对于画布原点x,y处的像素点的颜色列表
        function getPxInfo(imageData, x, y) {
          var color = [];
          var data = imageData.data;
          var w = imageData.width;
          var h = imageData.height;
          color[0] = data[(y * w + x) * 4];
          color[1] = data[(y * w + x) * 4 + 1];
          color[2] = data[(y * w + x) * 4 + 2];
          color[3] = data[(y * w + x) * 4 + 3];

          return color;
        }
      };
    </script>
  </head>
  <body>
    <canvas id="canvas" width="100px" height="100px"></canvas>
  </body>
</html>

方法2:使用ctx.isPointInPath(x, y)

不使用像素的方法判断,使用canvas提供了ctx.isPointInPath(x, y)判断

<!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;
      }
      #canvas {
        margin: 100px 0 0 200px;
      }
    </style>
    <script>
      window.onload = function () {
        // 1 获取画笔
        var canvas = document.querySelector("#canvas");
        canvas.style.backgroundColor = "#bfa";
        var ctx = canvas.getContext("2d");
        // 2 画圆
        ctx.fillStyle = "red";
        ctx.beginPath();
        ctx.arc(50, 50, 50, 0 * (Math.PI / 180), 360 * (Math.PI / 180));
        ctx.closePath();
        ctx.fill();
        

        // 3 添加点击事件,如果所点击点在当前路径中,则表明点击到了该圆上
        canvas.onclick = function (e) {
          e = e || window.e;
          let x = e.clientX - canvas.offsetLeft;
          let y = e.clientY - canvas.offsetTop;
          if (ctx.isPointInPath(x, y)) {
            alert("yes");
          }
        };
      };
    </script>
  </head>
  <body>
    <canvas id="canvas" width="100px" height="100px"></canvas>
  </body>
</html>

还有一个方法参考:这里,使用html实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现这样的效果,可以使用HTML的`<map>`标签和`<area>`标签来定义热点区域。具体步骤如下: 1. 首先需要准备一张风景图片,并且确定需要分割成四个区域的位置和大小。 2. 使用 `<map>` 标签定义一个图像映射。该标签包含一个 name 属性,用于标识该映射,以及一个 id 属性,用于方便使用 JavaScript 操作该映射。 ```html <img src="scenery.jpg" usemap="#sceneryMap" /> <map name="sceneryMap" id="sceneryMap"></map> ``` 3. 在 `<map>` 标签中定义四个热点区域,每个区域对应一个菜单栏。使用 `<area>` 标签定义热点区域。该标签包含以下属性: - shape:热点区域的形状。可以是矩形(rect)、圆形(circle)或多边形(poly)。 - coords:热点区域的坐标。对于矩形,需要提供左上角和右下角的坐标;对于圆形,需要提供圆心和半径;对于多边形,需要提供每个顶点的坐标。 - href:热点区域点击后要跳转的链接。 - alt:热点区域的描述,用于辅助技术。 ```html <area shape="rect" coords="0,0,200,400" href="#" alt="菜单1" /> <area shape="rect" coords="200,0,400,400" href="#" alt="菜单2" /> <area shape="rect" coords="0,400,200,800" href="#" alt="菜单3" /> <area shape="rect" coords="200,400,400,800" href="#" alt="菜单4" /> ``` 4. 最后,将菜单栏的 HTML 代码添加到页面中。可以使用 CSS 样式来美化菜单栏。 ```html <div class="menu"> <ul> <li><a href="#">菜单1</a></li> <li><a href="#">菜单2</a></li> <li><a href="#">菜单3</a></li> <li><a href="#">菜单4</a></li> </ul> </div> ``` 在 CSS 中定义菜单栏的样式: ```css .menu { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; } .menu ul { list-style: none; display: flex; justify-content: space-between; width: 80%; } .menu li { margin-right: 20px; } .menu a { text-decoration: none; color: #fff; font-size: 24px; } ``` 最终的HTML代码如下: ```html <div class="page"> <img src="scenery.jpg" usemap="#sceneryMap" /> <map name="sceneryMap" id="sceneryMap"> <area shape="rect" coords="0,0,200,400" href="#" alt="菜单1" /> <area shape="rect" coords="200,0,400,400" href="#" alt="菜单2" /> <area shape="rect" coords="0,400,200,800" href="#" alt="菜单3" /> <area shape="rect" coords="200,400,400,800" href="#" alt="菜单4" /> </map> <div class="menu"> <ul> <li><a href="#">菜单1</a></li> <li><a href="#">菜单2</a></li> <li><a href="#">菜单3</a></li> <li><a href="#">菜单4</a></li> </ul> </div> </div> <style> .page { position: relative; } .menu { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; } .menu ul { list-style: none; display: flex; justify-content: space-between; width: 80%; } .menu li { margin-right: 20px; } .menu a { text-decoration: none; color: #fff; font-size: 24px; } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值