javascript - 使内部 div 可滚动

标签 javascript jquery html css

当 body 设置为 overflow:hidden 时,我如何在内部 DIV 上启用滚动,因为我有限制编辑 body CSS

这是 JSfiddle demo

正文 CSS 是

body {
    margin: 0;
    padding: 0;
    background-color: #000;
    overflow: hidden;
    max-width: 100%;
    max-height: 100%;
}

内部 DIV CSS 如下 - 默认情况下,div 的高度由 jQuery 设置。

.custom-gallery-lineage {
    float: left;
    width: 100%;
    overflow: auto;
    color: #fff;
    font-size: 18px;
}

最佳答案

您必须使用 $(window).height() 而不是 $(document).height(),检查下面的代码片段或更新的 fiddle https://jsfiddle.net/sgh1gqh5/2/

$(document).ready(function() {
	var winw = $(window).width();
	var winh = $(window).height();
	$(".custom-gallery-lineage").height(winh);
});
body {
	margin: 0;
	padding: 0;
	background-color: #000;
	overflow: hidden;
	max-width: 100%;
	max-height: 100%;
}
.custom-gallery-lineage {
	float: left;
	width: 100%;
	overflow: auto;
	color: #fff;
	font-size: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="custom-gallery-lineage">
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
  <p>This is test content</p>
</div>

关于javascript - 使内部 div 可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44023412/

相关文章:

javascript - 悬停状态下的 anchor 颜色不起作用

javascript:切换一对类

javascript - 如何获取具有行跨度列的表行td值

jquery - 如何使用 jquery 的 .html() 方法将 div 的内容设置为另一个 div

html - 如何将第二列的高度调整为第一列的高度?

javascript - 让脚本在不同的浏览器上运行

asp.net - 为什么这个按钮不会导致三重回发?

javascript - getJSON 不适用于最新的 jquery,但适用于较旧的 jQuery

javascript - jquery 似乎无法在 bootstrap 4 popover 内工作

Javascript 转到 URL 如果用户输入特定数字,否则转到另一个 url