js+css实现图片网格切片位置打乱冒泡排序还原效果代码
代码语言:html
所属分类:其他
代码描述:js+css实现图片网格切片位置打乱冒泡排序还原效果代码,将图片切成25个小块,位置打乱,然后用冒泡排序算法进行还原的过程。
代码标签: js css 图片 网格 切片 位置 打乱 冒泡 排序 还原
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> *{ margin: 0; padding: 0; box-sizing: border-box; } img{ display: block; } body{ min-height: 100svh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1rem; background-color: #000; color: #FFF; font-family: system-ui,sans-serif; } #container { width: 400px; height: 400px; display: grid; grid-template-columns: repeat(5, 1fr); padding: 1rem; border: 1px solid #FFFFFF50; border-radius: 5px; } .box{ transition: 500ms ease-in-out; } button { -webkit-appearance: none; text-align: inherit; background: none; box-shadow: none; cursor: pointer; border: 1px solid #FFFFFF30; color: inherit; font: inherit; outline: none; background-color: #033e97; padding: .5rem 1.25rem; border-radius: 5px; transition: background 300ms ease-in-out; } button:disabled { opacity: 50%; cursor: none; } button:hover, button:focus-visible { background-color: #4a0366; } </style> </head> <body translate="no"> <div id="container"></div> <div class="controls"> <button type="button" id="btn-new-image">New Image</button> <button type="button" id="btn-scramble">Scramble </button> <button type="button" id="btn-sort">Bubble sort</button> </div> <script > const DELAY = 50; const NUM_BOXES = 25; const container = document.getElementById("container"); const newImageBtn = document.getElementById("btn-new-image"); const scrambleBtn = document.getElementById("btn-scramble"); const sortBtn = document.getElementById("btn-sort"); const IMAGES = [ "//repo.bfw.wiki/bfwrepo/image/628436604b2ea.png?x-oss-process=image/auto-orient,1/resize,m_fill,w_400,h_400,/quality,q_90", "//repo.bfw.wiki/bfwrepo/image/629a8c7fd8ea6.png?x-oss-process=image/auto-orient,1/resize,m_fill,w_400,h_400,/quality,q_90", "//repo.bfw.wiki/bfwrepo/image/62de84ba13711.png?x-oss-process=image/auto-orient,1/resize,m_fill,w_400,h_400,/quality,q_90", "//repo.bfw.wiki/bfwrepo/image/655d5e532c0b7.png?x-oss-process=image/auto-orient,1/resize,m_fill,w_400,h_400,/quality,q_90", "//repo.bfw.wiki/bfwrepo/image/6556ad58df910.png?x-oss-process=image/auto-orient,1/resize,m_fill,w_400,h_400,/quality,q_90"]; let currentImageIndex = 0; // get random image function setRandomImage() { let randomIndex = Math.floor(Math.random() * IMAGES.length); // Ensure that the new index is different than the current one while (randomIndex === currentImageIndex) { randomIndex = Math.floor(Math.random() * IMAGES.length); } // set selected image as current currentImageIndex = randomIndex // udpate container with new image, divided into squares createBoxes(); // disable sort button until scrambled sortBtn.disabled = true; } // create boxes, each one having the corresponding part of the background image function createBoxes() { // empty container container.innerHTML = ''; // define current image url const imageUrl = IMAGES[currentImageIndex]; // Get container size, taking into account padding const containerStyles = window.getComputedStyle(container); const containerPaddingLeft = parseFloat(containerStyles.paddingLeft); const containerPaddingRight = parseFloat(containerStyles.paddingRight); const containerPaddingTop = parseFloat(containerStyles.paddingTop); const containerPaddingBottom = parseFloat(cont.........完整代码请登录后点击上方下载按钮下载查看
网友评论0