基于在线js编辑器,本文提供实现了一种简单的九宫格动画案例,具体步骤如下:
效果有:展开、指针悬浮变色、收缩。

DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Documenttitle>
head>
<body>
<div class="navigation">
<span style="--i: 0; --x: -1; --y: 0"><ion-icon name="construct-outline">ion-icon>
span>
<span style="--i: 1; --x: 1; --y: 0"><ion-icon name="game-controller-outline">ion-icon>span>
<span style="--i: 2; --x: 0; --y: -1"><ion-icon name="megaphone-outline">ion-icon>span>
<span style="--i: 3; --x: 0; --y: 1"><ion-icon name="person-circle-outline">ion-icon>span>
<span style="--i: 4; --x: 1; --y: 1"><ion-icon name="videocam-outline">ion-icon>span>
<span style="--i: 5; --x: -1; --y: -1"><ion-icon name="trash-outline">ion-icon>span>
<span style="--i: 6; --x: 0; --y: 0"><ion-icon name="stats-chart-outline">ion-icon>span>
<span style="--i: 7; --x: -1; --y: 1"><ion-icon name="save-outline">ion-icon>span>
<span style="--i: 8; --x: 1; --y: -1"><ion-icon name="cog-outline">ion-icon>span>
div>
<script type="module" src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js">script>
<script>
let navigation = document.querySelector('.navigation');
navigation.onclick = function() {
navigation.classList.toggle('active');
};
script>
<script nomodule src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js">script>
body>
html>
其中,navigation.classList.toggle('active');该方法会给DOM元素添加类或者是消除类,点击toggle标签时会触发生成active类。
可见如下实例,会切换< p>标签的显示和隐藏的状态:
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js">script>
<script type="text/javascript">
$(document).ready(function(){
$(".btn1").click(function(){
$("p").toggle();
});
});
script>
head>
<body>
<p>This is a paragraph.p>
<button class="btn1">Togglebutton>
body>
html>
toggle() 方法切换元素的可见状态。如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素。
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background: #10131c;
}
.navigation {
position: relative;
width: 70px;
height: 70px;
background: #212532;
border-radius: 10px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: 0.5s;
transition-delay: 0.8s;
}
.navigation.active {
width: 200px;
height: 200px;
transition-delay: 0s;
}
.navigation span {
position: absolute;
width: 7px;
height: 7px;
background: #fff;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
/* 使用translate方法来移动文字或图像,在参数中分别指定水平方向上的移动距离与垂直方向上的移动距离。 */
transform: translate(calc(12px * var(--x)), calc(12px * var(--y)));
transition: transform 0.5s, width 0.5s, height 0.5s, background 0.5s;
transition-delay: calc(0.1s * var(--i));
}
.navigation.active span{
width: 45px;
height: 45px;
background: #333849;
transform: translate(calc(60px * var(--x)), calc(60px * var(--y)));
}
.navigation span ion-icon{
/* font-size CSS 属性指定字体的大小 */
font-size: 0em;
transition: 0.5s;
}
.navigation.active span ion-icon{
font-size: 1.35em;
color: #fff;
}
.navigation.active span:hover ion-icon{
color: #2dfc52;
/* 把所有图像都改为黑白(100% 灰色): */
filter: drop-shadow(0 0 2px #2dfc52) drop-shadow(0 0 5px #2dfc52) drop-shadow(0 0 15px #2dfc52);
}
filter 属性定义了元素(通常是< img>)的可视效果(例如:模糊与饱和度)。
CSS语法具体如下:
filter: none | blur() | brightness() | contrast() | drop-shadow() | grayscale() | hue-rotate() | invert() | opacity() | saturate() | sepia() | url();
注意:使用空格分隔多个滤镜。;滤镜通常使用百分比(例如:75%或者是0.75).
transition 属性设置元素当过渡效果,四个简写属性为:
注意: 始终指定transition-duration属性,否则持续时间为0,transition不会有任何效果。
CSS语法如下:
transition: property duration timing-function delay;
justify-content 属性(水平)对齐弹性容器的项目,当项目不占用主轴上所有可用空间时。
注意:使用align-items属性垂直对齐项目。
CSS语法如下:
justify-content: flex-start|flex-end|center|space-between|space-around|initial|inherit;
常用属性如下:
Flexbox 是 flexible box 的简称(注:意思是“灵活的盒子容器”),是 CSS3 引入的新的布局模式。它决定了元素如何在页面上排列,使它们能在不同的屏幕尺寸和设备下可预测地展现出来。
之所以称之为flex,因为其能够拓展收缩flex容器内的元素,最大限度地填充可用空间。
在flexbox模型中,有三个核心的概念,flex项目:

定义了多根轴线的位置,若项目仅仅只有一条轴线,则设置不起作用。
