效果图:
布局思路:
代码:
<!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>
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
margin-left: 500px;
margin-top: 100px;
}
.box .logo {
display: block;
width: 55px;
height: 55px;
background-color: #ff6700;
/* 超出盒子范围的不显示 */
overflow: hidden;
}
.logo::before {
/* 小米logo定位到盒子里 */
position: absolute;
content: '';
left: 0;
top: 0;
width: 55px;
height: 55px;
background: url(images/mi-logo.png) no-repeat center center;
/* before的过渡 */
transition: all .3s;
}
.logo::after {
/* 房子logo */
position: absolute;
content: '';
left: 0;
top: 0;
width: 55px;
height: 55px;
background: url(images/mi-home.png) no-repeat center center;
/* 虽然定位到盒子里,但是用外边距给它挤到盒子外边 */
margin-left: -55px;
/* after的过渡 */
transition: all .3s;
}
.logo:hover::before {
/* 鼠标移动到a标签上,小米logo往右移动到盒子外面
但是盒子里给了overflow:hidden 也就不显示了
*/
margin-left: 55px;
}
.logo:hover::after {
/* 将房子logo左外边距-55px重写为0,回到盒子里 */
margin-left: 0;
}
</style>
</head>
<body>
<div class="box">
<!-- a标签里包括伪元素的背景图片*2 -->
<a href="javascript:;" class="logo"></a>
</div>
</body>
</html>