移动端始终横屏及rem 适配

本文介绍了一种通过CSS和JavaScript实现的移动端始终横屏显示的方法,并结合REM单位进行页面布局适配的技术方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


移动端始终横屏及rem 适配

      这几天做一个项目需要始终横屏,然而找了许多资料,看到H5网页感觉并不能直接控制始终横屏,而是需要使用css3配合JS控制横屏,后面因为适配原因用到rem,于是为了方便和学习,在这结合资料记一个学习笔记。
下面上demo:
css代码:

html{color:#000;background:#FFF}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0}table{border-collapse:collapse;border-spacing:0}fieldset,img{border:0}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal}ol,ul{list-style:none}caption,th{text-align:left}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal}q:before,q:after{content:''}abbr,acronym{border:0;font-variant:normal}sup{vertical-align:text-top}sub{vertical-align:text-bottom}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;*font-size:100%}legend{color:#000}#yui3-css-stamp.cssreset{display:none}
html{-webkit-tap-highlight-color:rgba(255,255,255,0);
-webkit-appearance:none;
-webkit-transform:translate3d(0, 0, 0);}
img{ width: 100%; display: block;}

/* main css */
body{ width: 100%; height: 100%; position: relative; left:0; top: 0; background: #000; overflow: hidden;}
#wrap{ height: 100%; position: absolute; left: 50%; top:50%; overflow: hidden;}
.content{ width: 1000px; height: 640px; color: #000; position: absolute; left:50%; top:50%;}
.pageWrap{ width: 100%; height: 100%; position: absolute; overflow: hidden; left: 0; top:0;transform: translate3d(100%, 0, 0); -webkit-transform: translate3d(100%, 0, 0);}
.pageWrap{ background: #42a8fe url(../img/mzBg.png) no-repeat center bottom; background-size:cover; transform: translate3d(0, 0, 0); -webkit-transform: translate3d(0, 0, 0);}



html代码:

rel="stylesheet" href="css/main1.css"> 横屏测试

测试的内容11111111111


js代码:
 1.横屏 
var wrap = $('#wrap');

var ww = window.innerWidth;
var wh = window.innerHeight;
var sec = $('.pageWrap'),
	canswipe = ww*0.1,
	hadDown = false;

$('body').css({
	'width':ww+'px',
	'height':wh+'px'
});
changeScene();



window.onresize = function(){
	canswipe = ww*0.1;
	//调整画布的位置和大小
	changeScene();
}

function changeScene(){
	var iniW = 1000,
		iniH = 640,
		tarW = 0,
		tarH = 0;
	ww = window.innerWidth;
	wh = window.innerHeight;

	
	if(ww > wh){		//如果浏览器的宽度大于高度,说明是横屏的,画布的宽度 == 浏览器的宽度
		console.log('浏览器的宽度大于高度');
		tarW = ww;
		tarH = tarW*iniH/iniW;
		$('body').css({
			'width':ww+'px',
			'height':wh+'px'
		});

		wrap.css({
			'width':ww + 'px',
			'height':wh + 'px',
			'transform':'translate3d(-50%,-50%,0)',
			'-webkit-transform':'translate3d(-50%,-50%,0)'
		});
		

	}else if(ww <= wh){	//如果浏览器的宽度小于高度,说明是竖的,画布的宽度 == 浏览器的高度
		console.log('浏览器的宽度小于高度');
		tarW = wh;
		tarH = tarW*iniH/iniW;

		$('body').css({
			'width':ww+'px',
			'height':wh+'px'
		});


		wrap.css({
			'width':wh + 'px',
			'height':ww + 'px',
			'transform':'translate3d(-50%,-50%,0) rotate(90deg)',
			'-webkit-transform':'translate3d(-50%,-50%,0) rotate(90deg)'
		});

		
	}

}







 2.rem适配
 
(function (doc, win) {
var docEl = doc.documentElement,
    resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
    recalc = function () {
    	if(docEl.clientWidth>docEl.clientHeight){
    		var clientWidth = docEl.clientHeight;
    	}else{
    		var clientWidth = docEl.clientWidth;
    	}
//    var clientWidth = docEl.clientWidth;
      if (!clientWidth) return;
      docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
    };
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

以上就是这些内容了。
<think>嗯,用户问的是如何在Vue3项目中实现移动端到大适配方案。首先,我需要回忆一下移动端适配的常用方法。根据之前的引用内容,用户可能已经了解了一些rem适配方案,比如rem配合媒体查询、js动态设置或者使用VW。但这次的问题是要适配移动端到大,可能需要更灵活的响应式设计。 首先,我应该考虑Vue3的特性,比如组合式API和响应式工具,可能可以用来动态调整布局。然后,适配方案可能需要结合多种方法,比如使用rem或vw作为单位,同时利用媒体查询来针对不同幕尺寸应用不同的样式。另外,大可能需要更多的布局调整,比如弹性布局或者栅格系统,这时候可能需要像Tailwind CSS这样的工具来简化响应式设计。 接着,用户可能需要的不仅仅是基本的rem适配,还需要元素尺寸的动态缩放。这时候,使用JavaScript监听窗口变化,动态计算根字体大小可能有效。同时,像scale-box这样的组件可能对大适配有帮助,特别是在保持比例的情况下缩放内容。 另外,图片和媒体的适配也很重要,可能需要使用响应式图片,或者像v-lazy这样的懒加载插件来优化性能。视口单位(vw/vh)和百分比布局的结合使用可能更灵活,适应不同幕尺寸。 还要考虑第三方库的支持,比如lib-flexible或者postcss-px-to-viewport,这些工具可以自动转换单位,简化开发流程。同时,自定义指令可能在需要动态调整元素尺寸时派上用场,比如根据幕大小改变字体或间距。 最后,测试环节不可忽视,需要确保不同设备和幕尺寸下的显示效果一致,可能推荐使用浏览器开发者工具进行多设备模拟测试。 需要综合这些方法,提供一个分步骤的解决方案,涵盖基础配置、动态缩放、布局调整、第三方工具和测试建议。同时,要确保回答结构清晰,步骤明确,帮助用户逐步实现适配。</think>### Vue3移动端到大适配方案分步解析 在Vue3项目中实现移动端到大适配,需要结合响应式布局与动态缩放策略。以下是经过验证的完整解决方案: #### 一、基础适配方案 1. **视口单位+rem混合方案** ```javascript // main.js const setRootFontSize = () => { const baseWidth = 375 // 以iPhone6/7/8为基准 const scale = document.documentElement.clientWidth / baseWidth document.documentElement.style.fontSize = `${Math.min(scale, 4) * 100}px` // 限制最大缩放比例 } window.addEventListener('resize', setRootFontSize) setRootFontSize() ``` ```css /* 使用混合单位 */ .container { padding: 0.2rem 0.1vw; /* 向用vw保持流动性,纵向用rem保持比例 */ font-size: clamp(14px, 0.16rem + 0.5vw, 24px); /* 动态字体大小 */ } ``` 该方案结合了rem的等比缩放和vw的视口响应特性[^2] 2. **响应式断点配置** ```javascript // breakpoints.js export const BREAKPOINTS = { MOBILE: 375, TABLET: 768, DESKTOP: 1280, LARGE_SCREEN: 1920 } ``` #### 二、大适配增强方案 1. **动态缩放容器** ```vue <template> <div ref="scaleContainer" class="scale-wrapper"> <slot></slot> </div> </template> <script setup> import { ref, onMounted, onBeforeUnmount } from 'vue' const scaleContainer = ref(null) const baseWidth = 1920 // 设计稿基准宽度 const updateScale = () => { const { clientWidth } = document.documentElement const scale = clientWidth / baseWidth scaleContainer.value.style.transform = `scale(${scale}) translateX(-50%)` scaleContainer.value.style.width = `${baseWidth}px` } onMounted(() => { window.addEventListener('resize', updateScale) updateScale() }) onBeforeUnmount(() => { window.removeEventListener('resize', updateScale) }) </script> <style> .scale-wrapper { position: fixed; left: 50%; top: 0; transform-origin: 0 0; transition: transform 0.3s; } </style> ``` 2. **栅格系统配置** ```css /* 响应式栅格 */ .grid-container { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: clamp(10px, 1vw, 30px); } @media (min-width: 1280px) { .grid-container { grid-template-columns: repeat(4, 1fr); } } ``` #### 三、进阶优化方案 1. **PostCSS自动转换** ```javascript // postcss.config.js module.exports = { plugins: { 'postcss-px-to-viewport': { unitToConvert: 'px', viewportWidth: 375, // 移动端基准 viewportHeight: 667, unitPrecision: 5, propList: ['*'], viewportUnit: 'vw', fontViewportUnit: 'vw', selectorBlackList: [], minPixelValue: 1, mediaQuery: false, replace: true, exclude: [/node_modules/], landscape: false, landscapeUnit: 'vw', landscapeWidth: 1136 } } } ``` 2. **混合单位策略** ```css /* 弹性布局示例 */ .card { width: min(90vw, 1200px); /* 限制最大宽度 */ height: calc(100vh - 60px); /* 结合视口单位 */ margin: 0 auto; padding: max(2vh, 20px); } ``` #### 四、实践建议 1. **设备方向处理** ```javascript // 检测 const checkOrientation = () => { if (window.matchMedia("(orientation: portrait)").matches) { document.body.classList.add('portrait') document.body.classList.remove('landscape') } else { document.body.classList.add('landscape') document.body.classList.remove('portrait') } } ``` 2. **性能优化** ```vue <template> <img :src="responsiveImage.url" :srcset="responsiveImage.srcset" sizes="(max-width: 768px) 100vw, 50vw" alt="responsive image" > </template> <script setup> import { computed } from 'vue' const props = defineProps({ imageSet: Object }) const responsiveImage = computed(() => ({ url: props.imageSet.mobile, srcset: ` ${props.imageSet.mobile} 375w, ${props.imageSet.tablet} 768w, ${props.imageSet.desktop} 1280w ` })) </script> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值