简体   繁体   English

具有保存值的DVD喊话器声音开/关开关

[英]Dvz shoutbox sound on/off switch with saving value

i want to make shoutbox sound when shout is arrived. 我想在到达喊叫声时发出喊叫声。 I've got a script and i styled button but i cant make script working. 我有一个脚本并且设置了样式,但是我无法使脚本正常工作。 script for sound is: 声音脚本是:

dvz_shoutbox.callbacks['update'].push(function(){
if ($('#shoutbox .entry.new').length) {
    var audio = new Audio(rootpath + '/images/dvz_shoutbox.mp3');
    audio.volume = 0.2;
    audio.play();
}});

And it works,but i want to add on/of switch that will turn on and off this script, also i want to style it when it's on and off. 它有效,但是我想添加打开/关闭此脚本的开关,我也想在打开和关闭时设置样式。 I got css look like this: 我得到的CSS看起来像这样:

button.unmuted,button.muted {
vertical-align: middle;
padding: 0px 15px;
background: #060922;
line-height: 30px;
margin-top: -42px;
margin-left: 148px;
position: absolute;}

button.unmuted:hover {color:#c0c3bf} button.muted:hover {color:#c0c3bf;} button.muted:before{font-family: FontAwesome;content: "\\f026";} button.unmuted:before{font-family: FontAwesome;content: "\\f028";}

Can anyone help me ?? 谁能帮我 ??

Assuming that there is only one button in your HTML, you need to attach a click event handler to that button . 假设HTML中只有一个button ,则需要将click事件处理程序附加到该button The handler will toggle the classes muted and unmuted whenever the button is clicked. 每当单击button时,该处理程序将切换已mutedunmuted muted的类。

Then, in your callback that creates the audio, you can check to see if your button contains the class muted . 然后,在创建音频的回调中,您可以检查button包含muted的类。 If it does, then do not play the sound. 如果是,则不要播放声音。

Altogether, your code would look like so: 总共,您的代码如下所示:

dvz_shoutbox.callbacks['update'].push(function(){

    var $button = $("button");

    if ($button.length && $button.hasClass("unmuted")) { // checck if unmuted
        var audio = new Audio(rootpath + '/images/dvz_shoutbox.mp3');
        audio.volume = 0.2;
        audio.play();
    }
});

$("button").click(function() { // adds click handler

    var $this = $(this);

    $this.toggleClass("muted");
    $this.toggleClass("unmuted");
});

Note: in order for the code above to work, make sure your button starts out with class unmuted in your HTML. 注:为了使上述工作的代码,请确保您的button启动出去带班unmuted在你的HTML。

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

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