首页 > 其他 > 详细

viewport布局

时间:2020-08-21 18:36:06      阅读:58      评论:0      收藏:0      [点我收藏+]

1、viewport实例

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <title>vw/vh</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;padding: 0;
        }

        body {
            display: flex;
            flex-direction: column;
            justify-content: center;
            height: 100vh;  /* 回退设置,如果浏览器不支持calc()和var(); */
            height: calc(var(--vh, 1vh) * 100); /* 如果浏览器器不支持var(--vh)写法,则可以看成calc(1vh * 100);如果支持,则是calc(var(--vh) * 100); */
        }

        header, main, footer {
            padding: 1rem;
        }

        header, footer {background: #9ed0d7}

        main {
            overflow-y: scroll;
            -webkit-flex-grow: 1;
            flex-grow:1;
            padding: 20px;
            background: #f5e0e0;
        }

        div {
            height: 1000px;
            font-size: inherit;
        }

        div p:nth-child(1) { 

        }
        div p:nth-child(2) {
            font-size: 12px;
        }
        div p:nth-child(3) {
            /*font-size: 12px + 2vw;*/
            font-size: calc(12px + 2vw); /* 375px下,字体大小为19.5px */
        }
        
    </style>
</head>
<body>
    <header>header</header>
    <main>
        <div>
            <p>Main content -- 3</p>
            <p>浏览器默认字体16px,移动最小12px,PC14px</p>
            <p>沉迷学习,日渐消瘦~</p>
        </div>
    </main>
    <footer>Footer</footer>
   <script>
       window.onload = function () {
           // 得到视口高度,乘以1%得到一个vh单位的值(不支持以1%代替0.01),这里只是一个单位vh的占比。
           var vh = window.innerHeight * 0.01;  

           // 将‘--vh’设置为文档根元素的一个属性,值为(innerHeight / 100)px
           //vw:只是占页面宽度的百分比值,比如,375px的宽度,1vw就是3.75px
           document.documentElement.style.setProperty(--vh, vh + px);
       }
   </script>
</body>
</html>

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />    <title>vw/vh</title>    <style>        * {            box-sizing: border-box;            margin: 0;padding: 0;        }
        body {            display: flex;            flex-direction: column;            justify-content: center;            height: 100vh;  /* 回退设置,如果浏览器不支持calc()和var(); */            height: calc(var(--vh, 1vh) * 100); /* 如果浏览器器不支持var(--vh)写法,则可以看成calc(1vh * 100);如果支持,则是calc(var(--vh) * 100); */        }
        header, main, footer {            padding: 1rem;        }
        header, footer {background: #9ed0d7}
        main {            overflow-y: scroll;            -webkit-flex-grow: 1;            flex-grow:1;            padding: 20px;            background: #f5e0e0;        }
        div {            height: 1000px;            font-size: inherit;        }
        div p:nth-child(1) { 
        }        div p:nth-child(2) {            font-size: 12px;        }        div p:nth-child(3) {            /*font-size: 12px + 2vw;*/            font-size: calc(12px + 2vw); /* 375px下,字体大小为19.5px */        }            </style></head><body>    <header>header</header>    <main>        <div>            <p>Main content -- 3</p>            <p>浏览器默认字体16px,移动最小12px,PC14px</p>            <p>沉迷学习,日渐消瘦~</p>        </div>    </main>    <footer>Footer</footer>   <script>       window.onload = function () {           // 得到视口高度,乘以1%得到一个vh单位的值(不支持以1%代替0.01),这里只是一个单位vh的占比。           var vh = window.innerHeight * 0.01;  
           // 将‘--vh’设置为文档根元素的一个属性,值为(innerHeight / 100)px           //vw:只是占页面宽度的百分比值,比如,375px的宽度,1vw就是3.75px           document.documentElement.style.setProperty(‘--vh‘, vh + ‘px‘);       }   </script></body></html>

 

viewport布局

原文:https://www.cnblogs.com/L-xjco/p/13542458.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!