Js处理页面响应式

栏目: JavaScript · 发布时间: 5年前

内容简介:js处理页面响应式原理:假定页面的宽度为实际页面宽度为clientWidth,设置页面html元素的宽度为

js处理页面响应式原理:

假定页面的宽度为 320px (iphone5),页面元素html的字体大小为 20px ,这是比例标准

实际页面宽度为clientWidth,设置页面html元素的宽度为 20 * (clientWidth / 320) + 'px'

现有一设计页面,尺寸 720x1080(放大一倍),设计页面的logo(216x108),如何让logo图片自适应?

1.首先页面注入以下js

(function(doc, win) {
  var docEl = doc.documentElement,
    resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
    recalc = function() {
      var clientWidth = docEl.clientWidth;
      if (!clientWidth) return;
      docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
    };
  if (!doc.addEventListener) return;
  win.addEventListener(resizeEvt, recalc, false);
  doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

2.设置图片的宽度css

通过上面公式推出,设计页面html的字体大小为 (20*720)/320 = 45 px

所以图片上的宽自适应可以设置为 216/45 rem

.logo{
    width:4.8rem;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Data Structures and Algorithm Analysis in Java

Data Structures and Algorithm Analysis in Java

Mark A. Weiss / Pearson / 2006-3-3 / USD 143.00

As the speed and power of computers increases, so does the need for effective programming and algorithm analysis. By approaching these skills in tandem, Mark Allen Weiss teaches readers to develop wel......一起来看看 《Data Structures and Algorithm Analysis in Java》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具