通过javascript监听点击事件并动态添加.active类来实现选中状态高亮;2. 使用transition属性设置background-color的平滑过渡效果;3. 调整@keyframes中scale、opacity、animation-duration和animation-timing-function等参数优化脉冲动画视觉效果,最终实现具有交互反馈和动态视觉吸引力的分页导航点。

CSS创建分页导航点动画,核心在于利用
@keyframes
创建分页导航点动画,主要涉及以下步骤:定义关键帧动画、选择导航点元素、应用动画效果,以及调整动画参数。
选中状态的高亮显示通常通过CSS伪类
:active
:focus
立即学习“前端免费学习笔记(深入)”;
CSS部分:
.nav-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #ccc;
margin: 0 5px;
display: inline-block;
cursor: pointer;
}
.nav-dot.active {
background-color: #007bff; /* 选中状态的颜色 */
}JavaScript部分:
const navDots = document.querySelectorAll('.nav-dot');
navDots.forEach(dot => {
dot.addEventListener('click', function() {
// 移除所有导航点的 active 类
navDots.forEach(d => d.classList.remove('active'));
// 为当前点击的导航点添加 active 类
this.classList.add('active');
// 可以根据导航点的位置,切换内容区域的显示
// 例如:
// const index = Array.from(navDots).indexOf(this);
// showContent(index);
});
});这段代码首先定义了导航点的基本样式,以及选中状态下的
.active
.active
.active
平滑过渡效果可以使用CSS的
transition
transition
.nav-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #ccc;
margin: 0 5px;
display: inline-block;
cursor: pointer;
transition: background-color 0.3s ease; /* 添加过渡效果 */
}
.nav-dot:hover {
background-color: #999; /* 鼠标悬停时的颜色 */
}
.nav-dot.active {
background-color: #007bff; /* 选中状态的颜色 */
}在这个例子中,
transition: background-color 0.3s ease;
background-color
ease
linear
ease-in
ease-out
ease-in-out
@keyframes
transform: scale()
opacity
box-shadow
.nav-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #ccc;
margin: 0 5px;
display: inline-block;
cursor: pointer;
position: relative; /* 必须设置position,才能使用伪元素 */
}
.nav-dot::before {
content: '';
display: block;
position: absolute;
width: 100%;
height: 100%;
border-radius: 50%;
background-color: rgba(0, 123, 255, 0.5); /* 脉冲颜色 */
transform: scale(1);
opacity: 0;
animation: pulse 1.5s infinite; /* 应用动画 */
}
@keyframes pulse {
0% {
transform: scale(1);
opacity: 0.5;
}
100% {
transform: scale(1.5); /* 脉冲大小 */
opacity: 0;
}
}在这个例子中,
.nav-dot::before
@keyframes pulse
transform: scale(1.5)
opacity
animation: pulse 1.5s infinite;
transform: scale(1.5)
transform: scale(2)
animation-duration
1s
此外,还可以通过调整脉冲的颜色(
background-color
animation-timing-function
ease-in-out
以上就是CSS如何创建分页导航点动画?@keyframes脉冲效果的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号