前端案例:极速问诊项目(移动端自适应)(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))
相关推荐
excel2 分钟前
前端必备:从能力检测到 UA-CH,浏览器客户端检测的完整指南
前端
前端小巷子8 分钟前
Vue 3全面提速剖析
前端·vue.js·面试
悟空聊架构15 分钟前
我的网站被攻击了,被干掉了 120G 流量,还在持续攻击中...
java·前端·架构
CodeSheep17 分钟前
国内 IT 公司时薪排行榜。
前端·后端·程序员
尖椒土豆sss20 分钟前
踩坑vue项目中使用 iframe 嵌套子系统无法登录,不报错问题!
前端·vue.js
遗悲风21 分钟前
html二次作业
前端·html
江城开朗的豌豆25 分钟前
React输入框优化:如何精准获取用户输入完成后的最终值?
前端·javascript·全栈
CF14年老兵25 分钟前
从卡顿到飞驰:我是如何用WebAssembly引爆React性能的
前端·react.js·trae
画月的亮28 分钟前
前端处理导出PDF。Vue导出pdf
前端·vue.js·pdf
江城开朗的豌豆34 分钟前
拆解Redux:从零手写一个状态管理器,彻底搞懂它的魔法!
前端·javascript·react.js