【问题标题】:kentico youtube webpart used within slick.jsslick.js 中使用的 kentico youtube webpart
【发布时间】:2016-07-28 12:05:17
【问题描述】:

我有一个中继器,它为幻灯片放映插件 slick.js 输出面板。到目前为止,事情正在按计划进行。

用户在创建自定义页面时输入副本,然后输入图像、媒体库中的视频或指向您的电子管的链接。

我要做的是编写 JS 函数,当用户在 youtube 视频上单击播放时将触发该函数。

webpart 通过 iFrame 方法注入 youtube 视频。

这是我的转变:

<section class="slide">
    <div class="copy">
        <%# Eval("SlideContent") %>
    </div>
    <asp:PlaceHolder runat='server' id='slideImage' visible='<%# IfEmpty( Eval("SlideImage"), false, true )  %>'>
        <div class="img">
            <img class="img-responsive" src="<%# Eval(" SlideImage ") %>" alt="<%# Eval(" SlideContent ") %>">
        </div>
    </asp:PlaceHolder>
    <asp:PlaceHolder runat='server' id='slideVideo' visible='<%# IfEmpty( Eval("SlideVideo"), false, true )  %>'>
        <div class='videoHolder html5'>
            <video id='video' class='html5Video' controls>
                <source src='<%# Eval("SlideVideo") %>'>
            </video>
        </div>
    </asp:PlaceHolder>
    <asp:PlaceHolder runat='server' id='youTubeVideo' visible='<%# IfEmpty( Eval("YouTubeVideo"), false, true )  %>'>
        <%@ Register Src="~/CMSWebParts/Media/YouTubeVideo.ascx" TagName="YoutubeVideo" TagPrefix="webPart" %>
            <div class='videoHolder yt'>
                <webPart:YoutubeVideo runat="server" id="YouTubeVideoWebpart" CssClass="ytVideo" VideoURL='<%# ResolveMacros(Eval("YouTubeVideo").ToString())%>' FullScreen='true' />
            </div>
    </asp:PlaceHolder>
</section>

这是我的 JS(这还包括在滑块更改时暂停视频的代码)

$(function () {
  'use strict';
  var $slider = $('.slider'),
    $slickJS = '/kffIntranet/ui/bower_components/slick-carousel/slick/slick.min.js';

  // we check for a slider on the page
  if ($slider.length !== 0) {

    // if there is a slider, we load the slick.js plugin
    $.getScript($slickJS, function () {
      // init  the slider
      $slider.slick({
        dots: true,
        infinite: true,
        speed: 300,
        slidesToShow: 1,
        slidesToScroll: 1,
        fade: false,
        lazyLoad: 'ondemand',
        adaptiveHeight: true,
        autoplay: true,
        autoplaySpeed: 5000,
        responsive: [{
            breakpoint: 1024,
            settings: {}
          }, {
            breakpoint: 600,
            settings: {}
          }, {
            breakpoint: 480,
            settings: {
              arrows: false
            }
          }
          // You can unslick at a given breakpoint now by adding:
          // settings: "unslick"
          // instead of a settings object
        ]
      });
    });

    //

    // video control. If a slide has video, we need to pause

    //bind our event here, it gets the current slide and pauses the video before each slide changes.
    $slider.on('beforeChange', function (event, slick) {
      var currentSlide, player, command, videoType;

      //find the current slide element and decide which player API we need to use.
      currentSlide = $(slick.$slider).find('.slick-current');

      //determine which type of slide this by looking for the video holder than getting the video type class
      if (currentSlide.find('.videoHolder').length) {
        videoType = $('.videoHolder', currentSlide).attr('class').split(' ')[1];
        //get the iframe inside this slide.
        player = currentSlide.find('iframe').get(0);
      }
      // pause videos
      if (videoType === 'yt') {
        command = {
          'event': 'command',
          'func': 'pauseVideo'
        };
        player.contentWindow.postMessage(JSON.stringify(command), '*');
      } else if (videoType === 'html5') {
        document.getElementById('video').pause();
      }

    });

    // pause slider if a video is playing
    // html 5 video click
    $('.html5Video').on('click', function () {
      var $video = $(this).get(0);
      // control pause play state of video
      if ($video.paused) {
        $video.play();
      } else {
        $video.pause();
      }
      // call slide pause function
      pauseSlide();
    });
    // youtube play
    $('.ytVideo iframe').on('click', function () {
      // call slide pause function
      pauseSlide();
    });
  }

  // puse slider function
  function pauseSlide() {
    $slider.slick('slickPause');
    console.log('pause');
  }
});

所以我创建了一个暂停滑块的函数 pauseSlide,但我正在努力捕捉 youtube 播放点击。

【问题讨论】:

    标签: kentico slick.js youtube-iframe-api


    【解决方案1】:

    Check this answer out。它建议使用 YouTube iframe API。

    <!DOCTYPE html> <html> <head> <script src="https://www.youtube.com/iframe_api"></script> </head> <body> <div id='vidWrapper'> <!-- The <iframe> (and video player) will replace this <div> tag. --> <div id="ytplayer"></div> </div> <script> var player; function onYouTubeIframeAPIReady() { player = new YT.Player('ytplayer', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', events: { 'onStateChange': function(event) { if (event.data == YT.PlayerState.PLAYING) { pauseAudio(); } } } }); } function pauseAudio() { ... } </script> </body> </html>
    

    【讨论】:

    • 我一直在看那个。不使用 Kentico webpart,而是使用纯 JS。仅输入视频 ID,将其作为数据属性添加到转换中的 div 上,然后输入 JS。
    • 是的,我强烈建议不要在 webpart 中嵌入 webpart。 webpart 中的控件很好,但通常不是 webpart。 iframe 的纯 html 应该没问题
    • 或者他们真的不需要滑块中的 youtube 视频....大声笑...好的,谢谢。我会让你知道这是怎么回事。
    • 我们在光滑的滑块出现之前使用Repeater transformation + JS创建了这个组件。它真的很好用。如果您需要有关代码的任何帮助,请告诉我?
    • 谢谢切坦。我让它工作,只是一些 kenitco 表单验证和跨浏览器检查。干杯!
    【解决方案2】:

    好的,所以我退后了一步。转换现在寻找一个 YouTube ID,然后我有一个 jQuery 插件接管。

    所以滑块会自动播放,如果开始播放视频(youtube 或 mp4),滑块就会停止。此外,如果单击任一滑块方向箭头或其中一个点,视频将暂停。

    我仍然需要重构并添加一些逻辑,但它正在工作。

    这是更新后的转换:

    <section class="slide">
        <div class="copy">
            <%# Eval("SlideContent") %>
        </div>
        <asp:PlaceHolder runat='server' id='slideImage' visible='<%# IfEmpty( Eval("SlideImage"), false, true )  %>'>
            <div class="img">
                <img class="img-responsive" src="<%# Eval(" SlideImage ") %>" alt="<%# Eval(" SlideContent ") %>">
            </div>
        </asp:PlaceHolder>
        <asp:PlaceHolder runat='server' id='slideVideo' visible='<%# IfEmpty( Eval("SlideVideo"), false, true )  %>'>
            <div class='videoHolder html5'>
                <video id='video' class='html5Video' controls>
                    <source src='<%# Eval("SlideVideo") %>'>
                </video>
            </div>
        </asp:PlaceHolder>
        <asp:PlaceHolder runat='server' id='youTubeVideoID' visible='<%# IfEmpty( Eval("YouTubeVideoID"), false, true )  %>'>      
          <div class='videoHolder yt' data-vID='<%# Eval("YouTubeVideoID") %>'>
            <div class="vh"></div>
          </div>      
        </asp:PlaceHolder>
    </section>

    我的 video.js 文件

    $(function () {
      'use strict';
    
      var vHolder = $('.videoHolder'),
        vtPlugin = '/kffIntranet/ui/scripts/plugins/tubePlayer/jQuery.tubeplayer.min.js',
        vID;
    
      // check for a youtube video, and if there is one, load the youtube pluging
      if ($('.yt').length) {
        // load plugin
        $.getScript(vtPlugin, function () {
          // once plugin is loaded, loop through each YT and call the plugin
          $('.yt').each(function (i, v) {
            var $this = $(this);
            vID = $this.attr('data-vid');
    
            $('.vh', $this).tubeplayer({
              initialVideo: vID,
              onPlayerPlaying: function () {
                $('.slider').slick('slickPause');
              }
            });
          });
        });
      } else if ($('.html5').length) {
        $('.html5 .html5video').bind('pause', function () {
          $('.slider').slick('slickPause');
        });
      }
    
    });

    还有我的滑块 js 文件

    $(function () {
      'use strict';
      var $slider = $('.slider'),
        $slickJS = '/kffIntranet/ui/bower_components/slick-carousel/slick/slick.min.js',
        videoType;
    
      // we check for a slider on the page
      if ($slider.length !== 0) {
    
        // if there is a slider, we load the slick.js plugin
        $.getScript($slickJS, function () {
          // init  the slider
          $slider.slick({
            dots: true,
            infinite: true,
            speed: 300,
            slidesToShow: 1,
            slidesToScroll: 1,
            fade: false,
            lazyLoad: 'ondemand',
            adaptiveHeight: true,
            autoplay: true,
            autoplaySpeed: 5000,
            responsive: [{
                breakpoint: 1024,
                settings: {}
              }, {
                breakpoint: 600,
                settings: {}
              }, {
                breakpoint: 480,
                settings: {
                  arrows: false
                }
              }
              // You can unslick at a given breakpoint now by adding:
              // settings: "unslick"
              // instead of a settings object
            ]
          });
        });
        // on slide change
        $slider.on('beforeChange', function (event, slick) {
          var currentSlide;
          //find the current slide element and decide which player API we need to use.
          currentSlide = $(slick.$slider).find('.slick-current');
          //determine which type of slide this by looking for the video holder than getting the video type class
          if (currentSlide.find('.videoHolder').length) {
            videoType = $('.videoHolder', currentSlide).attr('class').split(' ')[1];
          }
          // pause videos
          if (videoType === 'html5') {
            document.getElementById('video').pause();
          } else if (videoType === 'yt') {
            $('.vh', currentSlide).tubeplayer('pause');
          }
        });
      }
    });

    【讨论】:

      猜你喜欢
      • 2016-10-18
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 2012-06-26
      • 2018-03-27
      • 2016-07-03
      相关资源
      最近更新 更多