简体   繁体   English

在网站上打开和关闭声音

[英]turn sound on and off on a site

I am making a bit of an old-school website with sound effects. 我正在制作一个带有声音效果的老式网站。 Now I got the sound effects to run but I am running into a bit of a problem. 现在,我可以运行声音效果,但是我遇到了一个问题。 I'd like to know how I can turn them all on and off with a button. 我想知道如何使用按钮将它们全部打开和关闭。 I want to have them ON by default when loading a page and then you can toggle it on or off. 加载页面时,我希望默认情况下将它们打开,然后可以将其打开或关闭。

I got as far as making them play when you enter the site and when you click on links and such. 当您进入站点以及单击链接等时,我可以使它们播放。 But now I want to mute them all. 但是现在我要全部静音。 The Jquery .on and .off aren't working for me... I guess I am doing something terribly wrong here, I'm not that savvy with javascript: jQuery .on和.off不适用于我...我想我在这里做错了很多,我对javascript不太了解:

var siteAudio = function () {
 if ( $(".turn-off").hasClass("turnedon") ) {   


var galleryleft = $("#galleryleft")[0];
$(".arrowleft").click(function() {
galleryleft.play();
});

}
else {
var galleryleft = $("#galleryleft")[0];
$(".arrowleft").click(function() {
galleryleft.pause();
});

}


$( ".turn-off" ).click(function() {
$('.turn-off').toggleClass('turnedon');
$('.turn-off').toggleClass('turnedoff');
$(siteAudio).off();
});

window.onload = function () {
$(siteAudio).on();
}

Now this is working, but the sound should be off completely, instead it plays at the 4th time you click something ( Or at the 4th mouse-over when that is being used.) I hope anyone can say what I'm doing wrong here 现在可以正常工作了,但声音应该完全关闭,而是在您单击某物时第四次播放(或者在使用鼠标时,在第四次鼠标悬停时播放)。我希望任何人都可以在这里说出我做错了什么

I tink this will work based on what you've said. 天衣这会根据你所说的工作。 It seems you were overthinking things, and that is very common in jQuery/javascript :) 看来您想得太多了,这在jQuery / javascript中很常见:)

$(document).ready( function() {
  var galleryleft = $("#galleryleft")[0];
  var gallerySoundOn = true;


  $( ".turn-off" ).click( function(e) {
    if( gallerySoundOn ) { gallerySoundOn = false; }
    else { gallerySoundOn = true; }
    e.preventDefault();
  });

  $(".arrowleft").click( function(e) {
    if( gallerySoundOn ) { galleryleft.play(); }
    else { galleryleft.play(); galleryLeft.currentTime = 0; }
    e.preventDefault();
  });

});

let me know how it goes. 让我知道事情的后续。

Simey, you have just provided me with the information I needed :), thank you ever so much! Simey,您刚刚为我提供了我需要的信息:),非常感谢!

Only thing I had to change was the 我唯一需要改变的是

galleryleft.play(); galleryLeft.currentTime = 0;

I changed it into: 我将其更改为:

galleryleft.pause;

but for the rest this makes SO MUCH more sense then my script. 但是对于其余部分,这比我的脚本更有意义。 Indeed I was overcomplicating things. 确实,我使事情复杂化了。 I owe you a beer... 我欠你啤酒...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM