【问题标题】:zoom body with keeping aspect ratio correctly在正确保持纵横比的情况下缩放机身
【发布时间】:2017-04-12 14:53:32
【问题描述】:

我有这个具有固定宽度和高度的网络单页应用程序。我需要一直适应页面。我在扩展它时遇到问题。

如果我只根据它来缩放它,它就可以完美地工作。

var app = $('body');
var appWidth = app.width();
var currentWidth = window.innerWidth;
var ratio = currentWidth / appWidth;

app.css('zoom', ratio);

$(window).resize(function() {
  currentWidth = window.innerWidth;
  ratio = currentWidth / appWidth;
  app.css('zoom', ratio);
  console.log(ratio);
});
html {
  background-color: red;
}
body {
  background-color: gray;
  width: 960px;
  height: 540px;
  position:relative;
}
p{
position:absolute;
bottom:0;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.min.css" rel="stylesheet" type="text/css" />

  <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
  <h1>This content shrinks once it's scaled</h1>
  <img src="http://lorempixel.com/400/200/" />
  <p>This shouldn't get cut off once it's scaled</p>
</body>

但是,我试图从它的高度和宽度实现缩放,而无需拉伸以使其正确地适合页面。我知道在相同的情况下可能会有一些额外的间距,但只要没有拉伸就可以了。

下面的代码是我所得到的。如您所见,一旦从两侧缩放,它就不能很好地缩放。

var app = $('body');
var appWidth = app.width();
var appHeight = app.height();
var lastWidth = window.innerWidth;
var lastHeight = window.innerHeight;
var currentWidth;
var currentHeight;
var ratio;


app.css('zoom', ratio);

fitToPage();

$(window).resize(function() {
  fitToPage();
});


function fitToPage() {
  currentWidth = window.innerWidth;
  currentHeight = window.innerHeight;

  if(currentWidth !== lastWidth && currentHeight !== lastHeight){
    console.log('both width and height changed');
    ratio = currentWidth / appWidth;
    app.css('zoom', ratio);
    lastWidth = window.innerWidth;
    lastHeight = window.innerHeight;
  }
  else if (currentWidth !== lastWidth){
    console.log('width changed');
    ratio = currentWidth / appWidth;
    app.css('zoom', ratio);
    lastWidth = window.innerWidth;
  }
  else if (currentHeight !== lastHeight){
    console.log('height changed');
    ratio = currentHeight / appHeight;
    app.css('zoom', ratio);
    lastHeight = window.innerHeight;
  }
   
}
html {
  background-color: red;

}
body {
  background-color: gray;
  width: 960px;
  height: 540px;
  position:relative;
}
p{
position:absolute;
bottom:0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.min.css" rel="stylesheet" type="text/css" />
  <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
  <h1>This content shrinks once it's scaled</h1>
  <img src="http://lorempixel.com/400/200/" />
  <p>This shouldn't get cut off once it's scaled</p>
</body>

【问题讨论】:

  • 我看看。首先,它似乎永远不会进入你的最后一个 else-if 语句。
  • 哦,对不起,我想我已经修改了代码。如果你再次打开它,我将它恢复回来它现在应该可以工作了。 output.jsbin.com/sejabi
  • 这是您要寻找的行为:codepen.io/gc-nomade/full/VmMPGd 吗?

标签: jquery css zooming aspect-ratio


【解决方案1】:

不太确定,但是如果您只查看窗口的宽度并保持具有相同高度/宽度比的主体。 CSS 可以提供帮助:

(高度可以溢出html)

html {
  background-color: red;
  position:relative;
  padding-top:56.25%;/* 54/96=0.5625 */
}
body {
  background-color: gray;
  position:absolute;
  top:0;
  bottom:0;
  left:0;
  right:0;
}
&lt;h1&gt;Test App&lt;/h1&gt;

要了解其工作原理,请参阅:

https://www.w3.org/TR/CSS2/box.html#padding-properties

&lt;percentage&gt;

百分比是根据生成框的包含块的宽度计算的,即使对于“padding-top”和“padding-bottom”也是如此。如果包含块的宽度取决于此元素,则结果布局在 CSS 2.1 中未定义。

你也可以避免盒子高于窗口的高度:

(框不会溢出窗口)

html {
  background:tomato;
  margin:0;
  max-width:156vh;
  position:relative;
  margin: 5vh auto;
}
html:before {
  content:'';
  padding-top:56.25%;
  display:inline-block
}
body {
  position:absolute;
  top:0;
  left:0;
  bottom:0;
  right:0;
  background:#aaa;
  /* makeup*/
  display:flex;
  align-items:center;
  justify-content:center;
  color:white;
  font-size:10vh;
}
keep my ratio &amp; inside window

【讨论】:

  • 这看起来是一个不错的 css 解决方案,但 css 中的内容不会随着大小的变化而缩放。
  • @TanKucukhas 没问题,但是会有一点,里面的内容太小而无法看到或阅读,或者太大而无法舒适地看到:) 测试和图像根据比例调整大小的示例@ 987654322@
  • 在我的情况下,我在固定宽度、高度和绝对位置工作,我知道如果宽度是动态的会更容易。我想要完成的是让应用程序在任何类型的设备上占用最多的空间。假设您正在 1920x1080 上运行该应用程序,该应用程序将完全适合全屏,因为纵横比匹配,但是如果您有不同的纵横比,例如 800 640,将会有空间,但应用程序仍然会占用最多的空间能够。使用浏览器缩放有助于在不影响布局宽度的情况下保持布局分离。
【解决方案2】:

如果您超过 960 像素的宽度,则任何 if-else-conditions 都不成立,因此缩放停止。

您可以复制它以在控制台中查看它。我添加了一个超过 960px 宽度的案例。

function fitToPage() {
  console.log("ratio" +ratio);
  console.log("appwidth " +appWidth);
  console.log("curwidth " +currentWidth);
  console.log("curheight " +currentHeight);
  console.log("appheight " +appHeight);
  currentWidth = window.innerWidth;
  currentHeight = window.innerHeight;

  if (appWidth > currentWidth && appHeight > currentHeight) {
      console.log('both');
      ratio = currentWidth / appWidth;
      app.css('zoom', ratio);
  } else if (appWidth > currentWidth) {
      console.log('only width');
      ratio = currentWidth / appWidth;
      app.css('zoom', ratio);
  } else if (appHeight > currentHeight) {
      console.log('only height');
      ratio = currentHeight / appHeight;
      app.css('zoom', ratio);
  }else if (appWidth < currentWidth){
      ratio = currentWidth / appWidth;
      app.css('zoom', ratio);
      console.log('smallercase');
  }

}

【讨论】:

  • 对代码进行了少许改进,但现在它最初会缩放并检测是否缩放了宽度、高度或两者,但在相同的情况下它仍然不适合页面。
猜你喜欢
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 2011-08-24
  • 2021-11-26
  • 2023-04-06
  • 1970-01-01
相关资源
最近更新 更多