该文章包含 h5 的多平台自适应 如下 :
- <script>
- /* 根据窗口的大小自动设置 body 内的文字大小 */
- (function (width) {
- var doc = width.document,
- element = doc.documentElement,
- i = 750,
- d = i / 100,
- o = "orientationchange" in width ? "orientationchange" : "resize",
- a = function () {
- var width = element.clientWidth || 375;
- width> 750 && (width = 750);
- element.style.fontSize = width / d + "px"
- };
- doc.addEventListener && (width.addEventListener(o, a, !1), doc.addEventListener("DOMContentLoaded", a, !1))
- })(Windows);
- </script>
使这个 div 内的 img 自动水平垂直居中的主要思路 给父级元素设置 position: relative;
该 img 设置 img[alt=""] {width: 100%; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);}
- <!DOCTYPE html>
- <HTML>
- <head>
- <meta charset="utf-8" />
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no,minimal-ui">
- <style>
- /* 设置全局自动水平居中 且 padding 的大小包含在元素的宽高内的 */ *{ margin: 0 auto; padding:0;
- position: relative; -webkit-appearance: none; -moz-box-sizing: border-box;
- -webkit-box-sizing: border-box; -o-box-sizing: border-box; -ms-box-sizing:
- border-box; box-sizing: border-box; } /* 清除 a 标签令人恶心的样式 */ a { color: #000;
- cursor: pointer; outline:none;-moz-outline-style:none; } a:link {text-decoration:
- none;} a:visited {text-decoration: none;} a:hover {text-decoration: none;color:
- #000;} a:active {text-decoration: none;} ul, ol ,a{list-style: none;} body
- { position: relative; box-shadow: none; color: #000; background: #fff;
- max-width: 750px; -webkit-tap-highlight-color: rgba(0,0,0,0); } body, button,
- input, select, textarea, div, nav, section { font: 0.29rem "微软雅黑"; -webkit-font-smoothing:
- antialiased; -moz-OS X-font-smoothing: grayscale;overflow:-Scroll; } /*
- 设置滚动条的样式 开始 */ ::-webkit-scrollbar {width:0px; height: 0px;} /* 滚动槽 */
- ::-webkit-scrollbar-track { -webkit-box-shadow:inset006pxrgba(0,0,0,.5);
- border-radius:1px; background:rgba(255,255,255,.5); border:1px solid rgba(0,0,0,.5);
- } /* 滚动条滑块 */ ::-webkit-scrollbar-thumb { border-radius:1px; background:rgba(0,0,0,0.7);
- -webkit-box-shadow:inset006pxrgba(0,0,0,0.5); } ::-webkit-scrollbar-thumb:Windows-inactive
- { background:rgba(0,0,0,0.2); } /* 设置滚动条的样式 结束 */ img[alt=""] { width:
- 100%; position: absolute; top: 50%; left: 50%; transform: translate(-50%,
- -50%); } .navCenterContent { transition: auto; animation: auto; -moz-transition:
- auto; -webkit-transition: auto; -o-transition: auto; } .display-flex {
- display: -webkit-box; display: -moz-box; display:-webkit-flex; display:
- -ms-flexbox; display:flex; } body { padding: .5em; } .information { padding:
- 10px 0; overflow: hidden; /*border-top: 1px solid #ccc;*/ border-bottom:
- 1px solid #ccc; } .information a { height: 100%; } .information_l{ width:
- 74%; padding-right: .3em; } .information_l p:nth-child(1) { font-size:
- 1.1em; } .information_l p:nth-child(2) { height: 2.6em; display: -webkit-box;
- -webkit-box-orient: vertical; -webkit-line-clamp: 2; font-size: .9em; text-indent:
- 2em; color: #333; overflow: hidden; } .p_bottom { } .p_bottom span:nth-child(1)
- { font-size: .8em; color: #999; } .information_r{ width: 26%; overflow:
- hidden; border-radius: 5px; }
- </style>
- </head>
- <body>
- <section class="information">
- <a class="display-flex">
- <div class="information_l">
- <p>
- 双十一红包
- </p>
- <p>
- 2018 双十一红包, 每天 3 次, 最高 1111 元, 最低 1 元余额红包, 可直接使用. 健康周为你双十一大战调理身体, 活动已开启,
- 参与地址: 境掌柜 App .
- </p>
- <p class="p_bottom">
- <span>
- 2018/10/24
- </span>
- </p>
- </div>
- <div class="information_r">
- <img src="https://cdn.portrait.qdfuns.com/000/03/16/31674_27ac0ca86e06d1822d546ed038ea33ae.jpg!/sq/140"
- alt />
- </div>
- </a>
- </section>
- <section class="information">
- <a class="display-flex">
- <div class="information_l">
- <p>
- 双十一红包
- </p>
- <p>
- 2018 双十一红包, 每天 3 次, 最高 1111 元, 最低 1 元余额红包, 可直接使用. 健康周为你双十一大战调理身体, 活动已开启,
- 参与地址: 境掌柜 App .
- </p>
- <p class="p_bottom">
- <span>
- 2018/10/24
- </span>
- </p>
- </div>
- <div class="information_r">
- <img src="https://cdn.portrait.qdfuns.com/000/03/16/31674_27ac0ca86e06d1822d546ed038ea33ae.jpg!/sq/140"
- alt />
- </div>
- </a>
- </section>
- </body>
- <script>
- /* 根据窗口的大小自动设置 body 内的文字大小 */
- (function(width) {
- var doc = width.document,
- element = doc.documentElement,
- i = 750,
- d = i / 100,
- o = "orientationchange" in width ? "orientationchange": "resize",
- a = function() {
- var width = element.clientWidth || 375;
- width > 750 && (width = 750);
- element.style.fontSize = width / d + "px"
- };
- doc.addEventListener && (width.addEventListener(o, a, !1), doc.addEventListener("DOMContentLoaded", a, !1))
- })(Windows);
- </script>
- </HTML>
来源: http://www.qdfuns.com/article/31674/51b02a79f92f7ee6584e622719c70783.html