利用 Javascript 让 DIV 自适应屏幕的分辨率,从而决定是否显示滚动条
直接贴代码了:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="resources/scripts/jquery-1.10.1.min.js"></script>
</head>
<body>
<div class="core-title">
<h2>这里是标题</h2>
</div>
<div id="coreComparisonReportDiv" style="overflow-y:scroll; ">
<div class="tableShow">
<table style="width:5000px;">
<thead>
<tr>
<th>Remark</th>
<th>Data Set</th>
<!-- 假设这里还有很多列 -->
</tr>
</thead>
<tbody>
<tr style="">
<td style="width:120px; ">No Change</td>
<td style="width:250px; ">2019-03-15</td>
<!-- 假设这里还有很多列 -->
</tr>
<!-- 假设这里还有很多行 -->
</tbody>
</table>
</div>
</div>
</body>
</html>
下面是 Javascript 脚本:
<script type="text/javascript">
$(document).ready(function () {
calcCoreComparisonReportDivHeight();
});
function getTableSuggestHeight(id, errorHeightValue) {
var jTable = $('#' + id);
if (jTable.length == 0) {
return;
}
var windowHeight = $(window).height();
var jTableOffsetTop = jTable.offset().top;
var iframeOffsetTop = 0;
if (window.parent != null) {
var appDivObj = window.parent.document.getElementById("app");
if (appDivObj) {
var appDivObjOffsetTop = appDivObj.offsetTop;
iframeOffsetTop = appDivObjOffsetTop;
}
}
var tempErrorHeightValue = 10;
if (errorHeightValue) {
tempErrorHeightValue = errorHeightValue;
}
var suggestHeight = windowHeight - jTableOffsetTop - iframeOffsetTop - tempErrorHeightValue; // 10 为误差
if (suggestHeight < 200) {
suggestHeight = 200; // 为了兼容 1024*768 的显示屏,最小高度 200
}
return suggestHeight;
}
function calcCoreComparisonReportDivHeight() {
var coreComparisonReportDivId = "coreComparisonReportDiv";
var coreComparisonReportDivHeight = getTableSuggestHeight(coreComparisonReportDivId);
$("#" + coreComparisonReportDivId).height(coreComparisonReportDivHeight);
}
$(window).resize(function () {
calcCoreComparisonReportDivHeight();
});
</script>
运行效果:

谢谢浏览!
利用 Javascript 让 DIV 自适应屏幕的分辨率,从而决定是否显示滚动条的更多相关文章
- Div 自适应屏幕大小
http://blog.csdn.net/wodetiankong516/article/details/7827256 Background 有时, 我们需要将div或者其他的Elemen ...
- 怎么可以让div自适应屏幕的高度?(已解决)
主要解决问题的方法是用JS脚本. 先看布局, 一个div是首部,另一个div是主体,主体包含左侧菜单和右侧内容. 我想把主体div的高度自适应屏幕剩余区域,怎么做? 首先,获取可见区域的高度,docu ...
- 限制div高度当内容多了溢出时显示滚动条
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type"content= ...
- Android 中Webview 自适应屏幕
随笔 - 478 文章 - 3 评论 - 113 Android 中Webview 自适应屏幕 webview中右下角的缩放按钮能不能去掉 settings.setDisplayZoomCon ...
- [转载]Android中WebView自适应屏幕
webview中右下角的缩放按钮能不能去掉 settings.setDisplayZoomControls(false); //隐藏webview缩放按钮 让Webview加载的页面居中显示有我知道的 ...
- 利用onresize使得div可以随着屏幕大小而自适应的代码
原文:http://www.jb51.net/article/21831.htm 当我们让div居中时候,一般有两种方法,一种是固定左右宽度,也就是使用像素绝对定位:另一种是用百分比来相对定位,在这种 ...
- #region 自适应屏幕分辨率
#region 自适应屏幕分辨率 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public ...
- Unity3D NGUI自适应屏幕分辨率(2014/4/17更新)
原地址:http://blog.csdn.net/asd237241291/article/details/8126619 原创文章如需转载请注明:转载自 脱莫柔Unity3D学习之旅 本文链接地址: ...
- JavaScript网站设计实践(四)编写about.html页面,利用JavaScript和DOM,选择性的显示和隐藏DIV元素
一.现在我们在网站设计(三)的基础上,来编写about.html页面. 这个页面要用到的知识点是利用JavaScript和DOM实现选择性地显示和隐藏某些DIV about.html页面在前面我们为了 ...
随机推荐
- Django JsonResponse 不自动设置 cookie 的解决方案
[背景] 目前在做一个前后端分离的 web 项目,后端使用的是 django 框架,所有的 API 都只返回 json :就在这个过程中遇到了一个问题,那就是对于所有的 JsonResponse dj ...
- C# copy files from source directory to destination file and rename repeated files and does not override
static void CopyFiles() { string sourceDir = @"D:\C\ll"; string destDir = @"D:\LL&quo ...
- python 安装impala包
一路安装就可以 .pip install six .pip install bit_array .pip install thriftpy .pip install thrift_sasl .pip ...
- PHP常量以及基本数据类型
1.常量 1.1用define()函数定义常量 define(常量名,值,[是否区别大小写]) true表示不区分大小写,默认是false 常量名前没有$符 常量名推荐使用大写 1.2.定义 ...
- ES新提案:双问号操作符
摘要: 简单实用的新特性. 原文:ES新提案:双问号操作符 译者:前端小智 本文主要讲Gabriel Isenberg撰写的ES提案"Nullish coalescing for JavaS ...
- c# 第26节 Main方法
本节内容: 1:Main方法 2:Main方法的定义 3:测试Main函数传入参数 4:Main的大总结 1:Main方法是什么 2:Main方法的定义 3:测试打印出外部传入Main的参数 clas ...
- python3.5.3rc1学习四:类
class calculator: def add(x,y): return x + y print(added) def sub(x,y): return x - y print(sub) def ...
- [C1W1] Neural Networks and Deep Learning - Introduction to Deep Learning
第一周:深度学习引言(Introduction to Deep Learning) 欢迎(Welcome) 深度学习改变了传统互联网业务,例如如网络搜索和广告.但是深度学习同时也使得许多新产品和企业以 ...
- 【Spring AOP】AOP实现原理(六)
原文链接:https://my.oschina.net/guangshan/blog/1797461
- SQL Server 修改数据库
1. 可视化界面修改数据库 (1)右击数据库,然后选择属性. (2)在工具选项卡中,选择[文件]页,可以更改所有者,文件大小,自增量等参数. 2. 使用ALTER Database修改数据库 (1) ...