[html+js][实现鼠标悬停事件]鼠标移动到行自动变色实现

下面是一个示例HTML代码,它创建了一个带有5行3列的表格,并为每一行添加了鼠标事件,当鼠标移到行上时,该行背景颜色变成蓝色,移出时恢复原来的颜色:

其中,onmouseoveronmouseout 事件分别表示鼠标移入和移出事件,changeColor() 函数根据传入的参数修改行的背景颜色。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>表格颜色变化</title>
    <style>
        table, th, td {
            border: 1px solid black;
        }
    </style>
</head>
<body>
<table id="myTable">
    <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
    </tr>
    <tr onmouseover="changeColor(this, true)" onmouseout="changeColor(this, false)">
        <td>张三</td>
        <td>20</td>
        <td>男</td>
    </tr>
    <tr onmouseover="changeColor(this, true)" onmouseout="changeColor(this, false)">
        <td>李四</td>
        <td>25</td>
        <td>女</td>
    </tr>
    <tr onmouseover="changeColor(this, true)" onmouseout="changeColor(this, false)">
        <td>王五</td>
        <td>30</td>
        <td>男</td>
    </tr>
</table>

<script>
    function changeColor(row, isMouseOver) {
        if (isMouseOver) {
            row.style.backgroundColor = "blue";
        } else {
            row.style.backgroundColor = "";
        }
    }
</script>
</body>
</html>

使用document.getelement改写上面的鼠标事件,解耦

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>表格颜色变化</title>
    <style>
        table, th, td {
            border: 1px solid black;
        }
    </style>
</head>
<body>
<table id="myTable">
  <tr>
    <th>Name</th>
    <th>Price</th>
    <th>Stock</th>
  </tr>
  <tr>
    <td>Product 1</td>
    <td>$10</td>
    <td>5</td>
  </tr>
  <tr>
    <td>Product 2</td>
    <td>$20</td>
    <td>10</td>
  </tr>
  <tr>
    <td>Product 3</td>
    <td>$30</td>
    <td>15</td>
  </tr>
</table>

<script>
  const table = document.getElementById("myTable");
  const rows = table.getElementsByTagName("tr");
  for (let i = 1; i < rows.length; i++) {
    rows[i].addEventListener("mouseover", function() {
      this.style.backgroundColor = "blue";
    });
    rows[i].addEventListener("mouseout", function() {
      this.style.backgroundColor = "";
    });
  }
</script>

</body>
</html>

这里我们首先使用 document.getElementById 方法获取表格元素,并获取表格中的所有行。然后使用 addEventListener 方法为每一行添加 mouseovermouseout 事件监听器,当鼠标移入时设置背景色为蓝色,当鼠标移出时恢复原来的颜色。注意在事件处理函数中使用 this 表示当前行的元素。 

继续解耦,如果有不同区域的行,可以为表格行添加一个类名,用于选择器。

<table id="myTable">
  <thead>
    <tr>
      <th>Name</th>
      <th>Price</th>
      <th>Stock</th>
      <th>Note</th>
    </tr>
  </thead>
  <tbody>
    <tr class="table-row">
      <td>Product 1</td>
      <td>$10.00</td>
      <td>20</td>
      <td>Note 1</td>
    </tr>
    <tr class="table-row">
      <td>Product 2</td>
      <td>$15.00</td>
      <td>10</td>
      <td>Note 2</td>
    </tr>
    <tr class="table-row">
      <td>Product 3</td>
      <td>$20.00</td>
      <td>5</td>
      <td>Note 3</td>
    </tr>
  </tbody>
</table>

此时的JS代码为:

const table = document.getElementById('myTable');

table.addEventListener('mouseover', function(event) {
  // 如果鼠标移入的是表格行,则设置其背景颜色为蓝色
  if (event.target.classList.contains('table-row')) {
    event.target.style.backgroundColor = 'blue';
  }
});

table.addEventListener('mouseout', function(event) {
  // 如果鼠标移出的是表格行,则将其背景颜色恢复
  if (event.target.classList.contains('table-row')) {
    event.target.style.backgroundColor = '';
  }
});

这样,当鼠标移入或移出表格行时,其背景颜色就会相应地变化。由于事件是委托到表格上的,因此不需要为每一行都绑定事件处理器,从而提高了代码的可维护性。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Python+matplotlib数据可视化鼠标悬停自动标注功能,可以使用matplotlib中的annotate()函数,该函数可以在图形中添加注释文本。 具体实现步骤如下: 1. 导入必要的库: ```python import matplotlib.pyplot as plt from matplotlib.offsetbox import OffsetImage, AnnotationBbox ``` 2. 创建图形并绘制数据: ```python fig, ax = plt.subplots() ax.plot(x_data, y_data) ``` 3. 定义鼠标悬停事件的回调函数,该函数将在鼠标悬停时被调用,根据鼠标所在的数据点位置添加注释文本: ```python def on_hover(event): for i in range(len(x_data)): if event.xdata is not None and event.ydata is not None: if x_data[i] - 0.5 <= event.xdata <= x_data[i] + 0.5 and \ y_data[i] - 0.5 <= event.ydata <= y_data[i] + 0.5: img = plt.imread(image_files[i]) imagebox = OffsetImage(img, zoom=0.2) ab = AnnotationBbox(imagebox, (x_data[i], y_data[i])) ax.add_artist(ab) fig.canvas.draw_idle() ``` 4. 将回调函数绑定到图形的鼠标悬停事件上: ```python fig.canvas.mpl_connect('motion_notify_event', on_hover) ``` 完整代码: ```python import matplotlib.pyplot as plt from matplotlib.offsetbox import OffsetImage, AnnotationBbox x_data = [1, 2, 3, 4, 5] y_data = [2, 4, 1, 5, 3] image_files = ['image1.png', 'image2.png', 'image3.png', 'image4.png', 'image5.png'] fig, ax = plt.subplots() ax.plot(x_data, y_data) def on_hover(event): for i in range(len(x_data)): if event.xdata is not None and event.ydata is not None: if x_data[i] - 0.5 <= event.xdata <= x_data[i] + 0.5 and \ y_data[i] - 0.5 <= event.ydata <= y_data[i] + 0.5: img = plt.imread(image_files[i]) imagebox = OffsetImage(img, zoom=0.2) ab = AnnotationBbox(imagebox, (x_data[i], y_data[i])) ax.add_artist(ab) fig.canvas.draw_idle() fig.canvas.mpl_connect('motion_notify_event', on_hover) plt.show() ``` 执该代码后,将会在图形中添加自动标注功能。当鼠标悬停在数据点附近时,会自动在该点添加注释文本,并显示与该数据点相关的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值