https://www.instagram.com/webstreet_code/
HTML CODE:
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Jashan's Puzzle Game</title> <link rel="stylesheet" href="style.css"> <!-- div.puzzle-piece#piece-$*9>img[src="./j${1}.png" alt="Piece ${1}"] --> <div class="puzzle-wrapper"> <div class="puzzle-container"> <div class="puzzle-piece" id="piece-1"> <img src="/static/imghwm/default1.png" data-src="./j1.png" class="lazy" alt="Piece 1"> </div> <div class="puzzle-piece" id="piece-2"> <img src="/static/imghwm/default1.png" data-src="./j2.png" class="lazy" alt="Piece 2"> </div> <div class="puzzle-piece" id="piece-3"> <img src="/static/imghwm/default1.png" data-src="./j3.png" class="lazy" alt="Piece 3"> </div> <div class="puzzle-piece" id="piece-4"> <img src="/static/imghwm/default1.png" data-src="./j4.png" class="lazy" alt="Piece 4"> </div> <div class="puzzle-piece" id="piece-5"> <img src="/static/imghwm/default1.png" data-src="./j5.png" class="lazy" alt="Piece 5"> </div> <div class="puzzle-piece" id="piece-6"> <img src="/static/imghwm/default1.png" data-src="./j6.png" class="lazy" alt="Piece 6"> </div> <div class="puzzle-piece" id="piece-7"> <img src="/static/imghwm/default1.png" data-src="./j7.png" class="lazy" alt="Piece 7"> </div> <div class="puzzle-piece" id="piece-8"> <img src="/static/imghwm/default1.png" data-src="./j8.png" class="lazy" alt="Piece 8"> </div> <div class="puzzle-piece" id="piece-9"> <img src="/static/imghwm/default1.png" data-src="./j9.png" class="lazy" alt="Piece 9"> </div> </div> <button id="shuffle-btn">Shuffle</button> </div> <script src="script.js"></script>
CSS CODE:
body { font-family: "Poppins", sans-serif; display: flex; justify-content: center; align-items: center; min-height: 100vh; background-color: #121212; color: #ffffff; margin: 0; } .puzzle-wrapper { display: flex; flex-direction: column; align-items: center; } .puzzle-container { display: grid; grid-template-columns: repeat(3, 100px); grid-template-rows: repeat(3, 100px); gap: 4px; margin-bottom: 20px; box-shadow: 0 20px 20px rgba(184, 26, 150, 0.879); border-radius: 15px; padding: 10px; background: #282828; } .puzzle-piece { position: relative; width: 100px; height: 100px; /* overflow: hidden; */ border-radius: 10px; box-shadow: 0 4px 6px rgba(226, 221, 221, 0.1); transition: transform 0.2s, box-shadow 0.2s; cursor: pointer; } .puzzle-piece img { border: 1px solid rgb(86, 10, 84); border-radius: 10px; width: 100%; height: 100%; filter: brightness(1) contrast(1); /* object-fit: cover; */ } .puzzle-piece.selected { border: 3px solid white; transform: scale(1.05); } .puzzle-piece:hover { box-shadow: 0 6px 12px rgba(255, 255, 255, 0.2); } #shuffle-btn { padding: 12px 25px; background-color: #e71d96; color: white; border: none; border-radius: 5px; font-size: 18px; cursor: pointer; transition: background-color 0.3s, box-shadow 0.3s; } #shuffle-btn:hover { background-color: #8c0a83; box-shadow: 0 4px 8px rgba(0, 255, 204, 0.3); }
JS CODE
let firstSelectedPiece = null; let secondSelectedPiece = null; // Initialize puzzle pieces array const pieces = Array.from(document.querySelectorAll('.puzzle-piece')); let shuffled = false; // Shuffle function // Shuffle function function shufflePuzzle() { // Create a new array to hold shuffled pieces const shuffledPieces = pieces.slice(); // Copy the original pieces array // Shuffle the array using Fisher-Yates shuffle algorithm for (let i = shuffledPieces.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [shuffledPieces[i], shuffledPieces[j]] = [shuffledPieces[j], shuffledPieces[i]]; // Swap elements } // Re-insert shuffled pieces into the puzzle container const puzzleContainer = document.querySelector('.puzzle-container'); shuffledPieces.forEach(piece => { puzzleContainer.appendChild(piece); // Append shuffled pieces to the container }); shuffled = true; // Mark as shuffled } // Swap two selected pieces function swapPieces(piece1, piece2) { setTimeout(() => { // Swap the images instead of text content const img1 = piece1.querySelector('img').src; const img2 = piece2.querySelector('img').src; piece1.querySelector('img').src = img2; // Set piece1's img to piece2's img piece2.querySelector('img').src = img1; // Set piece2's img to piece1's img piece1.classList.remove('selected'); piece2.classList.remove('selected'); firstSelectedPiece = null; secondSelectedPiece = null; checkCompletion(); }, 300); // Delay of 300ms for a smoother swap animation } // Check if puzzle is completed function checkCompletion() { // Get the current order of images by their src attributes const currentOrder = Array.from(document.querySelectorAll('img')).map(img => img.src); console.log("currentorderis:", currentOrder) // Define the correct order of the images const correctOrder = [ 'http://127.0.0.1:5500/htmlcss/j1.png', 'http://127.0.0.1:5500/htmlcss/j2.png', 'http://127.0.0.1:5500/htmlcss/j3.png', 'http://127.0.0.1:5500/htmlcss/j4.png', 'http://127.0.0.1:5500/htmlcss/j5.png', 'http://127.0.0.1:5500/htmlcss/j6.png', 'http://127.0.0.1:5500/htmlcss/j7.png', 'http://127.0.0.1:5500/htmlcss/j8.png', 'http://127.0.0.1:5500/htmlcss/j9.png' ]; // Compare the current order with the correct order if (JSON.stringify(currentOrder) === JSON.stringify(correctOrder)) { setTimeout(() => { alert("Congratulations! You completed the puzzle!"); }, 1000); } } // Add click event listeners for puzzle pieces pieces.forEach(piece => { piece.addEventListener('click', () => { if (!firstSelectedPiece) { firstSelectedPiece = piece; piece.classList.add('selected'); } else if (!secondSelectedPiece && piece !== firstSelectedPiece) { secondSelectedPiece = piece; piece.classList.add('selected'); swapPieces(firstSelectedPiece, secondSelectedPiece); } }); }); // Deselect pieces when clicking outside the puzzle document.addEventListener('click', (event) => { if (!event.target.classList.contains('puzzle-piece') && !event.target.closest('.puzzle-piece')) { if (firstSelectedPiece) { firstSelectedPiece.classList.remove('selected'); firstSelectedPiece = null; } if (secondSelectedPiece) { secondSelectedPiece.classList.remove('selected'); secondSelectedPiece = null; } } }); // Shuffle the puzzle at the beginning shufflePuzzle(); // Shuffle button document.getElementById('shuffle-btn').addEventListener('click', shufflePuzzle);
以上是使用 html css 和 js 的益智游戏的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript核心数据类型在浏览器和Node.js中一致,但处理方式和额外类型有所不同。1)全局对象在浏览器中为window,在Node.js中为global。2)Node.js独有Buffer对象,用于处理二进制数据。3)性能和时间处理在两者间也有差异,需根据环境调整代码。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver Mac版
视觉化网页开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6
视觉化网页开发工具