[英]jQuery click and background sound with on and off switch function
我需要 jQuery 的咔嗒聲和帶有開關的背景聲音。
以下是重要的:
這是我到目前為止編碼的內容:
$(".sound_control").click(function() { $(".on_off").toggle(); }); $(document).ready(function() { var background_sound = document.createElement("audio"); // This is the background sound function. background_sound.src = "https://www.pacdv.com/sounds/ambience_sounds/airport-gate-1.mp3"; background_sound.volume = 0.1; background_sound.autoPlay = true; background_sound.preLoad = true; background_sound.controls = true; $("*").hover(function() { // The sound should start playing when the website is loaded. I think the hover function is not the best solution. Also, the sound should be looped. background_sound.play(); }); var click_sound = document.createElement("audio"); // This is the click sound function. It should work for every button with the.click_sound class. click_sound.src = "http://soundbible.com/mp3/Stapler-SoundBible.com-374581609.mp3"; click_sound.volume = 0.1; click_sound.autoPlay = false; click_sound.preLoad = true; click_sound.controls = true; $(".click_sound").click(function() { click_sound.play(); }); });
.on_off:nth-child(2) { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="sound_control">Sound <span class="on_off">on</span><span class="on_off">off</span></button> <button class="click_sound">Button One</button> <button class="click_sound">Button Two</button> <button class="click_sound">Button Three</button> <button class="click_sound">Button Four</button>
如果有人可以幫助我,我會很高興。 :)
1.您可以使用 HTML5 音頻元素的currentTime
屬性來重新啟動音頻
將其設置為0
然后再次播放()聲音
2.將hover事件替換為background_sound.loop = true;
播放背景聲音 onload
3.對於背景聲音切換使用這個內部開/關按鈕.click
事件
4.添加變量SOUND_ON
& inside button click check if(SOUND_ON){}
然后播放點擊聲音
if (background_sound.currentTime){
background_sound.currentTime = 0;
background_sound.pause();
}else{
background_sound.play();
}
在下面運行代碼...希望有所幫助
$(document).ready(function() { var SOUND_ON = 1; var background_sound = document.createElement("audio"); background_sound.src = "https://www.pacdv.com/sounds/ambience_sounds/airport-gate-1.mp3"; background_sound.volume = 0.1; background_sound.autoPlay = true; background_sound.loop = true; background_sound.controls = true; background_sound.play(); var click_sound = document.createElement("audio"); click_sound.src = "http://soundbible.com/mp3/Stapler-SoundBible.com-374581609.mp3"; click_sound.volume = 0.1; click_sound.autoPlay = false; click_sound.preLoad = true; click_sound.controls = true; $(".click_sound").click(function() { if (SOUND_ON) { click_sound.currentTime = 0; click_sound.play(); } }); $(".sound_control").click(function() { $(".on_off").toggle(); if (background_sound.currentTime) { background_sound.currentTime = 0; SOUND_ON = 0; background_sound.pause(); } else { SOUND_ON = 1; background_sound.play(); } }); });
.on_off:nth-child(2) { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="sound_control">Sound <span class="on_off">on</span><span class="on_off">off</span></button> <button class="click_sound">Button One</button> <button class="click_sound">Button Two</button> <button class="click_sound">Button Three</button> <button class="click_sound">Button Four</button>
創建一個 boolean 變量,並在用戶單擊“Sound on”按鈕時將其值相應地更改為 True/False。 每次單擊“Sound on”或其更改形式的“Sound off”時檢查其值。 有了它,您可以播放和停止聲音。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.