css - 如何在 div 悬停时隐藏视频?
问题描述
我试图在 div 悬停时隐藏视频,但我似乎无法让它工作
目前,“live_video”类位于顶部,“eat_video”位于下方。当“video_hover”类悬停时,我想隐藏“live_video”的显示
我想要实现的是堆叠 2 个全屏视频,但是当您将鼠标悬停在浏览器窗口的右侧 50% 时,它会隐藏顶部视频并显示下方的视频
为什么 .right_hover:hover .live_video {display: none;} 不起作用?
<div class="live_video">
<video muted class="video" autoplay="autoplay" loop="loop" preload="auto">
<source src="NM_Web_Live_Vid_v1_1_1.mp4" type="video/mp4" >
</video>
</div>
<div class="eat_video">
<video muted class="video" autoplay="autoplay" loop="loop" preload="auto">
<source src="NM_Web_Live_EatPlay_v1_1_1.mp4" type="video/mp4" >
</video>
</div>
CSS
.video_hover {
width: 50%;
height: 100vh;
position: absolute;
right: 0;
top: 0;
z-index: 99;
cursor: pointer;
}
.eat_video {
position: absolute;
width: 100%;
height: 100vh;
top: 0;
z-index: -1;
}
.video_hover:hover .live_video {
display: none;
}
解决方案
要.video_hover:hover .live_video
生效,有一些基本要求 - 主要要求是文档中存在具有 class video_hover
(和 child )的元素。live_video
为了实现您想要做的事情,您可以将以下更改应用到您的 CSS/HTML:
/* Style video containers to occupy full client area of browser */
.video_hover {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
cursor: pointer;
}
.video_hover video {
display:block;
width: 100%;
height: 100%;
}
/* Define (hidden) pseudo element that will catch hover interactions
to control the visiblity of respective video elements */
.video_hover::before {
content:"";
display:block;
width:50%;
height:100%;
position: absolute;
top: 0;
z-index:100;
}
/* Specify placement of each pseudo element to occupy each side of the
client area */
.live_video::before {
right: 0;
}
.eat_video::before {
left: 0;
}
/* Eat video hidden when hovering not over right half of screen */
.eat_video video {
visibility:hidden;
}
/* When live video (or it's pseudo element) is hovered, "hide" the
video */
.live_video:hover video {
visibility:hidden;
}
/* When live video (or it's pseudo element) is hovered, "show" the
next eact_video video */
.live_video:hover + .eat_video video {
visibility:visible;
}
<!-- Add video_hover to class list -->
<div class="live_video video_hover">
<video muted class="video" autoplay="autoplay" loop="loop" preload="auto">
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" >
</video>
</div>
<!-- Add video_hover to class list -->
<div class="eat_video video_hover">
<video muted class="video" autoplay="autoplay" loop="loop" preload="auto">
<source src="https://www.fbdemo.com/video/video/demo.mp4" type="video/mp4" >
</video>
</div>
推荐阅读
- javascript - Google Analytics Real-Time API 批处理调用 - 返回数据的顺序
- javascript - (业余问题)需要创建一个函数来减少重复代码
- typescript - 如何使用类类型作为类型化变量?(打字稿)
- linux - 找不到-lcrypto
- linux - USB在磁盘中可见但无法打开
- go - 将结构转换为切片结构
- c# - 如何在通过存储过程插入数据时捕获和处理重复错误
- php - 如何通过 PHP 的期望结果合并两个数组?
- swift - 将目标添加到单元格但让局部变量迅速产生问题
- javascript - 检查 promises.push 之后是否发布返回值