Home > Web Front-end > JS Tutorial > body text

A few lines of js code to achieve self-adaptation

高洛峰
Release: 2017-02-25 16:11:32
Original
1406 people have browsed it

Write the following lines in javascript:

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

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. I also hope to support the PHP Chinese website. !

For more articles related to a few lines of js code to achieve self-adaptation, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template