代码 :
<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><h1>Bubble Sort Animation</h1> <p class="container" id="balls-container"></p> <p class="description" id="description">Starting Bubble Sort...</p> <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("p"); 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>
登录后复制
以上就是使用html css和js的动画进行冒泡排序的详细内容,更多请关注抖狐科技其它相关文章!
本站文章均为抖狐网站建设摘自权威资料,书籍,或网络原创文章,如有版权纠纷或者违规问题,请即刻联系我们删除,我们欢迎您分享,引用和转载,我们谢绝直接复制和抄袭!感谢...
我们猜你喜欢
-
中秋节逐月同游!《球球大作战》传说光环免费送
9月13日,《球球大作战》中秋限定福利降临波拉哩!现在,限定光环「月兔小眠」助力畅游月宫,cp携手解锁头像框、情侣皮肤就在此刻,更有节日抽奖、红包福利等好礼放送,中秋赏月,才子佳人,欢度今宵!登录即送...
-
多少位的操作系统怎么看
查看操作系统版本的方法:windows:按下 windows 键 + r,输入 "winver" 并回车,在对话框中查看版本号。macos:点击 apple 菜单,选择 "关于本机",查看 "maco...
-
233乐园如何关闭家长守护模式 操作方法介绍
233乐园如何关闭家长守护模式?今天php中文网为大家带来操作方法介绍相关介绍。 ###233乐园关闭家长模式方法是:1、首先选择进入233乐园的软件界面,点击选择页面右下角我的选项。2、其次选择打开...
-
大智慧app股宝盯盘功能怎么关闭
如何关闭大智慧app的股宝盯盘功能?大智慧app深受股民喜爱,其出色的盯股功能和数据分析受到广泛好评。为了帮助广大用户,php小编新一特地整理了关闭大智慧app股宝盯盘功能的详细操作指南。本文将一步一...
-
如何提升域名解析速度
提升域名解析速度的方法:选择可靠的 dns 提供商使用 dns 预取启用 dns 缓存使用 dnssec使用 cdn优化 dns 记录(a、aaaa、cname、mx、txt)如何提升域名解析速度 域...