核心实现方法:
利用定位+透明度+设置外边距切换显示图片,过渡属性调节切换时间
整体盒子设置宽高背景色,利用伪元素设置要切换的两张图片
例子:小米logo左侧滑动切换
<!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>
.header-logo {
position: relative;
}
/* 设置a标签的样式 */
.logo {
display: block;
width: 55px;
height: 55px;
overflow: hidden;
background-color: #ff6700;
text-align: left;
text-indent: -9999em;
}
/* mi logo的样式 */
.logo::before {
/* 定位 */
position: absolute;
/* 伪元素必须要设置content属性 */
content: '';
/* 左偏移 */
left: 0;
/* 上偏移 */
top: 0;
width: 55px;
height: 55px;
/* 设置过渡 */
transition: all .3s;
/* 背景图片 */
background: url(./images/mi-logo.png) no-repeat center center;
/* 透明度 */
opacity: 1;
}
/* mi home 的样式 */
.logo::after {
position: absolute;
content: '';
left: 0;
top: 0;
width: 55px;
height: 55px;
transition: all .3s;
background: url(./images/mi-home.png) no-repeat center center;
margin-left: -55px;
opacity: 0;
}
/* 鼠标移入 让mi logo 往右侧进行滑动 */
.logo:hover::before {
opacity: 0;
margin-left: 55px;
}
/* 鼠标移入 让mi home 回到盒子中间 */
.logo:hover::after {
opacity: 1;
margin-left: 0;
}
</style>
</head>
<body>
<div class="header-logo">
<a href="#" class="logo" title="小米官网">小米官网</a>
</div>
</body>
</html>