代码之家  ›  专栏  ›  技术社区  ›  Frank

当我播放了2个视频并关闭iFrame后,浏览器上的“后退”按钮将播放没有视频的声音,如何修复?

  •  0
  • Frank  · 技术社区  · 8 年前

    http://gatecybertech.com/test 但稍后将移至主站点: http://gatecybertech.com

    [1] 在iFrame中再次显示视频并正常播放

    [2] 不播放声音&不显示视频

    以上两种都可以。我怎样才能做到?

    我的html如下所示:

                <div class="tools-icon">
                  <a href=https://www.youtube.com/embed/IgBIaZgoAQc?autoplay=1 target=_blank data-target="modal" data-video-title="Keypad Pins Easily Stolen"><img src=https://i.ytimg.com/vi/IgBIaZgoAQc/hqdefault.jpg?sqp=-oaymwEXCPYBEIoBSFryq4qpAwkIARUAAIhCGAE=&rs=AOn4CLDW3KcjXsTR5utmlvhFfibLe-bvRg width=170 height=110></a>
                  <p class="tools-icon__text">Keypad Pins Easily Stolen</p>
                </div>
    

    我是。。。

      <!-- the modal div that will open when an anchor link is clicked to show the related video in an iframe. -->
    
        <div id="modal" tabindex="-1">
          <div class="content">
            <h4 class="title"></h4>
            <iframe class="yt-video" src="https://www.youtube.com/embed/A1nRiiWYgZw" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
          </div>
    
          <div class="black_overlay" onclick="closeLightBox()" style="display: block;">
            <div style=" z-index: 0; left: 76%; top: 17%; width: 22px; position: absolute;">
              <a class="close" onclick = "return close_iFrame();"><h2>&times;</h2></a>
            </div>
          </div>
        </div>
    

    脚本如下:

    <script>
      var modal = document.getElementById('modal'), 
          closeBtn = modal.querySelector('close'), 
          ytVideo = modal.querySelector('.content .yt-video'), 
          title = modal.querySelector('.content .title'), 
          anchors = document.querySelectorAll('a[data-target="modal"]'), 
          l = anchors.length;
    
      for (var i = 0; i < l; i++)
      {
        anchors[i].addEventListener("click", function(e)
        {
          e.preventDefault();
          title.textContent = this.dataset.videoTitle || 'No title';
          ytVideo.src = this.href;
          modal.classList.toggle('is-visible');
          modal.focus();
        });
      }
    
      modal.addEventListener("keydown", function(e)
      {
        if (e.keyCode == 27)
        {
          title.textContent = '';
          ytVideo.src = '';
          this.classList.toggle('is-visible');
        }
      });
    </script>
    
    <script language="javascript" type="text/javascript">
      function close_iFrame()
      {
        var modal = document.getElementById('modal'), 
                    ytVideo = modal.querySelector('.content .yt-video'); 
                    ytVideo.src = '';
                    modal.classList.toggle('is-visible');
      }
    </script>
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Frank    8 年前

    好吧,我想好了,只要做以下改变:

    <script language="javascript" type="text/javascript">
      function close_iFrame()
      {
        var modal = document.getElementById('modal'), 
                    ytVideo = modal.querySelector('.content .yt-video'); 
                    ytVideo.src = '';
                    modal.classList.toggle('is-visible');
        history.go(-1);
        window.location.href = '#Videos';
      }
    </script>