前端案例:极速问诊项目(移动端自适应)(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))
相关推荐
kyriewen19 小时前
我手写了一个 EventEmitter,面试官追问了 6 个问题——第 4 个我没答上来
前端·javascript·面试
IT_陈寒19 小时前
Java的Date类又坑了我一次,改用时间戳真香
前端·人工智能·后端
小林攻城狮20 小时前
使用 Transport 节流解决 Vercel AI SDK 流式渲染卡死问题
前端·react.js
前端缘梦20 小时前
告别 TS 运行时类型漏洞!Zod 完整入门实战教程(前端 / 全栈必备)
前端·react.js·全栈
the_answer21 小时前
Webpack vs Vite 深度对比分析
前端·webpack
转转技术团队21 小时前
验证码识别实战:前端不写页面,改训模型了?
前端
MomentYY21 小时前
Temperature:AI 的“脑洞旋钮”
前端·llm·ai编程
远航_21 小时前
OpenSpec 完整详细介绍
前端·后端
召钱熏21 小时前
状态枚举正确≠渲染正确:一个语音按钮的状态机边界修复实录
android·前端
SkyWalking中文站21 小时前
认识 Horizon UI · 1/17:SkyWalking 新一代可观测性控制台
运维·前端·监控