效果一览:
思路:
所有的视图采用绝对定位absoulte
一同悬浮于文档流相同的位置,并通过transform: translateX(-100%)
隐藏于X轴左部,当前视图锚点触发时,让当前视图transformX(0px)
沿X轴移动到原点处进行显示。移动过程中通过transition: all 1s
控制移动速度,使其在移动过程中明显看到页面的切换效果。
实现代码:
html
<main>
<div id="home">
<i class="fa fa-home" aria-hidden="true"></i>
</div>
<div id="video">
<i class="fa fa-vimeo" aria-hidden="true"></i>
</div>
<div id="my">
<i class="fa fa-viadeo" aria-hidden="true"></i>
</div>
</main>
<nav>
<a href="#home">home</a>
<a href="#video">video</a>
<a href="#my">my</a>
</nav>
css
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
}
main {
flex: 1;
position: relative;
}
nav {
width: 100%;
height: 6vh;
background-color: #34495e;
display: flex;
justify-content: space-evenly;
align-items: center;
text-align: center;
}
nav a {
text-decoration: none;
color: white;
font-size: 1.5em;
text-transform: uppercase;
flex: 1;
font-weight: 700;
}
nav a:nth-child(2) {
border-right: 1px solid #aaa;
border-left: 1px solid #aaa;
}
main > div {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
transition: all 1s;
transform: translateX(-100%);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 2em;
}
main>div:target {
transform: translateX(0px);
}
main>div:nth-of-type(1):target {
background: #0984e3;
}
main>div:nth-of-type(2):target {
background: #fab1a0;
}
main>div:nth-of-type(3):target {
background: #fdcb6e;
}
div i[class^="fa"] {
font-size: 100px;
color: white;
}