javascript - jQuery 元素像 div 下的播放列表一样循环

标签 javascript jquery css html

我想在网页中制作一个类似数字标牌的效果,但我不知道该怎么做,请在这里提供一些帮助。

我在页面中有几个 div,它们是区域的占位符。 对于每个区域,内部有多个媒体。

在定义的持续时间内,当前媒体将播放,其他媒体将被隐藏(或破坏),后续媒体将在相同位置出现。

最重要的是,当到达最后一个元素时,我将再次重放整个序列。所以网页不断滚动。

我已经为这个案例制作了一个 JSFiddle,请帮忙解决这个问题,我已经花了一天时间解决我糟糕的编码......

https://jsfiddle.net/vx8Lp9xd/11/

HTML

 <body>
        <div class='timecontrol region1 position' >
            <video duration='30' width="320" height="240" controls class='position'>
              <source src="movie.mp4" type="video/mp4">
            </video>
            <video duration='0' width="320" height="240" controls class='position'>
              <source src="movie.mp4" type="video/mp4">
            </video>    
        </div>
        <div class='timecontrol region2 position'>
            <img class='position' duration='10' src='1.gif'></img>
            <img class='position' duration='20' src='2.gif'></img>
        </div>  
        <div class='readyState'></div>
        </body>

Javascript

function checkContainer () {
  if($('#readyState').is(':visible')){ //if the container is visible on the page
    $('.timecontrol').each(function() {
        var elements = new Array();
        elements = $(this).children().toArray();
        for (element in elements){

            $(element).not(element).remove();
            $(element).show();


$(element).delay(parseInt($(element).attr('duration'))*1000).hide(0);
            }
        });   
      } 
    }   
        $( document ).ready(function() {
            checkContainer();
    });

CSS

.position{
    position:absolute;
}

.region1{
    position:absolute;
    top:0px;
    left:0px;
    height:320px;
    width:240px;
}

.region2{
    position:absolute;
    top:px;
    left:320px;
    height:320px;
    width:100px;
    border:1px;
}

最佳答案

A recursive function (调用自身的函数)可能比 .each()for 循环更适合您的需求。下面是我将如何使用递归:

jsFiddle

function checkContainer() {
    if ($('#readyState').is(':visible')) { //if the container is visible on the page
        $('.timecontrol').each(function () {
            change($(this).children());
        });
    }
}
function change(elements, index) {
    index = !index || index > elements.length - 1 ? 0 : index; // if index is higher than the index of the last element or not set at all, default to 0

    var element = $(elements[index]);
    var timeframe = parseInt(element.attr('duration')) * 1000;
    element.siblings().hide();
    element.show();
    if (element[0].tagName.match(/video/i)) element[0].play();
    setTimeout(function () {
        element.hide(0);
        if (element[0].tagName.match(/video/i)) element[0].pause();
        change(elements, index+1); // recursively call this same functoin passing in the elements and the index of the next one up
    }, timeframe);

}
$(document).ready(function () {
   checkContainer();
});
.position {
    position:absolute;
}
.region1 {
    position:absolute;
    top:0px;
    left:0px;
    height:320px;
    width:240px;
}
.region2 {
    position:absolute;
    top:px;
    left:320px;
    height:320px;
    width:100px;
    border:1px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='timecontrol region1 position'>
    <video duration='6' width="320" height="240" controls class='position'>
        <source src="http://stream.flowplayer.org/functional.mp4" type="video/mp4">
    </video>
    <video duration='6' width="320" height="240" controls class='position'>
        <source src="http://stream.flowplayer.org/bauhaus.mp4" type="video/mp4">
    </video>
</div>
<div class='timecontrol region2 position'>
    <img class='position' duration='10' src='https://placeimg.com/350/150/any'></img>
    <img class='position' duration='20' src='https://placeimg.com/350/150/animals'></img>
</div>
<div id='readyState'></div>

关于javascript - jQuery 元素像 div 下的播放列表一样循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32298970/

相关文章:

javascript - Bootstrap 固定边栏示例

javascript - 动态创建的复选框单击检测没有类或 id Jquery

html - 安排多个div如何最好地处理布局

css - CSS 显示属性的过渡

javascript - Leafletjs动态绑定(bind)到可见标记/簇

javascript - 删除/替换 Unicode 字符 javascript

javascript - Angular Directive(指令)链接函数中的 "attrs"参数有什么用

javascript - 如果重叠,则在窗口中重新定位 div

jquery - 使用 JSON 发送表单后重新加载位置

javascript - 强制带 % 宽度的 strip 像没有空格的选取框一样移动