<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://wiki.moorper.com/lib/exe/css.php?s=feed" type="text/css"?>
<rdf:RDF
    xmlns="http://purl.org/rss/1.0/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel rdf:about="https://wiki.moorper.com/feed.php">
        <title>Moorper Wiki - blog:html</title>
        <description>我等采石之人，当心怀建造大教堂之愿景。</description>
        <link>https://wiki.moorper.com/</link>
        <image rdf:resource="https://wiki.moorper.com/_media/wiki:dokuwiki.svg" />
       <dc:date>2026-04-08T21:08:23+00:00</dc:date>
        <items>
            <rdf:Seq>
                <rdf:li rdf:resource="https://wiki.moorper.com/blog:html:resize"/>
            </rdf:Seq>
        </items>
    </channel>
    <image rdf:about="https://wiki.moorper.com/_media/wiki:dokuwiki.svg">
        <title>Moorper Wiki</title>
        <link>https://wiki.moorper.com/</link>
        <url>https://wiki.moorper.com/_media/wiki:dokuwiki.svg</url>
    </image>
    <item rdf:about="https://wiki.moorper.com/blog:html:resize">
        <dc:format>text/html</dc:format>
        <dc:date>2021-11-18T14:31:47+00:00</dc:date>
        <dc:creator>Anonymous (anonymous@undisclosed.example.com)</dc:creator>
        <title>H5 移动页面自适应解决方案</title>
        <link>https://wiki.moorper.com/blog:html:resize</link>
        <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;h5-移动页面自适应解决方案&quot;&gt;H5 移动页面自适应解决方案&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
本文参考&lt;a href=&quot;https://main.m.taobao.com/&quot; class=&quot;urlextern&quot; target=&quot;_blank&quot; title=&quot;https://main.m.taobao.com/&quot; rel=&quot;ugc nofollow noopener&quot;&gt;手淘&lt;/a&gt;的 H5 页面自适应解决方案。
&lt;/p&gt;

&lt;p&gt;
打开手淘的首页，查看源码，我们会在 head 中发现这样一段代码，这就是手淘页面自适应的关键。
&lt;/p&gt;
&lt;pre class=&quot;code javascript&quot;&gt;&lt;span class=&quot;sy0&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;e&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;t&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;var&lt;/span&gt; n&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;t.&lt;span class=&quot;me1&quot;&gt;documentElement&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;d&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;e.&lt;span class=&quot;me1&quot;&gt;devicePixelRatio&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;function&lt;/span&gt; i&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;var&lt;/span&gt; e&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;n.&lt;span class=&quot;me1&quot;&gt;clientWidth&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;3.75&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;n.&lt;span class=&quot;me1&quot;&gt;style&lt;/span&gt;.&lt;span class=&quot;me1&quot;&gt;fontSize&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;e&lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;px&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;function&lt;/span&gt; e&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;t.&lt;span class=&quot;me1&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;?&lt;/span&gt;t.&lt;span class=&quot;me1&quot;&gt;body&lt;/span&gt;.&lt;span class=&quot;me1&quot;&gt;style&lt;/span&gt;.&lt;span class=&quot;me1&quot;&gt;fontSize&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;16px&amp;quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;:&lt;/span&gt;t.&lt;span class=&quot;me1&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;DOMContentLoaded&amp;quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;e&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;i&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;e.&lt;span class=&quot;me1&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;resize&amp;quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;i&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;e.&lt;span class=&quot;me1&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;pageshow&amp;quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;e&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;e.&lt;span class=&quot;me1&quot;&gt;persisted&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;i&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;lt;=&lt;/span&gt;d&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span class=&quot;kw1&quot;&gt;var&lt;/span&gt; o&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;t.&lt;span class=&quot;me1&quot;&gt;createElement&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;body&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;a&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;t.&lt;span class=&quot;me1&quot;&gt;createElement&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;div&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;a.&lt;span class=&quot;me1&quot;&gt;style&lt;/span&gt;.&lt;span class=&quot;me1&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;.5px solid transparent&amp;quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;o.&lt;span class=&quot;me1&quot;&gt;appendChild&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;a&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;n.&lt;span class=&quot;me1&quot;&gt;appendChild&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;o&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;===&lt;/span&gt;a.&lt;span class=&quot;me1&quot;&gt;offsetHeight&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;n.&lt;span class=&quot;me1&quot;&gt;classList&lt;/span&gt;.&lt;span class=&quot;me1&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;hairlines&amp;quot;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;n.&lt;span class=&quot;me1&quot;&gt;removeChild&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;o&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;window&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt;document&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
我们可以讲这段代码复制到我们自己页面的 head 中，其中需要关注两个数字。 1、3.75，这个数字需要根据我们设计图的宽带来调整，把设计图宽度除以100就可以了； 2、16，这个数字是 body 区域的默认字体大小，可以根据自己的需求修改。
&lt;/p&gt;

&lt;p&gt;
最后再添加一段仍是从手淘页面复制过来的 viewport 配置就可以尽情书写代码了：
&lt;/p&gt;
&lt;pre class=&quot;code html4strict&quot;&gt;&lt;span class=&quot;sc2&quot;&gt;&amp;lt;&lt;a target=&quot;_blank&quot; href=&quot;http://december.com/html/4/element/meta.html&quot;&gt;&lt;span class=&quot;kw2&quot;&gt;meta&lt;/span&gt;&lt;/a&gt; &lt;span class=&quot;kw3&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;viewport&amp;quot;&lt;/span&gt; &lt;span class=&quot;kw3&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover&amp;quot;&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
设计图中的 px 像素单位，我们把它除以 100 改为 rem 单位，就可以了，这样不管在什么屏幕上看到的页面比例和设计图都是一模一样的。
&lt;/p&gt;

&lt;p&gt;
&amp;lt;&lt;abbr title=&quot;HyperText Markup Language&quot;&gt;HTML&lt;/abbr&gt;&amp;gt;
&amp;lt;iframe src=“&lt;a href=&quot;https://codesandbox.io/embed/h5-yidongyemianziguaying-kvu0h?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark&amp;amp;view=preview&quot; class=&quot;urlextern&quot; target=&quot;_blank&quot; title=&quot;https://codesandbox.io/embed/h5-yidongyemianziguaying-kvu0h?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark&amp;amp;view=preview&quot; rel=&quot;ugc nofollow noopener&quot;&gt;https://codesandbox.io/embed/h5-yidongyemianziguaying-kvu0h?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark&amp;amp;view=preview&lt;/a&gt;”
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;   style=&amp;quot;width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;&amp;quot;
   title=&amp;quot;H5 移动页面自适应&amp;quot;
   allow=&amp;quot;accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking&amp;quot;
   sandbox=&amp;quot;allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts&amp;quot;
 &amp;gt;&amp;lt;/iframe&amp;gt;&lt;/pre&gt;

&lt;p&gt;
&amp;lt;/&lt;abbr title=&quot;HyperText Markup Language&quot;&gt;HTML&lt;/abbr&gt;&amp;gt;
&lt;/p&gt;

&lt;/div&gt;
</description>
    </item>
</rdf:RDF>
