jquery - 缩放主体并正确保持纵横比

标签 jquery css zooming aspect-ratio

我有一个具有固定宽度和高度的单页 Web 应用程序。我需要始终适应页面。我在缩放时遇到问题。

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

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>

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

下面的代码是我得到的。如您所见,一旦从两边进行缩放,它就无法很好地缩放。

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>

最佳答案

如果你超过 960px 的宽度,你的 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');
  }

}

关于jquery - 缩放主体并正确保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40851844/

相关文章:

javascript - 不引人注目的 javascript 的条件执行

javascript - 窗口大小更改时 jQuery 不执行

html - CSS 网格中的自动换行

jquery - 制作一个文本字段和一个全宽的按钮

zooming - 缩放后的 Ipython Bokeh 散点图标记大小

javascript - 使用多个 ID 和类的 jQuery 翻转

css - 使用 YUI3 动态加载样式表

html - 我怎样才能使 Zoom in mobile 像在 PC 上一样......?

c# - Unity 中相机平滑变焦的问题

jQuery .ready/resize 在 iPad 上不起作用