繁体   English   中英

将Javascript功能添加到多个元素中

[英]Add Javascript functionality to more than one element

我有几行Javascript定义了HTML5视频的自定义“播放”按钮。

我的问题是,它们只能用于一个视频,也只能用于一个视频。 我想在我的页面上放几个视频,并且使用相同的Javascipt代码来影响所有这些视频。 这样播放按钮就可以在每个视频上单独使用。 目前,它仅适用于以HTML列出的第一个视频。

我该如何工作?

JS

var vid, playbtn;

function intializePLayer(){
    vid = document.getElementById("my_video");
    playbtn = document.getElementById("playpausebtn");
    playbtn.addEventListener("click",playPause,false);
}

window.onload = intializePLayer;

function playPause(){
    if(vid.paused){
            vid.play();
            playbtn.style.opacity = '0';
    }else{
            vid.pause();
            playbtn.style.background = "url('http://i61.tinypic.com/xm8qdu.png')";
            playbtn.style.backgroundSize = "105px 105px";
            playbtn.style.opacity = '1';
    }
}

的CSS

#video_container{
    position:relative;
    width:480px;
    height:264px;
}

button#playpausebtn{
    background:url('http://i61.tinypic.com/xm8qdu.png') no-repeat;
    position:absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin:auto;
    border:none;
    outline:none;
    width:105px;
    height:105px;
    cursor:pointer;
    background-size: 105px 105px;
}

的HTML

<!-- Video #1 -->
<div id="video_container">
    <button id="playpausebtn"></button>
    <video id="my_video" width="480" loop>
        <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
    </video>
</div>
<!-- Video #2 -->
<div id="video_container">
    <button id="playpausebtn"></button>
    <video id="my_video" width="480" loop>
        <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
    </video>
</div>

这种方法将帮助您制作1-> n个视频。 在小提琴中,我们首先在DOM中搜索类名称为playpausebtn的元素,然后为每个元素添加一个onclick侦听器。 该侦听器获取视频对象(在这种情况下,该对象必须直接位于按钮之后,您可以更改此代码以使用递增的ID,例如video_1,video_2等,并以此方式找到视频对象),并将其添加到onclick事件侦听器中。

这只是一种方法,但这是解决问题的一种方法-如果您不了解任何内容,请提出问题。

http://jsfiddle.net/tL5ZU/1/

HTML:

<!-- Video #1 -->
<div>
    <button class="playpausebtn">Play</button>
    <video width="480" loop>
        <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" />
    </video>
</div>
<!-- Video #2 -->
<div>
    <button class="playpausebtn">Play</button>
    <video width="480" loop>
        <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" />
    </video>
</div>

JS

//set up listeners for buttons
function init() {
    //get all buttons
    var buttons = document.getElementsByClassName('playpausebtn');
    //for each button set up the onclick listener
    for (var i = 0; i < buttons.length; i++) {
        buttons[i].onclick = (function () {
            //the video needs to be right after the button
            var video = buttons[i].nextSibling.nextSibling;
            return function () {
                if (video.paused) {
                    video.play();
                } else {
                    video.pause();
                }
            };
        })();
    }
}

init();

暂无
暂无

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

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