<html>
<head>
<meta charset="UTF-8">
<title>鼠标悬停效果</title>
<style type="text/css">
*{
margin: 0
padding: 0
}
body{
background-color: #000
}
a{
width: 200px
height: 50px
display: block
text-align: center
line-height: 50px
text-decoration: none
position: absolute
top: 50%
left: 50%
transform: translate(-50%,-50%)
font-size: 24px
font-weight: bold
color: white
border: 1px solid white
overflow: hidden
}
a::before{
content: ""
position: absolute
top: 0
left: -100%
width: 100%
height: 100%
background-image: linear-gradient(60deg,transparent,rgba(146,148,248,.4),transparent)
transition: all 0.5s
}
a:hover::before{
left: 100%
}
a:hover{
box-shadow: 0 2px 10px 8px rgba(146,148,248,.4)
}
</style>
</head>
<body>
<a href="#">鼠标悬停效果</a>
</body>
</html>
CSS+HTML<悬停下划线效果>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
display: flex
height: 100vh
justify-content: center
align-items: center
}
ul {
padding: 0
margin: 0
list-style-type: none
}
ul li{
padding: 5px 0
}
ul li a {
position: relative
display: inline-block
text-decoration: none
color: #3a3a3a
/* 转大写 */
text-transform: uppercase
padding: 4px 0
transition: 0.5s
font-weight: bold
}
ul li a::after {
position: absolute
content: ""
width: 100%
height: 3px
top: 100%
left: 0
background: #347cdb
transition: transform 0.5s
transform: scaleX(0)
transform-origin: right
}
ul li a:hover {
color: #585858
text-shadow: 0 0 10px #ccc
}
ul li a:hover::after {
transform: scaleX(1)
transform-origin: left
}
</style>
</head>
<body>
<ul>
<li><a href="#">home</a></li>
<li><a href="#">archives</a></li>
<li><a href="#">tags</a></li>
<li><a href="#">categories</a></li>
<li><a href="#">about</a></li>
</ul>
</body>
</html>
1、将对应的大图小图存放在同一个li标签中(鼠标悬浮到小图时,方便定位到大图)。
2、内部样式中利用"display:none"隐藏大图所在div。
3、鼠标悬浮到相应区域时,修改同li标签下大图所在div的display属性("display:block")。
小结:1、最上层的div必须定位,否则无法约束子元素。
2、li标签设置浮动时,图片会重叠,无法排成一列。浮动必须落实到图片。
3、小照片放在span标签中,变成了内联元素。需为其添加"display: block"转换为块级元素,才能设置图片外边距.
以下是代码:
[html] view plain copy
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
*{
margin: 0px
padding: 0px }
/*相框背景*/
#all{
width: 800px
height: 800px
background: greenyellow
position: relative
/*父元素必须定位,否则无法约束子元素*/
}
/*相册大图*/
#all ul li .big img{
width: 500px
height: 400px
}
#all ul li .big{
position: absolute
left: 70px
top: 130px
display: none/*内部样式中隐藏大图所在div*/
}
/*小图设置*/
#all ul li .smallhover img{
width: 70px
height: 50px
position: absolute
right: 100px
float: left/*设置浮动,可使图片排成一列*/
}
#all ul li .smallhover{
/*因为小照片放在span标签中,变成内联元素。设置display: block将其转换为块级元素,才能设置图片外边距*/
display: block
margin-top: 40px
overflow: hidden
}
/*实现效果,鼠标悬浮在小图片时,相应的大图片显示*/
#all ul li:hover .big{
display: block
}
</style>
</head>
<body>
<div id="all">
<ul>
<li>
<!--小图-->
<span class="smallhover"><img src="img/lotus1.jpg" /></span>
<!--大图-->
<div class="big" style="display: block" >
<!--设置初始图片-->
<img src="img/lotus1.jpg" />
</div>
</li>
<li>
<span class="smallhover"><img src="img/lotus2.jpg" /></span>
<div class="big"><img src="img/lotus2.jpg" /></div>
</li>
<li>
<span class="smallhover"><img src="img/lotus3.jpg" /></span>
<div class="big"><img src="img/lotus3.jpg" /></div>
</li>
</ul>
</div>
</body>
</html>