前端案例:极速问诊项目(移动端自适应)(HTML+CSS+JS)

一个简单的移动端案例,模拟不同设备下逻辑分辨率不同,宽高自适应

正常打开整体布局

打开 f12 (ctrl + shift + M)或者点击左上角图标,将其模拟为移动端设备

在移动设备iPhone6/7/8,逻辑分辨率375的整体布局

其banner图尺寸

现在切换设备为iPad,逻辑分辨率为768,此设备下banner图尺寸自适应

再切换设备为iPhone 14 Pro Max,逻辑分辨率为430,此设备下banner图尺寸自适应

源码

HTML(index.html)代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>极速问诊</title>
    <link rel="stylesheet" href="./iconfont/iconfont.css">
    <link rel="stylesheet" href="./css/index.css">
</head>
<body>
    <!-- 头部区域 -->
    <header>
        <a href="#" class="back"><span class="iconfont icon-jiantou3"></span></a>
        <h3>极速问诊</h3>
        <a href="#" class="note">问诊记录</a>
    </header>

    <!-- banner区域 -->
    <div class="banner">
        <img src="./assets/docter.jpg" alt="">
        <p><span>20s</span> 快速匹配专业医生</p>
    </div>

    <!-- 问诊类型 -->
    <div class="type">
        <ul>
            <li>
                <a href="#">
                    <span class="iconfont icon-yisheng"></span>
                    <div class="txt">
                        <h4>三甲图文问诊</h4>
                        <p>三甲主治及以上级别医生</p>
                    </div>
                    <span class="iconfont icon-jiantou21"></span>
                </a>
            </li>
            <li>
                <a href="#">
                    <span class="iconfont icon-xinxi1"></span>
                    <div class="txt">
                        <h4>普通图文问诊</h4>
                        <p>二甲主治及以上级别医生</p>
                    </div>
                    <span class="iconfont icon-jiantou21"></span>
                </a>
            </li>
        </ul>
    </div>
    <!-- 引入flexible.js移动端布局js -->
    <script src="./js/flexible.js"></script>
</body>
</html>

CSS样式(index.css)代码:

*,
::after,
::before {
  box-sizing: border-box;
}
body,
ul,
ol,
p,
h1,
h2,
h3,
h4,
h5,
h6 {
  margin: 0;
  padding: 0;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, PingFangSC-Regular, "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  color: #333;
}
a {
  color: #333;
  text-decoration: none;
  -webkit-tap-highlight-color: regb(0, 0, 0, 0);
}
ul {
  list-style-type: none;
}
li {
  list-style: none;
}
img {
  vertical-align: bottom;
}
header {
  display: flex;
  justify-content: space-between;
  padding: 0 0.4rem;
  height: 1.17333333rem;
  line-height: 1.17333333rem;
}
header .icon-jiantou3 {
  font-size: 0.48rem;
}
header h3 {
  font-size: 0.45333333rem;
}
header .note {
  font-size: 0.4rem;
  color: #2cb2a5;
}
.banner {
  margin-top: 0.8rem;
  margin-bottom: 0.90666667rem;
  text-align: center;
}
.banner img {
  margin-bottom: 0.48rem;
  width: 6.4rem;
  height: 5.49333333rem;
}
.banner p {
  font-size: 0.42666667rem;
}
.banner p span {
  color: #16c2a3;
}
.type {
  padding: 0 0.4rem;
}
.type li {
  margin-bottom: 0.4rem;
  padding: 0 0.4rem;
  height: 2.08rem;
  border: 0.5px solid #ededed;
  border-radius: 0.10666667rem;
}
.type a {
  display: flex;
  align-items: center;
  height: 2.08rem;
}
.type a .icon-yisheng,
.type a .icon-xinxi1 {
  margin-right: 0.37333333rem;
  width: 1.06666667rem;
  height: 1.06666667rem;
  border-radius: 50%;
  line-height: 1.06666667rem;
  text-align: center;
  font-size: 0.64rem;
  color: #faf5f5;
}
.type a .icon-yisheng {
  background-color: #16c2a3;
  background-image: linear-gradient(to right bottom, #a3eddf, #41c2aa);
}
.type a .icon-xinxi1 {
  background-color: aqua;
  background-image: linear-gradient(to right bottom, #97c4f3, #6c8dde);
}
.type a .txt {
  flex: 1;
}
.type a .txt h4 {
  margin-bottom: 0.13333333rem;
  font-size: 0.42666667rem;
  color: #3c3e42;
  line-height: 0.64rem;
}
.type a .txt p {
  font-size: 0.34666667rem;
  color: #848484;
  line-height: 0.53333333rem;
}
.type a .icon-jiantou21 {
  font-size: 0.42666667rem;
}

JS(flexible.js)代码:

(function flexible(window, document) {
    //获取html根目录
    var docEl = document.documentElement
        //dpr 物理像素比
    var dpr = window.devicePixelRatio || 1

    // adjust body font size 设置body字体大小
    function setBodyFontSize() {
        if (document.body) {
            document.body.style.fontSize = (12 * dpr) + 'px'
        } else {
            //如果页面没有body 这个元素,则只用DOMContentLoaded 监听,等待 DOM 元素加载完毕
            document.addEventListener('DOMContentLoaded', setBodyFontSize)
        }
    }
    setBodyFontSize();

    // set 1rem = viewWidth / 10 设置html 元素字体大小
    function setRemUnit() {
        var rem = docEl.clientWidth / 10
        docEl.style.fontSize = rem + 'px'
    }

    setRemUnit()

    // reset rem unit on page resize 当页面尺寸大小发生改变时,重新设置一下rem大小。
    window.addEventListener('resize', setRemUnit)
        //pageshow 是我们重新加载页面触发的事件
    window.addEventListener('pageshow', function(e) {
        //e.persisted 返回的是 true 就是如果这个页面从缓存中获取过来,也要重新设置rem大小
        if (e.persisted) {
            setRemUnit()
        }
    })

    // detect 0.5px supports 解决移动端浏览器支持0.5像素
    if (dpr >= 2) {
        var fakeBody = document.createElement('body')
        var testElement = document.createElement('div')
        testElement.style.border = '.5px solid transparent'
        fakeBody.appendChild(testElement)
        docEl.appendChild(fakeBody)
        if (testElement.offsetHeight === 1) {
            docEl.classList.add('hairlines')
        }
        docEl.removeChild(fakeBody)
    }
}(window, document))
相关推荐
影子落人间2 分钟前
已解决npm ERR! request to https://registry.npm.taobao.org/@vant%2farea-data failed
前端·npm·node.js
世俗ˊ27 分钟前
CSS入门笔记
前端·css·笔记
子非鱼92127 分钟前
【前端】ES6:Set与Map
前端·javascript·es6
6230_32 分钟前
git使用“保姆级”教程1——简介及配置项设置
前端·git·学习·html·web3·学习方法·改行学it
想退休的搬砖人41 分钟前
vue选项式写法项目案例(购物车)
前端·javascript·vue.js
加勒比海涛1 小时前
HTML 揭秘:HTML 编码快速入门
前端·html
啥子花道1 小时前
Vue3.4 中 v-model 双向数据绑定新玩法详解
前端·javascript·vue.js
麒麟而非淇淋1 小时前
AJAX 入门 day3
前端·javascript·ajax
茶茶只知道学习1 小时前
通过鼠标移动来调整两个盒子的宽度(响应式)
前端·javascript·css
清汤饺子1 小时前
实践指南之网页转PDF
前端·javascript·react.js