You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

仿YouTube视频全屏后可滚动布局实现问题咨询

问题解答

1. 全屏元素选择

要实现YouTube那种全屏后仍能滚动到下方内容的效果,必须把包含视频和下方内容的.wrapper设为全屏元素。如果用document.documentElement(整个页面),全屏后虽然也能滚动,但不符合你要的“视频区域全屏后仍能访问下方内容”的布局逻辑——.wrapper是承载所有内容的容器,只有它进入全屏,内部的视频和文本内容才会被完整纳入全屏视图,才能实现滚动浏览。

2. 滚动事件绑定

  • 进入全屏状态后,需要给全屏状态下的.wrapper元素绑定滚轮事件(全屏模式下浏览器默认会拦截window的滚动事件,需手动处理)。
  • 要配合监听全屏状态变化,动态绑定/解绑事件,避免非全屏状态下干扰正常滚动逻辑。

修改后的完整代码

HTML

<div class="wrapper">
  <div class="video-wrap">
    <video class="video" src="http://download.blender.org/peach/trailer/trailer_480p.mov" controls></video>
    <button class="fs-button">toggle fullscreen</button>
  </div>
  <div class="content">
    Why do we use it?
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

    Where does it come from?
    Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

    The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.

    Where can I get some?
    There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
  </div>
</div>

CSS

.video {
  width: 100%;
}

.content {
  margin-top: 50px;
  padding: 0 20px;
}

/* 全屏状态下确保wrapper可滚动,兼容多浏览器 */
.wrapper:fullscreen,
.wrapper:-webkit-full-screen,
.wrapper:-moz-full-screen,
.wrapper:-ms-fullscreen {
  overflow-y: auto;
  height: 100vh;
}

JavaScript

const fsButton = document.querySelector('.fs-button');
const wrapper = document.querySelector('.wrapper');

// 切换全屏逻辑
fsButton.addEventListener('click', function() {
  if (document.fullscreenElement) {
    document.exitFullscreen();
  } else {
    // 兼容不同浏览器的全屏API
    if (wrapper.requestFullscreen) {
      wrapper.requestFullscreen();
    } else if (wrapper.webkitRequestFullscreen) {
      wrapper.webkitRequestFullscreen();
    } else if (wrapper.mozRequestFullScreen) {
      wrapper.mozRequestFullScreen();
    } else if (wrapper.msRequestFullscreen) {
      wrapper.msRequestFullscreen();
    }
  }
});

// 监听全屏状态变化,动态绑定滚轮事件
document.addEventListener('fullscreenchange', function() {
  if (document.fullscreenElement === wrapper) {
    wrapper.addEventListener('wheel', handleWheelScroll, { passive: false });
  } else {
    wrapper.removeEventListener('wheel', handleWheelScroll);
  }
});

// 手动处理全屏下的滚动
function handleWheelScroll(e) {
  e.preventDefault();
  wrapper.scrollTop += e.deltaY;
}

内容的提问来源于stack exchange,提问作者Toniq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 03:57:12