Skip to content

Commit

Permalink
fix: Match1 adjust size by window
Browse files Browse the repository at this point in the history
--story=1
  • Loading branch information
studyzy committed Sep 25, 2024
1 parent c5502d9 commit 2c6c411
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions match1.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Emoji 匹配记忆训练</title>
<title>对对碰·匹配记忆训练</title>
<style>
body {
display: flex;
Expand All @@ -13,6 +13,7 @@
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
margin: 0;
}
h1 {
margin: 10px;
Expand All @@ -22,13 +23,10 @@
gap: 5px;
}
.grid-cell {
width: 60px;
height: 60px;
background-color: #87CEEB;
display: flex;
align-items: center;
justify-content: center;
font-size: 32px;
cursor: pointer;
border-radius: 10px;
}
Expand Down Expand Up @@ -175,6 +173,8 @@ <h1> 对对碰·匹配记忆游戏</h1>
}
document.getElementById('memoryTime').textContent = memoryTimeLeft.toFixed(1);
}, 100);

adjustGameBoardSize();
}

function hideEmojis() {
Expand Down Expand Up @@ -247,11 +247,42 @@ <h1> 对对碰·匹配记忆游戏</h1>
}
}

function adjustGameBoardSize() {
const gameBoard = document.getElementById('gameBoard');
const infoHeight = document.getElementById('info').offsetHeight;
const availableHeight = window.innerHeight - infoHeight - 40; // 40 for margins
const availableWidth = window.innerWidth - 40; // 40 for margins
const gapSize = 5; // 统一的格子间隔大小

// 计算行和列的最大数量
const maxCells = Math.max(rows, cols);
const size = Math.min(availableHeight, availableWidth);
const cellSize = (size - (maxCells - 1) * gapSize) / maxCells;

gameBoard.style.width = `${size}px`;
gameBoard.style.height = `${size}px`;
gameBoard.style.gridGap = `${gapSize}px`;
gameBoard.style.gridTemplateColumns = `repeat(${cols}, ${cellSize}px)`;
gameBoard.style.gridTemplateRows = `repeat(${rows}, ${cellSize}px)`;

cells.forEach(cell => {
cell.style.width = `${cellSize}px`;
cell.style.height = `${cellSize}px`;
cell.style.fontSize = `${cellSize * 0.6}px`; // Adjust font size based on cell size
});
}

function startGame() {
updateDifficulty();
createBoard();
updateTime();
}

// 窗口大小变化时调整游戏板大小
window.addEventListener('resize', adjustGameBoardSize);

// 初始调整游戏板大小
adjustGameBoardSize();
</script>
</body>
</html>
</html>

0 comments on commit 2c6c411

Please sign in to comment.