Giter VIP home page Giter VIP logo

basics's Introduction

Basics

移动端适配,rem和固定宽度

方法一: rem布局,动态设置html的font-size

   我们知道rem是根据html设置的font-size计算的,因此我们可以在布局是全部使用rem,再通过动态设置html的font-size值来缩放页面, 达到适配的效果。

   为了方便计算,个人喜欢将fonts-size设置为100px,这样750px * 1334px的设计稿,为7.5rem * 13.34rem

   当然,你也可以设置其他font-size值,尽量合理些,因为计算时可能出现一些除不断的情况,会有误差

   布局好后,通过js动态计算font-size值    

//屏幕适应
var html = document.documentElement;
    var k = 750;//设计稿宽度
    html.style.fontSize = html.clientWidth / k * 100 + "px";

方法二: 固定宽度

   添加头部

 <meta name="viewport" content="width=设计稿宽度, target-densitydpi=device-dpi">

   布局时完全按照设计稿布局即可,浏览器会帮我们自动缩放到适配屏幕大小,包括图片字体等。

   target-densitydpi=device-dpi 属性用于适配android4.4以下机型,已逐渐不再支持。

对比

   两种方法都很很好的适配绝大部分浏览器,方法一适配范围更广些,对于那些不支持缩放的环境(如webview中不开启缩放)也能很好的适应。

basics's People

Contributors

zhengyl36 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.