简体   繁体   English

使用jQuery交换图像以静音/取消静音音频播放器

[英]Swap image using jquery for mute/unmute audio player

I need to change the image "icontrue,jpg" with "iconfalse.jpg" when click on the img, and when I click another in this image I need to return to icontrue.jpg , 单击img时,我需要将图像“ icontrue,jpg”更改为“ iconfalse.jpg” ,当我单击此图像中的另一个图像时,我需要返回icontrue.jpg

When icon set on "iconfalse.jpg" I require this function: 当图标设置在“ iconfalse.jpg”上时,我需要此功能:

document.getElementById('myaudio').muted = true;

and when return to "icontrue.jpg" I require the reverse: 当返回“ icontrue.jpg”时,我需要相反的操作:

document.getElementById('myaudio').muted = false;

I try over 10 methods for build this script, search in stackoverflow and other, but nothing methods is allowed. 我尝试了10种以上的方法来构建此脚本,在stackoverflow中进行搜索以及其他方法,但是不允许使用任何方法。

All methods that I build not return to original image and not unmute the audio. 我构建的所有方法都不会返回原始图像,也不会取消音频静音。 Thanks for answers. 感谢您的回答。

jsBin demo jsBin演示

Instead of images use <span> for your ico buttons: 代替图像,将<span>用作ico按钮:

<span data-audio="song1.ogg"></span> 
<span data-audio="song2.ogg"></span>

Now let's simply create our "Play / Stop" icons for our SPAN elements, 现在,我们只需为SPAN元素创建“播放/停止”图标,
On SPAN click we'll than toggle the .stop class using jQuery 在SPAN点击,我们将使用jQuery切换.stop

CSS 的CSS

[data-audio]{
    cursor:pointer;
    display:inline-block;
    vertical-align:middle;
    width:20px;
    height:20px;
    background: url(ico-play.jpg);
}
[data-audio].stop{
    background: url(ico.stop.jpg);
}

jQuery jQuery的

var $btn  = $("[data-audio]"),
    AUDIO = new Audio();

$btn.click(function() {     
    $btn.not(this).removeClass('stop');
    $(this).toggleClass('stop');    
    AUDIO.src = $(this).data().audio;
    AUDIO[ $(this).hasClass('stop') ? "play" : "pause" ]();
});

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

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