微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 基于Web的全屏幻灯片与视频元素

我想在全屏幕上开发基于 HTML 5的幻灯片与图像和视频.当几分钟内没有用户活动时,这将是我们的一个信息亭上的某种屏幕保护程序.我们已经在全屏幕上实现了基于图像的幻灯片,所以没有问题,但现在我们还要添加视频自动播放功能,例如让我们说这是屏幕内容的顺序

> image.jpeg
> image2.jpeg
> videoclip.mp4
> image3.jpeg
> someothervide.mp4

jquery脚本运行之后,我们想连续运行这些文件,并且在播放视频完成时显示图像几秒钟或自动启动视频并移动到下一张幻灯片

有人建议如何做到这一点,如果有任何已经实现的jQuery插件可以提供链接

解决方法

其实这很容易解决.查找 JavaScript评论内的所有解释.将所有这些都封装在$(document).ready(function(){})中;你准备好了.

HTML

<div id="canvas" class="canvas"></div>

CSS

div.canvas {
    display:           table-cell;
    width:             1280px;
    height:            800px;
    background:        black;
    vertical-align:    middle;
}

div.canvas > video {
    display:           block;
    margin:            auto;
}

div.canvas > img {
    display:           block;
    margin:            auto;
}

JavaScript – 变量

// array containing links to the content
var content = ['movie_1.m4v','movie_2.m4v','image_1.jpg','image_2.jpg'];
// element where anything will be played
var canvas = $('#canvas');
// duration an image is shown in ms (milliseconds)
var durationImage = 1000;
// basic source for image and video tag
var srcImage = '<img src="$" alt="">';
var srcVideo = '<video autoplay><source src="$" type="video/mp4"></source></video>';
// current position
var current = -1;
// regex for getting file extension (from https://stackoverflow.com/questions/680929/how-to-extract-extension-from-filename-string-in-javascript)
var regex = /(?:\.([^.]+))?$/;

JavaScript – 功能

// method to play the next content element
function playNext() {
    // increase current element and set it to 0 if end is reached
    ++current;  
    if (content.length == current) {
        current = 0;
    }
    // get file and its extension and check whether it's valid
    var source      = null;
    var file        = content[current];
    var extension   = regex.exec(file)[1];
    if ('jpg' == extension) {
        source      = srcImage;
    }
    if ('m4v' == extension) {
        source      = srcVideo;
    }
    // if source seems valid
    if (null !== source) {
        // replace placeholder with the content and insert content into canvas
        source  = source.replace('$',file);
        canvas.html(source);
        // if content is an image play next after set duration
        if ('jpg' == extension) {
            setTimeout(function() { playNext(); },durationImage);              
        }
        // if content is a video,bind 'onend' event handler to it,to play next
        if ('m4v' == extension) {
            canvas.find('video').bind("ended",function() {
                playNext();
            });
        }
    }
}

JavaScript – 最后:初始化函数调用

// show first (remember current = -1 from above :) )
playNext();

演示

Demo on jsfiddle.net

演示说明:由于提供的视频格式(视频/快速时间),该演示仅在Safari中运行(也可能在IE9中运行).

原文地址:https://www.jb51.cc/jquery/180040.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐