Javascript声音开始/停止和图像更改 [英] Javascript Sound Start/Stop and Image Change

查看:123
本文介绍了Javascript声音开始/停止和图像更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JS的新手,但我有一个很简单的问题.

I am new to JS and I have a pretty simple-seeming problem.

我希望有一个较小的图像,单击该图像可以更改为其他图像,同时开始播放循环播放的声音文件.再次单击该图像时,它会变回原始图像,并且还会停止声音文件.

I want to have a small image that when clicked changes to a different image and at the same time begins playing a looped sound file. When the image is clicked a second time it changes back to the original image and also stops the sound file.

您可以将其视为显示开始"的按钮.单击开始"时,它将循环播放声音,并更改为停止";单击停止"时,它将返回开始播放,并且声音停止播放.

You can think of it as a button that says "start". when "start" is clicked it loops the sound and changes to "stop" and when "stop" is clicked it goes back to start and the sound ceases playing.

我已经得到尽可能创建标签,其是正方形,当检查播放声音,并且当未选中停止声音的无显示的复选框内.问题是我无法使用"check","uncheck"将复选框更改为不同的图像.

I've gotten as far as creating none-displayed checkbox inside of a label which is a square that when checked plays sound and when unchecked stops sound. Problem is I can't get the checkbox to change into different images with "check", "uncheck".

我也有一些带有链接更改图像的代码,但是我不知道如何播放声音.

I've also got some code that has a link change images, but I cannot figure out how to make it play the sound.

所以基本上我需要将这两件事结合起来.但是我不知道怎么做.在过去的两天里,我一直在不停地搜寻,但找不到清晰且简单的答案.

SO basically i need to combine these two things. BUT I CAN'T figure out how. And I've been googling for the past two days nonstop but cannot find a clearcut and simple answer.

知道我计划在同一页面上放置许多这些小的可单击图像可能会有所帮助,因此Javascript必须能够保持简洁,但会影响所有链接或div或它们最终的作用.

It may help to know that I plan on having many of these small clickable images on the same page, so the Javascript needs to be able to be light but effect all of the links or divs or whatever they are in the end.

很抱歉这么长的问题.有人吗?

Sorry for such a long question. Anybody?

提前谢谢!

推荐答案

<html>
    <head>
        <script type="text/javascript">
            var pos = 0
            var sId = 'sound';

            function change() {
                if(pos == 0) {
                    pos = 1;
                    document.getElementById('btn').src="http://www.buzzingup.com/wp-content/uploads/2011/07/stop.png";

                    var e = document.createElement('embed');
                    e.setAttribute('src','beep.mp3');
                    e.setAttribute('id',sId);
                    e.setAttribute('hidden','true');
                    e.setAttribute('autostart','true');
                    e.setAttribute('loop','true');

                    document.body.appendChild(e);
                } else {
                    pos = 0;
                    document.getElementById('btn').src="http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png";
                    document.body.removeChild(document.getElementById(sId));
                }
            }
        </script>
    </head>
    <body>
        <img src="http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png" onClick="change()" id="btn" />
    </body>
</html>

那怎么办,我认为应该可以.

How about that, I think it should work.

这是应该满足您需求的OO版本:

Here is an OO version that should do what you need:

<html>
    <head>
        <script type="text/javascript">
            function imageSwitch(_imgID,_imgStart,_imgStop,_soundFile) {
                this.imgID = _imgID;
                this.imgStart = _imgStart;
                this.imgStop = _imgStop;
                this.soundFile = _soundFile;

                this.pos = 0;
                this.e;

                this.change = function() {
                    if(this.pos == 0) {
                        this.pos = 1;
                        document.getElementById(this.imgID).src = this.imgStop;

                        this.e = document.createElement('embed');
                        this.e.setAttribute('src',this.soundFile);
                        this.e.setAttribute('id','sound'+this.imgID);
                        this.e.setAttribute('hidden','true');
                        this.e.setAttribute('autostart','true');
                        this.e.setAttribute('loop','true');

                        document.body.appendChild(this.e);
                    } else {
                        this.pos = 0;
                        document.getElementById(this.imgID).src = this.imgStart;
                        this.e.parentNode.removeChild(this.e);
                    }
                }
            }
        </script>
        <script type="text/javascript">
            var abc = new imageSwitch('btn1','http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png','http://www.buzzingup.com/wp-content/uploads/2011/07/stop.png','beep.mp3');
            var def = new imageSwitch('btn2','http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png','http://www.buzzingup.com/wp-content/uploads/2011/07/stop.png','beep.mp3');
        </script>
    </head>
    <body>
        <img src="http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png" onClick="abc.change()" id="btn1" />
        <img src="http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png" onClick="def.change()" id="btn2" />
    </body>
</html>

这篇关于Javascript声音开始/停止和图像更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆