Home >Web Front-end >JS Tutorial >A few lines of js code to achieve self-adaptation

A few lines of js code to achieve self-adaptation

高洛峰
高洛峰Original
2017-02-25 16:11:321447browse

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);

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!

Statement:
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