html+css 静态页面(phone端+pc端)

本文通过两个案例——活力广东和星巴克,展示了如何使用HTML和CSS创建适应PC及手机端的静态页面。每个案例包含独立的index.html文件以及针对PC(pc.css)、平板(pad.css)和手机(phone.css)的样式文件,实现了多设备适配。

案例1  活力广东

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>活力广东</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        body{
            height: 100vh;
            /* background-color: aqua; */
            background-image: url(../img/bg.png);
            background-size: cover;
            background-position: center center;
        }
    </style>
    <link rel="stylesheet" href="../css/pc.css" media="only screen and (min-width: 800px)">
    <link rel="stylesheet" href="../css/phone.css" media="only screen and (max-width: 800px)">
</head>
<body>
    <header>
        <img src="../img/logo.png" alt="">
        <img src="../img/welcome.png" alt="">
    </header>
    <main>
        <ul class="top-left">
            <li><img src="../img/visit.png" alt=""></li>
            <li><img src="../img/contract.png" alt=""></li>
            <li><img src="../img/admin.png" alt=""></li>
            <li><img src="../img/guide.png" alt=""></li>
            <li><img src="../img/expo.png" alt=""></li>
            <li><img src="../img/axam.png" alt=""></li>
        </ul>
        <ul class="bottom-right">
			<li><img src="../img/sjzx.png" alt=""></li>
			<li><img src="../img/sina.png" alt=""></li>
			<li><img src="../img/hall.png" alt=""></li>
			<li><img src="../img/tencent.png" alt=""></li>
			<li><img src="../img/visit.png" alt=""></li>
			<li><img src="../img/weico.png" alt=""></li>
		</ul>
    </main>
</body>
</html>

pc.css

header{
    height: 60px;
    /* background-color: pink; */
    margin-top: 112px;
}
header img:nth-of-type(1){
    margin-left: 150px;
}
main{
    width: 863px;
    height: 424px;
    margin: 22px auto 0;
}
li img{
    width: 100%;
    height: 100%;
}
.top-left{
    width: 454px;
    height: 100%;
    float: left;
    /* background-color: pink; */
}
.bottom-right{
    width: 404px;
    height: 100%;
    float: right;
    /* background-color: gold; */
}
.top-left li:nth-of-type(2n+1){
    float: left;
    width: 278px;
    height: 140px;
    margin-bottom: 2px;
}
.top-left li:nth-of-type(2n){
    float: left;
    width: 162px;
    height: 140px;
    margin-left: 14px;
    margin-bottom: 2px;
}
.bottom-right li:nth-of-type(2n+1){
    float: left;
    width: 240px;
	height: 140px;
	margin-bottom: 2px;

}
.bottom-right li:nth-of-type(2n){
    float: left;
    width: 162px;
	height: 140px;
	margin-left: 2px;
	margin-bottom: 2px;
}

phone.css

header{
    width: 90vw;
    height: 66px;
    /* background-color: pink; */
    margin: 44px auto 0;
}
header img:nth-of-type(1){
    display: block;
    height: 34px;
    margin-bottom: 12px;
}
header img:nth-of-type(2){
    display: block;
    height: 20px;
}
main{
    width: 90vw;
    height: 502px;
    margin: 40px auto 0;
}
li img{
    width: 100%;
    height: 100%;
}
.top-left{
    width: 100%;
    height: 250px;
}
.bottom-right{
    width: 100%;
    height: 250px;
    margin-top: 2px;
}
.top-left li:nth-of-type(2n+1) {
    float: left;
	width: 58%;
	height: 82px;
	margin-bottom: 2px;
}

.top-left li:nth-of-type(2n) {
    float: left;
	width: 40%;
	height: 82px;
	margin-left: 2%;
	margin-bottom: 2px;
}
.bottom-right li:nth-of-type(2n) {
    float: left;
	width: 58%;
	height: 82px;
	margin-left: 2%;
	margin-bottom: 2px;
}

.bottom-right li:nth-of-type(2n+1) {
    float: left;
	width: 40%;
	height: 82px;
	margin-bottom: 2px;
}

案例2 星巴克

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>start bark</title>
    <link rel="stylesheet" href="../css/reset.css">
    <link rel="stylesheet" href="../css/pc.css" media="only screen and (min-width: 1200px)">
    <link rel="stylesheet" href="../css/pad.css" media="only screen and (min-width: 800px) and (max-width: 1200px)">
    <link rel="stylesheet" href="../css/phone.css" media="only screen and (max-width: 800px)">
    <style>

    </style>
</head>
<body>
    <nav>
        <div class="star">
            <img class="logo" src="../img/logo.png" alt="">
            <ul>
                <li><a href="#">门店</a></li>
                <li><a href="#">我的账户</a></li>
                <li><a href="#">菜单</a></li>
            </ul>
            <img src="../img/more.png" alt="" class="more">
        &
<think>我们正在处理一个关于Vue项目在PC和移动实现响应式屏幕适配的问题。用户提供了两段引用信息,分别介绍了使用Vite和TypeScript的项目配置,以及通过PostCSS插件进行单位转换的方法。 根据引用[1]: - 在移动H5项目中,通常设置设计稿宽度为375(如iPhone 6/7/8的宽度),并提供了一个配置对象(options)用于设置转换比例和设计稿宽度。 - 在PC,设计稿宽度通常设置为1920,使用相同的配置对象。 - 对于某些特殊情况(如行内样式或第三方组件中必须使用px单位的情况),需要特殊处理。 根据引用[2]: - 使用PostCSS插件`postcss-pxtorem`进行单位转换,将px转换为rem。配置中`rootValue`设置为37.5(基于设计稿宽度375,因为375/10=37.5,即1rem=37.5px,这样10rem就是整个设计稿宽度),`propList`设置为`['*']`表示所有属性都转换。 现在,我们需要为用户提供实现PC和移动响应式屏幕适配的方法。通常有两种主流方案: 1. 使用rem单位,通过动态设置根元素的字体大小来实现适配。 2. 使用媒体查询(Media Queries)结合百分比、flex布局、grid布局等实现响应式布局。 考虑到用户的项目是基于Vue3+Vite+TS,我们可以结合引用中提到的配置,采用rem方案,并配合PostCSS插件自动转换px为rem。同时,为了处理PC和移动的不同设计稿,我们需要区分环境或使用不同的配置。 具体步骤: ### 1. 安装所需依赖 首先,安装`postcss-pxtorem`插件: ```bash npm install postcss-pxtorem --save-dev ``` ### 2. 配置PostCSS 在项目根目录下创建或修改`postcss.config.js`文件,配置`postcss-pxtorem`插件。但是,由于PC和移动的设计稿宽度不同,我们需要根据环境或条件来设置不同的`rootValue`。 然而,`postcss-pxtorem`的配置是静态的,不支持根据环境动态改变。因此,我们可以考虑以下两种方式: - 为PC和移动分别构建不同的版本(不推荐,因为通常希望一套代码适配多)。 - 使用一个动态设置`rootValue`的函数,根据当前设备的屏幕宽度来动态计算。 但是,`postcss-pxtorem`的`rootValue`可以是一个函数,这样我们就可以在编译时根据文件路径或环境变量来动态设置。然而,在运行时动态改变`rootValue`是不可能的,因为PostCSS是在构建时运行的。 因此,我们可能需要放弃在构建时区分PC和移动,而是采用一套适配方案。通常,移动采用rem适配,而PC则更多采用固定宽度+响应式布局(媒体查询)的方式,或者采用rem+媒体查询来调整根字体大小。 ### 3. 动态设置根字体大小 在项目的入口文件(如`main.ts`)中,我们可以动态设置根元素的字体大小。通常,我们会以设计稿宽度为基准,将屏幕宽度分成若干份,然后设置1rem为屏幕宽度的1/n。 例如,移动设计稿375px,我们将其分成10份,则1rem=37.5px。那么,在任意尺寸下,我们希望1rem都等于屏幕宽度的1/10。 在PC,如果设计稿是1920px,我们也可以分成10份,则1rem=192px。但是,这样在PC字体和元素会非常大,因为192px的1rem在1920px屏幕上显示正常,但在小屏幕笔记本(如1366px)上,1rem=136.6px,元素也会按比例缩小,但可能影响可读性。 因此,更常见的做法是: - 移动使用rem适配,动态设置根字体大小。 - PC使用固定宽度(如1200px)居中,或者使用媒体查询调整布局,也可以使用rem但设置一个最大宽度。 另一种方案是统一使用rem,但根据不同的屏幕宽度范围设置不同的根字体大小。例如: - 在移动(宽度小于768px),根字体大小设置为屏幕宽度的1/10。 - 在PC(宽度大于等于768px),我们可以设置一个固定的根字体大小(如16px),或者设置一个最大宽度(如1200px),并让内容居中,然后使用rem单位,但此时rem的大小是固定的,不会随屏幕宽度变化。 ### 4. 具体实现 #### 4.1 动态设置根字体大小(移动适配) 在入口文件中添加以下代码: ```javascript // 设置根字体大小函数 function setRootFontSize() { const designWidth = 375; // 移动设计稿宽度 const baseSize = 16; // 基础大小,也可以根据设计稿比例计算 const scale = document.documentElement.clientWidth / designWidth; document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'; // 限制最大缩放比例 } // 初始化 setRootFontSize(); // 窗口大小变化时重新设置 window.addEventListener('resize', setRootFontSize); ``` 但是,上述代码只考虑了移动。为了同时适配PC,我们可以修改为根据屏幕宽度范围设置不同的基准。 #### 4.2 结合媒体查询和JS动态设置 我们可以这样设计: - 在移动(小于768px):使用动态rem,根字体大小为屏幕宽度的1/10。 - 在PC(大于等于768px):设置根字体大小为固定的值(如16px),并且设置一个最大宽度(如1200px)居中显示。 ```javascript function setRootFontSize() { const clientWidth = document.documentElement.clientWidth; // 如果是移动 if (clientWidth < 768) { document.documentElement.style.fontSize = clientWidth / 10 + 'px'; } else { // PC,设置固定字体大小,比如16px document.documentElement.style.fontSize = '16px'; // 同时可以设置一个最大宽度,并居中 const container = document.getElementById('app'); if (container) { container.style.maxWidth = '1200px'; container.style.margin = '0 auto'; } } } ``` 注意:上述JS代码需要在DOM加载前执行,并且要监听窗口大小变化。 #### 4.3 配置PostCSS 在`postcss.config.js`中,我们配置`postcss-pxtorem`插件。由于在PC我们设置了固定的根字体大小(16px),而在移动根字体大小是动态的(屏幕宽度的1/10),所以我们在写样式时,需要按照设计稿的尺寸来写,然后由插件转换为rem。 但是,`postcss-pxtorem`的`rootValue`应该设置为设计稿宽度的1/10吗?实际上,`rootValue`应该设置为设计稿中1rem对应的像素值。在移动,设计稿375px,1rem=37.5px,所以`rootValue`设置为37.5。在PC,设计稿1920px,如果我们希望1rem=16px(固定),那么`rootValue`应该设置为16。 然而,我们无法在同一个配置中根据屏幕动态改变`rootValue`。因此,我们只能选择一种设计稿作为基准。通常,我们会为移动PC分别设计,然后通过媒体查询在CSS中分别写样式。或者,我们只针对移动使用rem适配,PC使用固定单位或媒体查询。 另一种折中方案:在写样式时,对于需要适配移动的元素,使用rem单位(通过PostCSS转换,配置为移动的设计稿基准),而对于PC,我们使用媒体查询覆盖这些样式,使用px或rem(固定)单位。 考虑到复杂度,我们通常这样处理: - 移动使用rem适配,配置`postcss-pxtorem`的`rootValue`为37.5。 - PC使用一套独立的样式,通过媒体查询来调整,不使用rem。 因此,在`postcss.config.js`中,我们只配置移动的转换: ```javascript module.exports = { plugins: { 'postcss-pxtorem': { rootValue: 37.5, // 移动设计稿宽度375px的1/10 propList: ['*'], // 排除不需要转换的文件,比如PC单独的样式文件(如果有的话) exclude: /node_modules|pc-components/ // 根据实际情况调整 } } } ``` 然后,在CSS中,对于PC的样式,我们可以这样写: ```css /* 移动样式(默认) */ .container { width: 10rem; /* 在375px设计稿中,10rem=375px */ } /* PC样式,通过媒体查询覆盖 */ @media screen and (min-width: 768px) { .container { width: 1200px; /* 在PC使用固定宽度 */ } } ``` ### 5. 特殊情况处理 对于某些必须使用px的情况,我们可以使用大写PX来避免转换,或者使用`postcss-pxtorem`的`selectorBlackList`或`exclude`选项来排除某些选择器或文件。也可以使用注释来禁止转换: ```css /* 不转换 */ .example { border: 1px solid #000; /* px-to-rem: ignore */ } ``` 或者,在配置中设置不转换某些属性: ```javascript propList: ['*', '!border*'] // 不转换border ``` ### 总结 实现PC和移动的响应式适配,我们采用以下方案: 1. 动态设置根字体大小:在移动(宽度<768px)设置为屏幕宽度的1/10,在PC设置为固定值(如16px)并设置最大宽度居中。 2. 使用`postcss-pxtorem`插件将样式中的px转换为rem,配置`rootValue`为37.5(移动设计稿375px的1/10),并排除PC样式文件(如果有的话)或使用媒体查询覆盖PC样式。 3. 对于PC,使用媒体查询调整布局,使用固定宽度或固定rem(基于固定根字体大小)。 4. 处理特殊情况(如echarts图表)时,避免转换或使用js根据屏幕动态调整。 ### 代码示例 #### 入口文件(main.ts)中添加根字体设置 ```typescript // 设置根字体大小 function setRootFontSize(): void { const docEl = document.documentElement; const clientWidth = docEl.clientWidth; if (clientWidth < 768) { // 移动,设置1rem = 屏幕宽度 / 10 docEl.style.fontSize = clientWidth / 10 + 'px'; } else { // PC,设置固定字体大小 docEl.style.fontSize = '16px'; // 设置最大宽度并居中 const appContainer = document.getElementById('app'); if (appContainer) { appContainer.style.maxWidth = '1200px'; appContainer.style.margin = '0 auto'; } } } // 初始化 setRootFontSize(); // 监听窗口变化 window.addEventListener('resize', setRootFontSize); ``` #### postcss.config.js配置 ```javascript module.exports = { plugins: { 'postcss-pxtorem': { rootValue: 37.5, // 移动设计稿375px,1rem=37.5px propList: ['*'], // 排除一些不需要转换的样式文件 exclude: /(node_modules)|(pc-style)/ // 根据项目实际情况调整 } } } ``` #### 样式文件示例 ```css .container { width: 10rem; /* 移动上宽度为整个屏幕宽度 */ } @media screen and (min-width: 768px) { .container { width: 100%; /* PC上宽度为100%,但受限于最大宽度1200px */ } } ``` ### 相关问题 §§相关问题§§ 1. 如何解决在PC和移动适配中echarts图表的适配问题? 2. 在响应式布局中,如何优雅地处理图片的适配? 3. Vue3项目中如何结合使用rem和vw单位进行适配?</think>### Vue项目PC与移动响应式屏幕适配方案 在Vue3 + Vite + TS项目中实现PC和移动响应式适配,核心是通过动态单位转换和媒体查询实现。以下是具体实现方案: #### 一、核心适配方案 1. **移动适配方案**(基于rem) ```javascript // postcss.config.js module.exports = { plugins: { 'postcss-pxtorem': { rootValue: 37.5, // 设计稿宽度375px的1/10 propList: ['*'], exclude: /node_modules/ // 排除第三方库 } } } ``` 2. **PC适配方案**(响应式布局) ```javascript // postcss.config.js module.exports = { plugins: { 'postcss-pxtorem': { rootValue: 192, // 设计稿宽度1920px的1/10 propList: ['*'], mediaQuery: true // 允许媒体查询中使用rem } } } ``` #### 二、动态根字体大小设置 ```typescript // utils/rem.ts export const setRem = () => { const baseSize = 16; // 基础字体大小 const designWidth = window.innerWidth < 768 ? 375 : 1920; // 区分设备 const scale = document.documentElement.clientWidth / designWidth; document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'; }; // main.ts import { setRem } from './utils/rem'; setRem(); window.addEventListener('resize', setRem); ``` #### 三、响应式布局实现 1. **CSS媒体查询** ```css /* 移动样式 (<768px) */ @media screen and (max-width: 767px) { .container { padding: 0 10px; } } /* PC样式 (≥768px) */ @media screen and (min-width: 768px) { .container { max-width: 1200px; margin: 0 auto; } } ``` 2. **Vue组件级响应** ```vue <script setup> import { ref, onMounted, onBeforeUnmount } from 'vue'; const isMobile = ref(false); const checkDevice = () => { isMobile.value = window.innerWidth < 768; }; onMounted(() => { checkDevice(); window.addEventListener('resize', checkDevice); }); onBeforeUnmount(() => { window.removeEventListener('resize', checkDevice); }); </script> <template> <div :class="['layout', { 'mobile-layout': isMobile }]"> <!-- 响应式内容 --> </div> </template> ``` #### 四、特殊场景处理 1. **强制使用px的场景**(如第三方库) ```css /* 使用大写PX避免转换 */ .third-party-component { width: 100Px !important; } ``` 2. **viewport设置** ```html <!-- index.html --> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> ``` #### 五、最佳实践 1. **设计稿规范** - 移动:375px宽设计稿 - PC:1920px宽设计稿 - 使用1:1比例标注尺寸 2. **开发工作流** ```mermaid graph TD A[设计稿标注] --> B[px单位开发] B --> C[PostCSS自动转rem] C --> D[动态根字体大小] D --> E[多设备测试] ``` 3. **TS配置补充** ```json // tsconfig.node.json { "include": [ "vite.config.*", "src/plugins/*", "postcss.config.*" // 添加PostCSS配置支持 ] } ``` [^1]: 引用[1]: vue3 + VIte + TS 移动 H5 项目屏幕适配,PC响应式布局 [^2]: 引用[2]: 手把手教你实现VuePC,移动适配
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值