在.click上加载功能

时间:2018-09-08 07:02:25

标签: jquery function plyr.js

我有一个问题要弄清楚我需要在哪里放置resizeBg();使其起作用。 例如,当用户单击按钮(btnNext时,它将调用loadVideo,并且在事件就绪时,需要再次调用resizeBg()。但是控制台什么都不给我

和代码示例:

var instance = Plyr.setup('.js-player', {
        controls: [],
        loop: { active: true },
        debug: false,
        autoplay: true,
        clickToPlay: false
    });
    var player = instance[0],
    index = 0,
    videoCount = video_list.length;
    player.source = {
        type: 'video',
        sources: [
          {
            src: video_list[index].src,
            provider: 'vimeo',
          }
        ]
    },
    video_title = $('.video-title').text(video_list[index].title),
    player.on('ready', function () {
        player.volume = 0;
        resizeBg();
    },
    btnPrev = $('.home-video-direction .prev').click(function (event) {
        event.preventDefault();
        if ((index - 1) > -1) {
            index--;
            loadVideo(index);
        } else {
            index = videoCount - 1;
            loadVideo(index);
        }
    }),
    btnNext = $('.home-video-direction .next').click(function (event) {
        event.preventDefault();
        if ((index + 1) < videoCount) {
            index++;
            loadVideo(index);
        } else {
            index = 0;
            loadVideo(index);
        }

    }),
    loadVideo = function (id) {
        player.source = {
            type: 'video',
            sources: [
              {
                src: video_list[id].src,
                provider: 'vimeo',
              }
            ]
        },
        video_title = $('.video-title').text(video_list[id].title),
        index = id;
        Plyr.setup();
        player.on('ready', function () {
            player.volume = 0;
            resizeBg();
        })
    });

}

var theWindow        = $(window),
    $bg              = $(".video-intro .plyr__video-wrapper"),
    aspectRatio      = 1.77777777778;

function resizeBg() {
    var w_H = theWindow.height(),
            w_W = theWindow.width(),
            sum = w_W / w_H;
    if ( (w_W / w_H) < aspectRatio ) {
        $bg
            .removeClass('bgwidth')
            .addClass('bgheight')
            .css({
                    width: (w_H * aspectRatio),
                    left: (w_W - (w_H * aspectRatio)) / 2
                });
    } else {
        $bg.removeClass('bgheight').addClass('bgwidth');
    }



theWindow.resize(resizeBg).trigger("resize");

1 个答案:

答案 0 :(得分:0)

我发现我犯了一个错误。基本上是DOM。我将这些变量从function

中删除
var theWindow        = $(window),
    $bg              = $(".video-intro .plyr__video-wrapper"),
    aspectRatio      = 1.77777777778;

因为它们附加在上一个DOM元素上。 $bg存在,但指向在.click上加载视频数据后插件添加的新DOM元素并不重要

所以这个功能应该看起来像这样

function resizeBg() {
        var theWindow        = $(window),
            $bg              = $(".video-intro .plyr__video-wrapper"),
            aspectRatio      = 1.77777777778,
            w_H = theWindow.height(),
            w_W = theWindow.width(),
            sum = w_W / w_H;

        if ( (w_W / w_H) < aspectRatio ) {
            $bg
                .removeClass('bgwidth')
                .addClass('bgheight')
                .css({
                        width: (w_H * aspectRatio),
                        left: (w_W - (w_H * aspectRatio)) / 2
                    });
        } else {
            $bg.removeClass('bgheight').addClass('bgwidth');
        }