JavaScript视差场景:滑块控制游戏速度时的音频播放暂停实现
问题解决方案
核心问题是要在滑块拖动过程中实时检测速度值,并据此自动控制音频的播放/暂停状态。以下是完整的实现代码及逻辑说明:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>视差背景音频控制</title> <style> body { margin: 0; overflow: hidden; height: 100vh; } .parallax-container { position: relative; width: 100%; height: 100%; } .parallax-layer { position: absolute; width: 200%; height: 100%; background-repeat: repeat-x; background-position: bottom; } .layer-1 { background-image: url('https://picsum.photos/id/1015/1200/800'); background-size: cover; animation: scroll 60s linear infinite; } .layer-2 { background-image: url('https://picsum.photos/id/1016/1200/800'); background-size: cover; animation: scroll 30s linear infinite; } .layer-3 { background-image: url('https://picsum.photos/id/1018/1200/800'); background-size: cover; animation: scroll 15s linear infinite; } @keyframes scroll { 0% { transform: translateX(0); } 100% { transform: translateX(-50%); } } .speed-control { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; align-items: center; gap: 10px; color: white; font-family: Arial, sans-serif; background: rgba(0,0,0,0.5); padding: 10px; border-radius: 8px; } #speedSlider { width: 200px; } </style> </head> <body> <div class="parallax-container"> <div class="parallax-layer layer-3"></div> <div class="parallax-layer layer-2"></div> <div class="parallax-layer layer-1"></div> </div> <div class="speed-control"> <label for="speedSlider">游戏速度: <span id="speedValue">1</span></label> <input type="range" id="speedSlider" min="0" max="2" step="0.1" value="1"> </div> <audio id="gameAudio" loop> <source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mpeg"> 你的浏览器不支持音频播放 </audio> <script> const speedSlider = document.getElementById('speedSlider'); const speedValue = document.getElementById('speedValue'); const gameAudio = document.getElementById('gameAudio'); const parallaxLayers = document.querySelectorAll('.parallax-layer'); let gameSpeed = 1; // 更新视差滚动速度 function updateParallaxSpeed() { parallaxLayers.forEach((layer, index) => { const baseSpeed = [60, 30, 15][index]; layer.style.animationDuration = `${baseSpeed / gameSpeed}s`; }); } // 根据速度控制音频状态 function controlAudio() { if (gameSpeed === 0) { !gameAudio.paused && gameAudio.pause(); } else { gameAudio.paused && gameAudio.play().catch(err => console.log(err)); } } // 滑块实时监听事件 speedSlider.addEventListener('input', function() { gameSpeed = parseFloat(this.value); speedValue.textContent = gameSpeed.toFixed(1); updateParallaxSpeed(); controlAudio(); }); // 初始化状态 updateParallaxSpeed(); controlAudio(); </script> </body> </html>
关键实现逻辑
- 实时监听滑块状态:使用
input事件替代change事件,确保拖动滑块的每一刻都能触发状态更新 - 音频状态判断:每次速度变更时,检查当前速度值:
- 速度为0时,若音频未暂停则执行暂停
- 速度大于0时,若音频已暂停则执行播放
- 视差速度同步:通过修改CSS动画的
animationDuration属性,让背景滚动速度与gameSpeed成正比
注:部分浏览器的自动播放策略会阻止音频自动启动,首次拖动滑块后即可正常触发播放。
内容的提问来源于stack exchange,提问作者Little Me
相关产品推荐
相关产品推荐

