这里介绍两种方式:
一:通过css样式中的 ":hover"实现,代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div{
background:red;
width:200px;
height:200px;
}
div:hover{
background:red;
width:500px;
height:500px;
}
</style>
</head>
<body>
<div>变大</div>
</body>
</html>
二:通过javascript方式实现,代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div{
background:red;
width:200px;
height:200px;
}
.divs{
background:red;
width:500px;
height:500px;
}
</style>
</head>
<body>
<div>变大</div>
<script>
// 首先获取div元素
var divs = document.getElementsByTagName('div')[0];
//鼠标移入,将divs的className样式赋给该标签
divs.onmouseover = function () {
this.className = "divs";
}
//鼠标移出,将空的className样式赋给该标签
divs.onmouseout = function () {
this.className = "";
}
</script>
</body>
</html>