Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery音频淡出并暂停,单击淡入音频并继续_Jquery_Fadein_Fadeout - Fatal编程技术网

jQuery音频淡出并暂停,单击淡入音频并继续

jQuery音频淡出并暂停,单击淡入音频并继续,jquery,fadein,fadeout,Jquery,Fadein,Fadeout,这里有什么问题?单击#background#u audio div时,音频会淡出,然后自动淡入并继续播放,无需再次按下按钮。我怎么修理它 我试图使音频在单击#background(背景)音频时淡出并暂停,然后再次单击#background(背景)音频时,音频淡入并恢复 <script type="text/javascript"> $(document).ready(function(){ $('#mute').click(function(){ $('#ba

这里有什么问题?单击
#background#u audio div
时,音频会淡出,然后自动淡入并继续播放,无需再次按下按钮。我怎么修理它

我试图使音频在单击#background(背景)音频时淡出并暂停,然后再次单击#background(背景)音频时,音频淡入并恢复

<script type="text/javascript">
$(document).ready(function(){
    $('#mute').click(function(){
        $('#background_audio').animate({volume: 0}, 1000);
        setTimeout(function(){('#background_audio').pause()},1000);
    });
    $('#mute').click(function(){
        $('#background_audio').animate({volume: 1}, 1000);
        setTimeout(function(){('#background_audio').pause()},1000);
    });
});
</script>

$(文档).ready(函数(){
$(“#静音”)。单击(函数(){
$('#背景_音频')。制作动画({volume:0},1000);
setTimeout(function(){('#background_audio').pause()},1000);
});
$(“#静音”)。单击(函数(){
$(#background_audio')。制作动画({volume:1},1000);
setTimeout(function(){('#background_audio').pause()},1000);
});
});

您正在向同一jQuery元素添加两个单击处理程序,因此一次单击将调用两个处理程序

您必须添加一个
if
,以验证声音是否静音

当您单击按钮时,声音将通过fadeIn静音或取消静音

HTML

我不使用音频插件。我尝试了您提供的代码,它只在单击#background#u audio div后将音频淡出,但再次单击时,音频不会淡入并继续播放。您确定已将脚本更新到页面中吗?请清除缓存。是的,我确定(否则您的代码不会与我的代码表现不同)。JSFIDLE在这里:出于某种原因,JSFIDLE没有正确关闭and标记。。。所以它甚至不像我用你的代码在我的服务器上测试时那样静音。谢谢!这非常有效。我会投赞成票,但上面说我至少需要15个名声。
<div style="bottom:0; position:absolute; height:32px; width:100%;">
    <button id="mute">Mute sound</button>
    <audio id="background_audio"  src="http://www.noiseaddicts.com/samples/2541.mp3" autoplay="autoplay"></audio>
</div>
$(document).ready(function(){
    var backAudio = $('#background_audio');

    var muted = false;

    $('#mute').click(function(){
        var button = $(this);
        if (!muted) {
            button.attr("disabled", "");
            backAudio.animate({volume: 0}, 1000, function () {
                muted = true;
                button.removeAttr("disabled", "");
                button.text("Unmute sound");
            });
        }
        else {
            button.attr("disabled", "");
            backAudio.animate({volume: 1}, 1000, function () {
                muted = false;
                button.removeAttr("disabled", "");
                button.text("Mute sound");
            });
        }
    });
});