0

0

使用html css和js的动画进行冒泡排序

碧海醫心

碧海醫心

发布时间:2024-10-14 20:42:02

|

599人浏览过

|

来源于dev.to

转载

使用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>


热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
js正则表达式
js正则表达式

php中文网为大家提供各种js正则表达式语法大全以及各种js正则表达式使用的方法,还有更多js正则表达式的相关文章、相关下载、相关课程,供大家免费下载体验。

531

2023.06.20

js获取当前时间
js获取当前时间

JS全称JavaScript,是一种具有函数优先的轻量级,解释型或即时编译型的编程语言;它是一种属于网络的高级脚本语言,主要用于Web,常用来为网页添加各式各样的动态功能。js怎么获取当前时间呢?php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

576

2023.07.28

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

761

2023.08.03

js是什么意思
js是什么意思

JS是JavaScript的缩写,它是一种广泛应用于网页开发的脚本语言。JavaScript是一种解释性的、基于对象和事件驱动的编程语言,通常用于为网页增加交互性和动态性。它可以在网页上实现复杂的功能和效果,如表单验证、页面元素操作、动画效果、数据交互等。

6331

2023.08.17

js删除节点的方法
js删除节点的方法

js删除节点的方法有:1、removeChild()方法,用于从父节点中移除指定的子节点,它需要两个参数,第一个参数是要删除的子节点,第二个参数是父节点;2、parentNode.removeChild()方法,可以直接通过父节点调用来删除子节点;3、remove()方法,可以直接删除节点,而无需指定父节点;4、innerHTML属性,用于删除节点的内容。

494

2023.09.01

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

221

2023.09.04

Js中concat和push的区别
Js中concat和push的区别

Js中concat和push的区别:1、concat用于将两个或多个数组合并成一个新数组,并返回这个新数组,而push用于向数组的末尾添加一个或多个元素,并返回修改后的数组的新长度;2、concat不会修改原始数组,是创建新的数组,而push会修改原数组,将新元素添加到原数组的末尾等等。本专题为大家提供concat和push相关的文章、下载、课程内容,供大家免费下载体验。

240

2023.09.14

js截取字符串的方法介绍
js截取字符串的方法介绍

JavaScript字符串截取方法,包括substring、slice、substr、charAt和split方法。这些方法可以根据具体需求,灵活地截取字符串的不同部分。在实际开发中,根据具体情况选择合适的方法进行字符串截取,能够提高代码的效率和可读性 。

303

2023.09.21

C++多线程并发控制与线程安全设计实践
C++多线程并发控制与线程安全设计实践

本专题围绕 C++ 在高性能系统开发中的并发控制技术展开,系统讲解多线程编程模型与线程安全设计方法。内容包括互斥锁、读写锁、条件变量、原子操作以及线程池实现机制,同时结合实际案例分析并发竞争、死锁避免与性能优化策略。通过实践讲解,帮助开发者掌握构建稳定高效并发系统的关键技术。

4

2026.03.16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Sass 教程
Sass 教程

共14课时 | 1.0万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3.6万人学习

CSS教程
CSS教程

共754课时 | 43.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号