演示地址
Rotating Navigation (50projects50days.com)
实现思路
结构样式方面主要是flex弹性布局,绝对定位,并且添加过渡效果。
利用rotate来实现旋转动画。
代码
html结构
<!DOCTYPE html>
<html lang="zh-CN">
<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>旋转导航动画</title>
<link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.1.1/css/all.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="circle-container">
<div class="circle">
<button id="close">
<i class="fas fa-times"></i>
</button>
<button id="open">
<i class="fas fa-bars"></i>
</button>
</div>
</div>
<div class="content">
<h1>Amazing Article</h1>
<small>Florin Pop</small>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium quia in ratione dolores cupiditate,
maxime aliquid impedit dolorem nam dolor omnis atque fuga labore modi veritatis porro laborum minus,
illo, maiores recusandae cumque ipsa quos. Tenetur, consequuntur mollitia labore pariatur sunt quia
harum aut. Eum maxime dolorem provident natus veritatis molestiae cumque quod voluptates ab non, tempore
cupiditate? Voluptatem, molestias culpa. Corrupti, laudantium iure aliquam rerum sint nam quas dolor
dignissimos in error placeat quae temporibus minus optio eum soluta cupiditate! Cupiditate saepe
voluptates laudantium. Ducimus consequuntur perferendis consequatur nobis exercitationem molestias
fugiat commodi omnis. Asperiores quia tenetur nemo ipsa.</p>
<h3>My Dog</h3>
<img src="./image/dog.jpg" alt="doggy" />
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Sit libero deleniti rerum quo, incidunt vel
consequatur culpa ullam. Magnam facere earum unde harum. Ea culpa veritatis magnam at aliquid.
Perferendis totam placeat molestias illo laudantium? Minus id minima doloribus dolorum fugit deserunt
qui vero voluptas, ut quia cum amet temporibus veniam ad ea ab perspiciatis, enim accusamus asperiores
explicabo provident. Voluptates sint, neque fuga cum illum, tempore autem maxime similique laborum odio,
magnam esse. Aperiam?
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quod corrupti, at perspiciatis dicta minima
odit dolorum sunt aut ipsam exercitationem itaque, temporibus eaque perferendis ipsa nostrum et ea
distinctio! Tenetur.
Porro adipisci magnam vel aut omnis architecto iste eius aperiam fugit. Pariatur, modi. Eaque ratione
fugiat, temporibus qui assumenda, harum ad quam dicta quod in, obcaecati distinctio placeat asperiores
officiis!
</p>
</div>
</div>
<nav>
<ul>
<li><i class="fas fa-home"></i> Home</li>
<li><i class="fas fa-user-alt"></i> About</li>
<li><i class="fas fa-envelope"></i> Contact</li>
</ul>
</nav>
<script src="./script.js"></script>
</body>
</html>
css样式
* {
box-sizing: border-box;
}
body {
font-family: 'Lato', sans-serif;
background-color: #333;
color: #222;
overflow-x: hidden;
margin: 0;
}
.container {
padding: 50px;
width: 100vw;
min-height: 100vh;
background-color: #fafafa;
/* 旋转基点 */
transform-origin: top left;
transition: transform 0.5s ease;
}
.container.show-nav {
/* 逆时针旋转20° */
transform: rotate(-20deg);
}
.circle-container {
/* 绝对定位 */
position: fixed;
/* 只显示盒子的1/4 */
top: -100px;
left: -100px;
}
.circle {
/* 子元素绝对定位,父元素相对定位 */
position: relative;
height: 200px;
width: 200px;
border-radius: 50%;
background-color: #ff7979;
transition: transform 0.5s ease;
}
.container.show-nav .circle {
transform: rotate(-70deg);
}
.circle button#open {
left: 60%;
}
.circle button#close {
top: 60%;
transform: rotate(90deg);
transform-origin: top left;
}
.circle button {
cursor: pointer;
/* 绝对定位 */
position: absolute;
top: 50%;
left: 50%;
height: 100px;
background: transparent;
border: 0;
font-size: 26px;
color: #fff;
}
.content {
max-width: 1000px;
margin: 50px auto;
}
.content h1 {
margin: 0;
}
.content small {
color: #555;
}
.content p {
color: #333;
line-height: 1.5;
}
.content img {
width: 100%;
}
nav {
/* 固定定位 */
position: fixed;
bottom: 40px;
left: 0px;
z-index: 100;
}
nav ul {
list-style: none;
padding-left: 30px;
}
nav ul li {
/* 文字字母全大写 */
text-transform: uppercase;
color: #fff;
margin: 40px 0px;
/* X轴负方向移动本身的宽度,隐藏不显示 */
transform: translateX(-100%);
transition: transform 0.4s ease-in;
}
.container.show-nav+nav li {
transform: translateX(0);
transition-delay: 0.3s;
}
nav ul li i {
font-size: 20px;
margin-right: 10px;
}
nav ul li+li {
margin-left: 15px;
transform: translateX(-150%);
}
nav ul li+li+li {
margin-left: 30px;
transform: translateX(-200%);
}
js行为
// 定义需要用到的dom对象
const open = document.getElementById('open');
const close = document.getElementById('close');
const container = document.querySelector('.container');
open.addEventListener('click', () => {
container.classList.add('show-nav');
});
close.addEventListener('click', () => {
container.classList.remove('show-nav');
});
参考资料:50 Projects 50 Days | Traversy Media
50 unique mini-projects to sharpen your HTML, CSS & JavaScript skills
网友评论