【问题标题】:Change background color to video on mouse hover鼠标悬停时将背景颜色更改为视频
【发布时间】:2018-03-27 06:35:01
【问题描述】:

我是视频背景的新手。

我的问题是,当我将鼠标悬停在 youtube 的每一侧时,我可以将每一侧(右侧或左侧)的颜色背景更改为来自 youtube 的视频吗?

所以实际上,当我将鼠标悬停在左侧时..橙色背景将被替换为特定的 youtube 视频,而当我将鼠标悬停在右侧(红色背景)时,它将播放视频(不同于左侧)作为背景.

我已经在网上搜索了几个小时,但我就是找不到合适的解决方案。

这是我的代码:

.split {
    height: 100%;
    width: 50%;
    position: fixed;
    z-index: 1;
    top: 0;
    overflow-x: hidden;
    padding-top: 20px;
}

.left {
    left: 0;
    background-color: #E9D94C;
}

.right {
    right: 0;
    background-color: #EA2029;
}

.centered {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
}

.centered img {
    width: 100px;
    border-radius: 50%;
}
<div class="container col-md-10">
  <div class="top-intro" style="position: relative;padding:20px;height:auto;margin:0 auto;font-family: arial;font-size: 12px;z-index: 99999;font-size: 15px;color: #000;text-align: center;border-radius: 10px;top:25px;background:rgba(255,255,255,0.5);">
    Lorem ipsum sit amet dolor
  </div>
</div>
<div class="container-fluid">
  <div class="split left">
    <div class="centered">
      <img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar woman">
      <h2>Jane Flex</h2>
      <p>Some text.</p>
    </div>
  </div>

  <div class="split right">
    <div class="centered">
      <img src="https://www.w3schools.com/howto/img_avatar2.png" alt="Avatar man">
      <h2>John Doe</h2>
      <p>Some text here too.</p>
    </div>
  </div>
</div>

这是我的完整代码:http://jsfiddle.net/xkobLdv9/4/

或者我需要一些 javascript 的东西来做到这一点?如果有人能提供解决方案将不胜感激,因为我不懂javascript。

谢谢大家,

【问题讨论】:

  • 是的,这是可能的,但你需要 javascript(甚至更好:JQuery)来实现它
  • 很遗憾,我两个都不懂:(你能帮我吗@TimGerhard先生?
  • 我只使用 css 发布了答案
  • @webBer 我不懂 jquery 编码先生,但如果你可以在我以前的 jsfiddle 上添加/修改 jquery 代码,那将非常有帮助。问候,
  • 嗨@icaks,我注意到您的问题仍然没有明确的解决方案。这是因为您只是忘记了,还是您的问题仍未得到解答?我很乐意为您的案例提供进一步帮助,否则请将此问题标记为已解决,因为它可以帮助其他用户在 stackoverflow 上获得更好的体验。

标签: javascript html css html5-video mouseover


【解决方案1】:

我给你做了一个小例子:希望这会有所帮助: https://jsfiddle.net/pkbxupxc/4/

(不知何故,视频无法在 stackoverflow 中播放(但它适用于 jsfiddle)。

基本上我所做的只是将 iframe 放入 div 中,为其设置 100% 的宽度和高度,然后将其设置为 display:block on hover 和 display:none 作为默认值。

.hoverMe {
  background: coral;
  height: 400px;
  width: 100%;
  position: relative;
}

.hoverMe iframe {
  padding: 0;
}

.hidden {
  display: none;
}

.hoverMe:hover iframe.hidden {
  display: block;
}

.foreground {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba (255, 255, 255, 0.7);
}
<div class="hoverMe">
  <iframe class="hidden" width="100%" height="100%" src="https://www.youtube.com/embed/avmjunRX_xo" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  <div class="foreground">
    <h1>
      Hello World
    </h1>
  </div>
</div>

【讨论】:

  • 谢谢先生...但我有一个问题,allow="autoplay" 假设我们将鼠标悬停时自动播放视频对吗?但是为什么您的 vid 示例不自动播放?
  • 其实我也不知道。我刚刚在 youtube 上点击了分享,这是他们给你的默认嵌入 iframe
  • 好的,先生,我会尝试找到解决自动播放问题的方法。许多问候,
  • 不客气。您还可以使用 html
【解决方案2】:

这对你有用:

下面的 sn-p 视频无法在 StackOverflow 中播放,但在 fiddle 和其他页面中运行良好:

.split {
  height: 100%;
  width: 50%;
  position: fixed;
  z-index: 1;
  top: 0;
  overflow-x: hidden;
  padding-top: 20px;
}

.left_video,
.right_video {
  height: 100%;
  width: 50%;
  position: fixed;
  z-index: 0;
  top: 0;
  left: 0;
  /* overflow-x: hidden; */
  padding: 0px;
  display: none;
  pointer-events:none;
}

.right_video {
  left: auto;
  right: 0px;
}

.left {
  left: 0;
  background-color: #E9D94C;
}

.right {
  right: 0;
  background-color: #EA2029;
}

.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.centered img {
  width: 100px;
  border-radius: 50%;
}

.split:hover+.left_video,
.split:hover+.right_video {
  display: inline-block;
}

.split:hover {
  background-color: transparent;
}
<div class="container col-md-10">
  <div class="top-intro" style="position: relative;padding:20px;height:auto;margin:0 auto;font-family: arial;font-size: 12px;z-index: 99999;font-size: 15px;color: #000;text-align: center;border-radius: 10px;top:25px;background:rgba(255,255,255,0.5);">
    Lorem ipsum sit amet dolor
  </div>
</div>
<div class="container-fluid">
  <div class="split left">
    <div class="centered">
      <img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar woman">
      <h2>Jane Flex</h2>
      <p>Some text.</p>
    </div>
  </div>
  <iframe class="left_video" src="https://www.youtube.com/embed/J5OSRpRyl6g?autoplay=1" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  <div class="split right">
    <div class="centered">
      <img src="https://www.w3schools.com/howto/img_avatar2.png" alt="Avatar man">
      <h2>John Doe</h2>
      <p>Some text here too.</p>
    </div>
  </div>
  <iframe class="right_video" src="https://www.youtube.com/embed/NuIAYHVeFYs?autoplay=1" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>

播放视频的有效小提琴:fiddle

为了自动播放 iframe 视频,我使用了:?autoplay=1 在 youtube 网址的最后一个

例如:https://www.youtube.com/embed/J5OSRpRyl6g?autoplay=1

希望这对您有所帮助。

【讨论】:

  • 哇,先生..你让我很开心,非常感谢先生!如果我以后问你,我希望你不会感到不安。祝你有美好的一天先生:)
  • @icaks 很高兴我能帮助你:),顺便说一句,这里的任何人都不需要打电话给 Sir.. 都是学生.. :)
  • @icaks 如果我的代码对您有用或任何其他答案,请将其标记为答案。
【解决方案3】:

我也想亲自尝试一下,所以我已经使用 JQuery 创建了您所需要的内容。这是指向它的链接JSFIddle。只需将鼠标悬停在角色的脸上即可。

<div class="container col-md-10">
  <div class="top-intro" style="position: relative;padding:20px;height:auto;margin:0 auto;font-family: arial;font-size: 12px;z-index: 99999;font-size: 15px;color: #000;text-align: center;border-radius: 10px;top:25px;background:rgba(255,255,255,0.5);">
    Lorem ipsum sit amet dolor
  </div>
</div>
<div class="container-fluid">
  <div class="split left">
    <div class="centered">
        <iframe width="640" height="360" src="https://www.youtube.com/embed/N7ZmPYaXoic" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
      <img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar woman">
      <h2>Jane Flex</h2>
      <p>Some text.</p>
    </div>
  </div>

  <div class="split right">
    <div class="centered">
         <iframe width="640" height="360" src="https://www.youtube.com/embed/cKhVupvyhKk" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
      <img src="https://www.w3schools.com/howto/img_avatar2.png" alt="Avatar man">
      <h2>John Doe</h2>
      <p>Some text here too.</p>
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
.split {
    height: 100%;
    width: 50%;
    position: fixed;
    z-index: 1;
    top: 0;
    overflow-x: hidden;
    padding-top: 20px;
}

.left {
    left: 0;
    background-color: #E9D94C;
}

.right {
    right: 0;
    background-color: #EA2029;
}

.centered {
    position: absolute;
    top: 50%;
    left: 0;
    text-align: center;
    right: 0;
}

.centered img {
    width: 100px;
    border-radius: 50%;
}

.centered iframe {
     display: none;
    /* position: absolute; */
    width: 100%;
    /* left: 0; */
    /* right: 0; */
    height: auto;
}
</style>
<script>
$('.split').on('mouseover', function() {
 $(this).find('img').hide();
    $(this).find('iframe').show();
});

$('.split').on('mouseout', function() {
    $(this).find('iframe').hide();
    $(this).find('img').show();
});
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-07
    • 2010-12-25
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    相关资源
    最近更新 更多