当前位置:网站首页>移动端常用解决方案
移动端常用解决方案
2022-07-01 05:21:00 【带脑子的CV工程师】
1、resize监听
var resizeTimer = null;
$(window).resize(function(){
if(resizeTimer){
clearTimeout(resizeTimer);
}
resizeTimer = setTimeout(function(){
console.log("窗口改变")
},100)
})
2、ResizeObserver
监听元素宽度的变化
const objResizeObserver = new ResizeObserver(entries => {
const entry = entries[0];
// dom属性
const cr = entry.contentRect;
setTimeout(() => {
const distanceW = Math.abs(cr.width - oldScreen.width) > 100;
load = cr.width === window.innerWidth;
if (distanceW && load) {
load = false;
oldScreen.width = window.innerWidth;
onOrientation();
} else {
onResizeRem();
}
}, 100);
});
监听不同设备
const ua = useUa();
const isLark = /lark/i.test(ua);
const isIpad = /ipad/i.test(ua);
const isTouch = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
飞书上的ipad
if (isLark && isIpad && isTouch)
监听横竖屏切换
window.addEventListener('orientationchange', onOrientation);
处理根字体大小的变化
export const useRem = (): void => {
// const [rem, setRem] = useState<number>(100);
function setRem() {
// setRem((window.innerWidth / 414) * 100);
const fontSize = (window.innerWidth / 375) * 100;
document.documentElement.style.fontSize = `${
fontSize}px`;
}
useEffect(() => {
setRem();
window.addEventListener('resize', setRem);
window.addEventListener('orientationchange', onOrientation);
return () => {
window.removeEventListener('resize', setRem);
window.removeEventListener('orientationchange', onOrientation);
document.documentElement.style.fontSize = '16px';
};
}, []);
};
监听手机端字体大小
设置根字体大小变化放在useEffect里面
会造成,页面初始化显示,根字体还没计算好
元素先缩然后放大的问题
放在页面的头部
例如react
<Helmet>
<script>{
SCRIPT_CONTENT}</script>{
' '}
</Helmet>
在手机上,
没有兼容横屏,所以期望横屏也用竖屏的根字体大小
iPad还好
const ua = useUa();
const isAnd = /android/i.test(ua);
// const isIphone = /iphone/i.test(ua);
const isIpad = /ipad/i.test(ua);
const isIpadsafari = ua.includes('Safari') && !ua.includes('iPhone');
const isMobile = isIpad || (isIpadsafari && !isAnd);
const SCRIPT_CONTENT = `document.documentElement.style.fontSize = ${
isMobile ? window.innerWidth : 375
} / 375 * 100 + "px";`;
ua
const isMobile = /(ipad|iphone|ipod|windows phone|android)/i.test(navigator.userAgent);
边栏推荐
- Thread safety issues
- Actual combat: gateway api-2022.2.13
- Global and Chinese market for instant messaging security and compliance solutions 2022-2028: Research Report on technology, participants, trends, market size and share
- Like cloud functions
- 在Rainbond中一键部署高可用 EMQX 集群
- LRU cache for leveldb source code analysis
- Thread process foundation of JUC
- Copy baby prompt: material cannot be empty. How to solve it?
- Simple read / write verification of qdatastream
- Solution: drag the Xib control to the code file, and an error setvalue:forundefined key:this class is not key value coding compliant for the key is reported
猜你喜欢

Rainbond结合NeuVector实践容器安全管理

Redis数据库的部署及常用命令

Memtable for leveldb source code analysis

Numeric amount plus comma; JS two methods of adding three digits and a comma to numbers; JS data formatting

tar命令

Tar command
![[data recovery in North Asia] a data recovery case of raid crash caused by hard disk drop during data synchronization of hot spare disk of RAID5 disk array](/img/22/606ff1e8dad3d5896b32d2146b0477.jpg)
[data recovery in North Asia] a data recovery case of raid crash caused by hard disk drop during data synchronization of hot spare disk of RAID5 disk array

Go learning notes (5) basic types and declarations (4)

Thread process foundation of JUC

提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
随机推荐
Numeric amount plus comma; JS two methods of adding three digits and a comma to numbers; JS data formatting
Cockroachdb: the resistant geo distributed SQL database paper reading notes
Actual combat: gateway api-2022.2.13
TypeORM 框架
第05天-文件操作函数
Global and Chinese market of search engine optimization (SEO) software 2022-2028: Research Report on technology, participants, trends, market size and share
每日一题-LeetCode1175-质数排列-数学
Set集合详细讲解
担心侵权?必备无版权素材网站分享,不用担心视频剪辑缺素材
如何创建一个根据进度改变颜色的进度条
FileOutPutStream
0xc000007b the application cannot start the solution normally (the pro test is valid)
Character input stream and character output stream
Detailed explanation of distributed global unique ID solution
And search: the suspects (find the number of people related to the nth person)
Serialization and deserialization of objects
Youqitong [vip] v3.7.2022.0106 official January 22 Edition
Global and Chinese market of mainboard 2022-2028: Research Report on technology, participants, trends, market size and share
STM32 expansion board digital tube display
Software intelligence: the "world" and "boundary" of AI sentient beings in AAAs system