前端案例:极速问诊项目(移动端自适应)(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))
相关推荐
雪碧聊技术28 分钟前
深入解析Vue中v-model的双向绑定实现原理
前端·javascript·vue.js·v-model
快起来别睡了30 分钟前
手写 Ajax 与 Promise:从底层原理到实际应用
前端
打不着的大喇叭1 小时前
uniapp的光标跟随和打字机效果
前端·javascript·uni-app
无我Code1 小时前
2025----前端个人年中总结
前端·年终总结·创业
程序猿阿伟1 小时前
《前端路由重构:解锁多语言交互的底层逻辑》
前端·重构
Sun_light2 小时前
6个你必须掌握的「React Hooks」实用技巧✨
前端·javascript·react.js
爱学习的茄子2 小时前
深度解析JavaScript中的call方法实现:从原理到手写实现的完整指南
前端·javascript·面试
莫空00002 小时前
Vue组件通信方式详解
前端·面试
呆呆的心2 小时前
揭秘 CSS 伪元素:不用加标签也能玩转出花的界面技巧 ✨
前端·css·html
susnm2 小时前
Dioxus 与数据库协作
前端·rust