Js处理页面响应式

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

内容简介: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;
}

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

查看所有标签

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

Writing Windows VxDs and Device Drivers, Second Edition

Writing Windows VxDs and Device Drivers, Second Edition

Karen Hazzah / CMP / 1996-01-12 / USD 54.95

Software developer and author Karen Hazzah expands her original treatise on device drivers in the second edition of "Writing Windows VxDs and Device Drivers." The book and companion disk include the a......一起来看看 《Writing Windows VxDs and Device Drivers, Second Edition》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具