【问题标题】:Youtube Video Overlay?Youtube视频叠加?
【发布时间】:2017-04-23 14:35:22
【问题描述】:

所以我一直试图弄清楚当您单击播放链接时如何用 youtube 视频覆盖英雄图像(这样视频会填充 div 的高度和宽度)。但我不知道该怎么做。我尝试过使用弹出插件,但我认为它们不是解决方案。我正在努力完成这些人在他们的网站上所做的事情:https://www.squarespace.com/ 但因为我是菜鸟,所以我无法阅读他们的所作所为。

我该怎么办?用 div 覆盖 youtube 视频,或者?我也需要 jQuery 吗?有人,帮忙!

谢谢!

编辑: 我试过使用 Magnific Popup。但我无法让它工作。无论如何,这是我尝试过的:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/magnific-popup.css">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>

<a class="video" href="https://www.youtube.com/watch?v=LGZaWik2430">Click To View</a>

$('.video').magnificPopup({
  type: 'iframe',


  iframe: {
     markup: '<div class="mfp-iframe-scaler">'+
                '<div class="mfp-close"></div>'+
                '<iframe class="mfp-iframe" frameborder="0" allowfullscreen></iframe>' +
              '</div>'
  },
  callbacks: {
    markupParse: function(template, values, item) {
     values.title = item.el.attr('title');
    }
  }


});

【问题讨论】:

  • 你试过什么代码?编辑您的问题并将其粘贴到那里。这不是一个免费的代码编写网站,你至少要尝试一下
  • 会的,请稍等。虽然,他们都没有工作。大声笑

标签: javascript jquery html css youtube


【解决方案1】:

首先,视频的位置、尺寸,甚至悬停动画和背景图像、叠加按钮等都可以使用 CSS 完成。我认为不需要为此使用 javascript。

至于视频播放,也可以使用 CSS 适配背景图片的大小,可以根据需要设置播放选项。

【讨论】:

  • 确实如此。您不需要javascript,但我的答案使用JS打开弹出窗口
【解决方案2】:

您可以使用W3 Schools 将视频插入到模态中。您可以使用图像而不是模式按钮。这是我得到的:

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
    background-color: #fefefe;
    margin: auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
}

/* The Close Button */
.close {
    color: #aaaaaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}
<h2>Modal Example</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn"><img src="IMAGE URL" height="50px" width="50px" alt="YOUR IMAGE"></button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Your Video Here</p>
  </div>

</div>

【讨论】:

  • @Retros Upvote 并将我的答案标记为您认为合适的答案。 :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-06
相关资源
最近更新 更多