使用html css和js的动画进行冒泡排序
来源:dev.to
时间:2024-10-17 08:00:35 205浏览 收藏
学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《使用html css和js的动画进行冒泡排序》,以下内容主要包含等知识点,如果你正在学习或准备学习文章,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

代码 :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bubble Sort Animation</title>
<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #1c1c1c;
color: white;
font-family: Arial, sans-serif;
height: 100vh;
margin: 0;
}
h1 {
margin-bottom: 100px;
}
.container {
display: flex;
justify-content: center;
align-items: center;
gap: 20px;
}
.ball {
width: 70px;
height: 70px;
background-color: black;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 18px;
transition: transform 1.5s ease, box-shadow 1.5s ease;
}
.comparing {
box-shadow: 0px 0px 20px 5px yellow;
transform: translateY(-20px); /* Move upward */
}
.swapping {
box-shadow: 0px 0px 20px 5px red;
transform: translateY(20px); /* Move downward for swap effect */
}
.sorted {
background-color: green;
}
.description {
margin-top: 60px;
font-size: 25px;
color:yellow;
}
</style>
</head>
<body>
<h1>Bubble Sort Animation</h1>
<div class="container" id="balls-container"></div>
<div class="description" id="description">Starting Bubble Sort...</div>
<script>
const array = [5, 3, 8, 4, 2, 6]; // Initial array
const container = document.getElementById("balls-container");
const description = document.getElementById("description");
// Function to create the circular balls representing the array
function createBalls() {
container.innerHTML = '';
array.forEach((value, index) => {
const ball = document.createElement("div");
ball.classList.add("ball");
ball.textContent = value;
ball.setAttribute("id", `ball-${index}`);
container.appendChild(ball);
});
}
// Function to swap elements in the DOM
function swapElements(idx1, idx2) {
const ball1 = document.getElementById(`ball-${idx1}`);
const ball2 = document.getElementById(`ball-${idx2}`);
const tempText = ball1.textContent;
ball1.textContent = ball2.textContent;
ball2.textContent = tempText;
}
// Bubble Sort Algorithm with Animation
async function bubbleSort() {
let n = array.length;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n - i - 1; j++) {
// Highlight comparing elements
const ball1 = document.getElementById(`ball-${j}`);
const ball2 = document.getElementById(`ball-${j + 1}`);
ball1.classList.add("comparing");
ball2.classList.add("comparing");
description.textContent = `Comparing: ${array[j]} and ${array[j + 1]}`;
await new Promise(resolve => setTimeout(resolve, 3000)); // Slower animation
// Compare and swap if necessary
if (array[j] > array[j + 1]) {
description.textContent = `Swapping: ${array[j]} and ${array[j + 1]}`;
[array[j], array[j + 1]] = [array[j + 1], array[j]];
swapElements(j, j + 1);
ball1.classList.add("swapping");
ball2.classList.add("swapping");
await new Promise(resolve => setTimeout(resolve, 2000));
}
// Remove comparison and swapping highlights
ball1.classList.remove("comparing", "swapping");
ball2.classList.remove("comparing", "swapping");
}
// Mark the last sorted element
document.getElementById(`ball-${n - i - 1}`).classList.add("sorted");
}
description.textContent = "Array is sorted!";
}
// Initialize and start the animation
createBalls();
bubbleSort();
</script>
</body>
</html>
好了,本文到此结束,带大家了解了《使用html css和js的动画进行冒泡排序》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多文章知识!
声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
375 收藏
-
175 收藏
-
448 收藏
-
358 收藏
-
419 收藏
-
135 收藏
-
233 收藏
-
485 收藏
-
234 收藏
-
342 收藏
-
114 收藏
-
491 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习