调整大小功能仅适用于一个维度
resize function only works in one dimension
我有一个调整大小的功能,可以使主div以一致的比例调整大小。我最初让它根据窗口宽度调整大小,效果很好(函数中的第一个条件)。然后我添加了第二个条件,以便在窗口太短时,它会根据高度调整大小。 此函数在加载时正常工作。
当窗口变窄或变宽时,onresize 适用于宽度,但高度条件仅在窗口变短时才有效。如果将窗口拖得更高,则 onresize 事件似乎不会触发,我必须手动重新加载页面才能调整函数的大小。
<script>
function contentResizeHeight() {
var contentBG = document.getElementById("content");
var windowHeight = window.innerHeight;
var newHeight = Math.round(contentBG.offsetWidth * .6);
if ( windowHeight > newHeight ){
contentBG.style.height = newHeight + "px";
}
if ( windowHeight < newHeight ){
var newerWidth = windowHeight * 1.666666666666666666;
var newerHeight = Math.round(newerWidth * .6);
contentBG.style.height = newerHeight + "px";
contentBG.style.width = newerWidth + "px";
}
};
</script>
#contentdiv 被背景图像覆盖。因此,我们的想法是保持图像宽高比相同。
div#content{
background-repeat:no-repeat;
background-position:center 0px;
background-size: cover;
min-width:1024px;
max-width:1600px;
min-height:614px;
max-height:960px;
margin-right:auto;
margin-left:auto;
}
我在正文标签中调用函数
<body onload="contentResizeHeight()" onresize="contentResizeHeight()">
使用背景图像实现此目标
如果您尝试使用背景图像来实现此目的,则可以使用 CSS background-size: contain;
的帮助。来自Mozilla网站的关键字contain
:
此关键字指定背景图像应缩放为尽可能大,同时确保其两个维度都小于或等于背景定位区域的相应维度。
使用此逻辑,您几乎可以根据窗口大小根据需要放大每个维度,并让CSS完成其余的工作。
function contentResizeHeight() {
var contentBG = document.getElementById("content"),
windowHeight = window.innerHeight,
windowWidth = window.innerWidth;
contentBG.style.height = windowHeight + "px";
contentBG.style.width = windowWidth + "px";
}
JSFiddle 在这里查看它的实际效果。
使用纯div/无背景图像时
对于碰巧偶然发现此答案以寻找与上述相同结果的人来说,这更多,但有一个普通的div
(可能带有背景颜色或其他东西)并且没有 CSS 的帮助:JSFiddle
function contentResizeHeight() {
var contentBG = document.getElementById("content"),
windowHeight = window.innerHeight,
windowWidth = window.innerWidth,
contentHeight = contentBG.offsetHeight,
contentWidth = contentBG.offsetWidth,
newHeight = null,
newWidth = null;
if ( windowHeight > contentHeight ){
// 40 is the buffer zone for increasing the windows width quickly
if( contentWidth < windowWidth - 40 ){
newWidth = contentHeight * 1.666666666666666666;
if( newWidth >= windowWidth - 10 ){
newHeight = Math.round(newWidth * .6);
} else {
newHeight = windowHeight;
}
} else {
newHeight = Math.round(contentWidth * .6);
newWidth = windowWidth - 4;
}
} else if ( windowHeight < contentHeight ){
newHeight = windowHeight;
newWidth = newHeight * 1.666666666666666666;
}
contentBG.style.height = newHeight + "px";
contentBG.style.width = newWidth + "px";
}
这就是我设法让它工作 95% 的方式,所以如果有人有解决窗口宽度问题的方法,我很想听听。
相关文章:
- 按下一个HTML按钮,该按钮使用一个功能在同一个新窗口中打开URL
- 使用单选按钮参考另一个功能
- 流星:移除不是一个功能
- 浏览器同步摄像头加速视频播放不是一个功能
- 谷歌应用程序脚本搜索UI下一个和上一个功能
- 如何使用上一个/下一个功能循环数组中的图像
- 是否可以设置一个功能“;联系我们”;页面使用HTML/CSS
- 只有在前一个功能成功完成的情况下,我才能正确地启动此功能
- 如果另一个功能完成,则执行该功能,而不是何时
- 单击和键控操作可以完成一个功能
- 如果选中单选按钮,如何转到另一个功能?(Javascript)
- 你能在javascript中命名一个功能点击吗?
- 使用 firebase 获取$add angular 应用程序不是一个功能
- 在按钮单击中将一个功能更改为第二个功能
- 在谷歌地图自动补充之后启动一个功能
- 单击圆圈时启动一个功能 - 传单
- 这是一个功能声明、表达式和对象吗?
- 谷歌图表:嗨不是一个功能
- 灯箱“下一个”功能仍然显示已隐藏的元素 (li) 显示:无;
- 有一个功能可以将字符串很好地缩短为一个、两个或三个字母