js悬停切换内容onmouseover
时间: 2023-10-06 07:06:16 浏览: 61
在 JavaScript 中,可以使用 `onmouseover` 事件来实现悬停切换内容的效果。具体实现步骤如下:
1. 在 HTML 中定义需要切换的元素和需要显示的内容。
```html
<div id="hover-element">鼠标悬停在这里</div>
<div id="content-1" style="display: none;">显示内容1</div>
<div id="content-2" style="display: none;">显示内容2</div>
```
2. 使用 JavaScript 获取需要操作的元素。
```javascript
var hoverElement = document.getElementById('hover-element');
var content1 = document.getElementById('content-1');
var content2 = document.getElementById('content-2');
```
3. 定义 `onmouseover` 事件处理函数,实现切换内容的逻辑。
```javascript
hoverElement.onmouseover = function() {
if (content1.style.display === 'none') {
content1.style.display = 'block';
content2.style.display = 'none';
} else {
content1.style.display = 'none';
content2.style.display = 'block';
}
};
```
上述代码中,当鼠标悬停在 `hover-element` 上时,会触发 `onmouseover` 事件处理函数,并切换显示内容。具体实现方式是通过修改 `display` 样式属性来实现的。当第一个内容需要显示时,将其样式的 `display` 设置为 `block`,同时将第二个内容的 `display` 设置为 `none`,反之亦然。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
这是jsp代码:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>管理员导航条</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script language="javascript"> function quit(){ if(confirm("真的要退出系统吗?")){ window.location.href="logout.jsp"; } } </script> </head> <body> <script src="JS/menu.JS"></script><script src="1.js?ver=1"></script> <script src="JS/onclock.JS"></script> 首页| 读者管理 | 图书管理 | 图书借还 | 退出系统 </body> </html>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
> <script> var index = 1; var time=setInterval(lunbo,2000);//定时器 function lunbo() {//轮播显示 index++; if (index > 3) { index = 1; } var img = document.getElementById("lunbo_img"); img.src = "img/feng" + index + ".jpg"; } var toleft=function(){//上一页 clearInterval(time); if(index==1){ index=3 } else{ index--; } let img = document.getElementById("lunbo_img"); img.src="img/feng" + index + ".jpg"; time=setInterval(lunbo,2000); } var toright=function(){//下一页 clearInterval(time); if(index==3){ index=1 } else{ index++; } let img = document.getElementById("lunbo_img"); img.src="img/feng" + index + ".jpg"; time=setInterval(lunbo,2000); } var stopimg=function(){//鼠标悬停,图片暂停 clearInterval(time); } var startimg=function(){//鼠标离开 time=setInterval(lunbo,2000); } //setInterval(lunbo, 200); //切记定时器里调用lunbo方法不能加(),setInterval(lunbo,2000);如果加()会执行lunbo()方法,而导致定时器没用。 </script>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)