【问题标题】:Pausing jQuery slider when Video is playing播放视频时暂停 jQuery 滑块
【发布时间】:2013-11-11 16:35:35
【问题描述】:

我在我的网站上使用了一个 jQuery 滑块,它允许嵌入 Vimeo 视频,我想知道如何制作它,以便如果有人点击 vimeo 视频上的“播放”,滑块将停止自动旋转直到视频结束或用户点击“暂停”。我假设我需要利用 Vimeo API 来完成此操作。这是我的滑块代码,非常感谢任何帮助!

更新在 Trevor 的帮助下解决了这个问题,这里是滑块的代码:

var slideInProgress = 0;
var currentSlideIndex = 0,
    slides;
var playing = false;    
function setTopSlider() {
    if (jQuery('#top_slider #container .slide').length != 0) {
        slides = jQuery('#top_slider #container > .slide');
        for (var i = 0; i <= slides.length; i++) {
            jQuery(slides[i]).css('left', '100%');
        }
        ;
        jQuery(slides[currentSlideIndex]).css('left', '0%');
        var el = jQuery('.dot:eq(' + currentSlideIndex + ')');
        src = el.css("background-image").replace("_off", "_over");
        el.css("background-image", src);
        el.addClass('active');
    };
};

function slide(dir) {
    if (slideInProgress != 0) {
        return;
    }
    slideInProgress = 3;
    var current, next, nextSlide;
    var slideSpeed = 200;
    current = jQuery(slides[currentSlideIndex]);
    if (!isNaN(dir)) {
        next = dir;
        if (next > currentSlideIndex)
            dir = 'left';
        else
            dir = 'right';
    };
    if (dir == 'left') {
        if (!next) {
            next = currentSlideIndex + 1;
            if (next >= slides.length) {
                next = 0;
            }
        }
        nextSlide = jQuery(slides[next]);
        nextSlide.css('left', '100%');
        nextSlide.css('z-index', parseInt(current.css('z-index'), 10) + 1);
        //nextSlide.animate({left: '0%'}, 1500);
        nextSlide.animate({
            left: '0%'
        }, {
            duration: slideSpeed,
            complete: function() {
                slideInProgress--;
            }
        });
        //current.animate({left: '-100%'}, 1500);
        current.animate({
            left: '-100%'
        }, {
            duration: slideSpeed,
            complete: function() {
                slideInProgress--;
            }
        });

    } else {
        console.log('moving right');
        if (!next) {
            next = currentSlideIndex - 1;
            if (next < 0) {
                next = slides.length - 1;
            }
        }
        nextSlide = jQuery(slides[next]);
        nextSlide.css('left', '-100%');
        nextSlide.css('z-index', parseInt(current.css('z-index'), 10) + 1);
        //nextSlide.animate({left: '0%'}, 1500);
        nextSlide.animate({
            left: '0%'
        }, {
            duration: slideSpeed,
            complete: function() {
                slideInProgress--;
            }
        });
        //current.animate({left: '100%'}, 1500);
        current.animate({
            left: '100%'
        }, {
            duration: slideSpeed,
            complete: function() {
                slideInProgress--;
            }
        });

    }
    current.addClass('active');
    nextSlide.removeClass('active');
    var el = jQuery('.dot:eq(' + currentSlideIndex + ')');
    src = el.css("background-image").replace("_over", "_off");
    el.css("background-image", src);
    el.removeClass('active');
    el = jQuery('.dot:eq(' + next + ')');
    src = el.css("background-image").replace("_off", "_over");
    el.css("background-image", src);
    el.addClass('active');
    console.log('currentSlideIndex' + currentSlideIndex);
    console.log('next' + next);
    console.log('dir' + dir);
    currentSlideIndex = next;
    // **** //
    slideInProgress--;
    // **** //
}
setTopSlider();
playing = setInterval(function() {slide('left')}, 6000);

以及我是如何将它与 Vimeo API 绑定的:

(function () {

    var $=jQuery;


    var f = $('iframe');
    var url = f.attr('src').split('?')[0]; <?php //HACK! had to hard code the protocol in here or postMethod shows error: Uncaught SyntaxError: An invalid or illegal string was specified. ?>
    //var status = $('.status');


    // Listen for messages from the player
    if (window.addEventListener){
        window.addEventListener('message', onMessageReceived, false);
    } else {
        window.attachEvent('onmessage', onMessageReceived, false);
    }

    // Handle messages received from the player
    function onMessageReceived(e) {
        var data = JSON.parse(e.data);


        switch (data.event) {
            case 'ready':
                onReady();
                break;

            case 'playProgress':
                onPlayProgress(data.data);
                break;

            case 'pause':
                onPause();
                break;

            case 'finish':
                onFinish();
                break;
        }
    }

    // Call the API when a button is pressed
    $('button').on('click', function() {
        post($(this).text().toLowerCase());
    });

    // Helper function for sending a message to the player
    function post(action, value) {
        var data = { method: action };

        if (value) {
            data.value = value;
        }
        $('iframe')[0].contentWindow.postMessage(JSON.stringify(data), url);
    }

    function onReady() {


        post('addEventListener', 'pause');
        post('addEventListener', 'finish');
        post('addEventListener', 'playProgress');
    }

    function onPause() {
        console.log("vimeo paused");
    }

    function onFinish() {
        playing = setInterval(function() {slide('left')}, 6000);
        console.log("vimeo finish");
        slide('left');
    }

    function onPlayProgress(data) {
        clearInterval(playing);
        console.log("vimeo play progress");
    }
})();

【问题讨论】:

    标签: jquery carousel vimeo


    【解决方案1】:

    如何创建一个布尔变量playing 并使用它来跟踪视频何时播放。如果视频正在播放,请不要运行幻灯片功能。

    例如

    var playing = false;
    
    function slide(dir) {
       if(playing)
            return false;
       ...
    }
    
    function onPause() {
        playing = false;
        console.log("vimeo paused");
    }
    
    function onFinish() {
        playing = false;
        console.log("vimeo finish");
        slide('left');
    }
    
    function onPlayProgress(data) {
        playing = true;
        console.log("vimeo play progress");
    }
    

    【讨论】:

    • @APAD1 如果它回答了您的问题,请将此答案标记为已接受,否则请就未解决的问题提供反馈。谢谢
    • 嗨特雷弗,不幸的是,这个问题现在已经被搁置了,因为已经要求进行更重要的更改,不过我一定会回复你的。谢谢!
    • 嘿特雷弗,今天预留了一些时间再次研究这个问题,虽然我没有使用布尔变量,但您的建议使我找到了解决方案(我将使用代码更新我的原始帖子) .非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 2011-05-18
    • 2022-11-29
    • 1970-01-01
    相关资源
    最近更新 更多