feat(utils): 添加设备检测和自适应样式工具函数
新增getDeviceInfo函数用于检测设备类型和屏幕信息 添加getAdaptiveFontSize和getAdaptivePadding函数实现样式自适应 在map.vue中应用自适应样式函数优化标记显示效果
Showing
2 changed files
with
168 additions
and
4 deletions
| ... | @@ -77,4 +77,92 @@ const strExist = (array, str) => { | ... | @@ -77,4 +77,92 @@ const strExist = (array, str) => { |
| 77 | return exist.length > 0 | 77 | return exist.length > 0 |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | -export { formatDate, wxInfo, hasEllipsis, parseQueryString, strExist }; | 80 | +/** |
| 81 | + * @description 获取设备类型和屏幕信息 | ||
| 82 | + * @returns {Object} 设备信息对象 | ||
| 83 | + */ | ||
| 84 | +const getDeviceInfo = () => { | ||
| 85 | + const userAgent = navigator.userAgent; | ||
| 86 | + const screenWidth = window.innerWidth || document.documentElement.clientWidth; | ||
| 87 | + const screenHeight = window.innerHeight || document.documentElement.clientHeight; | ||
| 88 | + | ||
| 89 | + // 检测iPad | ||
| 90 | + const isiPad = /iPad/.test(userAgent) || | ||
| 91 | + (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1) || | ||
| 92 | + (screenWidth >= 768 && screenWidth <= 1024); | ||
| 93 | + | ||
| 94 | + // 检测微信环境 | ||
| 95 | + const isWeChat = /MicroMessenger/i.test(userAgent); | ||
| 96 | + | ||
| 97 | + // 检测移动设备 | ||
| 98 | + const isMobile = /Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent); | ||
| 99 | + | ||
| 100 | + // 检测平板设备(包括iPad) | ||
| 101 | + const isTablet = isiPad || (screenWidth >= 768 && screenWidth <= 1024); | ||
| 102 | + | ||
| 103 | + // 检测大屏设备 | ||
| 104 | + const isLargeScreen = screenWidth >= 1024; | ||
| 105 | + | ||
| 106 | + return { | ||
| 107 | + isiPad, | ||
| 108 | + isWeChat, | ||
| 109 | + isMobile, | ||
| 110 | + isTablet, | ||
| 111 | + isLargeScreen, | ||
| 112 | + screenWidth, | ||
| 113 | + screenHeight, | ||
| 114 | + userAgent | ||
| 115 | + }; | ||
| 116 | +}; | ||
| 117 | + | ||
| 118 | +/** | ||
| 119 | + * @description 根据设备类型获取适配的字体大小 | ||
| 120 | + * @param {number} baseFontSize 基础字体大小(rem) | ||
| 121 | + * @returns {string} 适配后的字体大小 | ||
| 122 | + */ | ||
| 123 | +const getAdaptiveFontSize = (baseFontSize = 0.8) => { | ||
| 124 | + const deviceInfo = getDeviceInfo(); | ||
| 125 | + | ||
| 126 | + // 字体大小倍数配置 | ||
| 127 | + let fontSizeMultiplier = 1; | ||
| 128 | + | ||
| 129 | + if (deviceInfo.isWeChat && deviceInfo.isLargeScreen) { | ||
| 130 | + // 微信小程序大屏环境 | ||
| 131 | + fontSizeMultiplier = 1.8; | ||
| 132 | + } else if (deviceInfo.isiPad || deviceInfo.isTablet) { | ||
| 133 | + // iPad或平板设备 | ||
| 134 | + fontSizeMultiplier = 1.5; | ||
| 135 | + } else if (deviceInfo.isLargeScreen) { | ||
| 136 | + // 大屏设备(桌面) | ||
| 137 | + fontSizeMultiplier = 1.6; | ||
| 138 | + } else if (deviceInfo.screenWidth >= 481 && deviceInfo.screenWidth <= 768) { | ||
| 139 | + // 中等屏幕设备 | ||
| 140 | + fontSizeMultiplier = 1.2; | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + const adaptedSize = baseFontSize * fontSizeMultiplier; | ||
| 144 | + return `${adaptedSize}rem`; | ||
| 145 | +}; | ||
| 146 | + | ||
| 147 | +/** | ||
| 148 | + * @description 根据设备类型获取适配的内边距 | ||
| 149 | + * @param {string} basePadding 基础内边距 | ||
| 150 | + * @returns {string} 适配后的内边距 | ||
| 151 | + */ | ||
| 152 | +const getAdaptivePadding = (basePadding = '.5rem .2rem .5rem .2rem') => { | ||
| 153 | + const deviceInfo = getDeviceInfo(); | ||
| 154 | + | ||
| 155 | + if (deviceInfo.isWeChat && deviceInfo.isLargeScreen) { | ||
| 156 | + return '1rem .4rem 1rem .4rem'; | ||
| 157 | + } else if (deviceInfo.isiPad || deviceInfo.isTablet) { | ||
| 158 | + return '.8rem .3rem .8rem .3rem'; | ||
| 159 | + } else if (deviceInfo.isLargeScreen) { | ||
| 160 | + return '.9rem .35rem .9rem .35rem'; | ||
| 161 | + } else if (deviceInfo.screenWidth >= 481 && deviceInfo.screenWidth <= 768) { | ||
| 162 | + return '.6rem .25rem .6rem .25rem'; | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + return basePadding; | ||
| 166 | +}; | ||
| 167 | + | ||
| 168 | +export { formatDate, wxInfo, hasEllipsis, parseQueryString, strExist, getDeviceInfo, getAdaptiveFontSize, getAdaptivePadding }; | ... | ... |
| ... | @@ -107,7 +107,7 @@ import audioBackground1 from '@/components/audioBackground1.vue' | ... | @@ -107,7 +107,7 @@ import audioBackground1 from '@/components/audioBackground1.vue' |
| 107 | import BottomNav from '@/components/BottomNav.vue' | 107 | import BottomNav from '@/components/BottomNav.vue' |
| 108 | import { mapState, mapActions } from 'pinia' | 108 | import { mapState, mapActions } from 'pinia' |
| 109 | import { mainStore } from '@/store' | 109 | import { mainStore } from '@/store' |
| 110 | -import { parseQueryString } from '@/utils/tools' | 110 | +import { parseQueryString, getAdaptiveFontSize, getAdaptivePadding } from '@/utils/tools' |
| 111 | import AMapLoader from '@amap/amap-jsapi-loader' | 111 | import AMapLoader from '@amap/amap-jsapi-loader' |
| 112 | import { mapAudioAPI } from '@/api/map.js' | 112 | import { mapAudioAPI } from '@/api/map.js' |
| 113 | 113 | ||
| ... | @@ -197,6 +197,82 @@ export default { | ... | @@ -197,6 +197,82 @@ export default { |
| 197 | */ | 197 | */ |
| 198 | zoom() { | 198 | zoom() { |
| 199 | return this.data_zoom; | 199 | return this.data_zoom; |
| 200 | + }, | ||
| 201 | + /** | ||
| 202 | + * 动态计算适配的标记样式 - 选中状态(垂直) | ||
| 203 | + * @returns {Object} 适配后的样式对象 | ||
| 204 | + */ | ||
| 205 | + adaptiveMarkerStyle2() { | ||
| 206 | + return { | ||
| 207 | + "padding": getAdaptivePadding(".5rem .2rem .5rem .2rem"), | ||
| 208 | + "border-color": "#DD7850", | ||
| 209 | + "border-radius": ".25rem", | ||
| 210 | + "background-color": "#FFF", | ||
| 211 | + "font-size": getAdaptiveFontSize(0.8), | ||
| 212 | + "color": "#DD7850", | ||
| 213 | + "writing-mode": "vertical-rl", | ||
| 214 | + "text-orientation": "mixed", | ||
| 215 | + "display": "flex", | ||
| 216 | + "justify-content": "center", | ||
| 217 | + "align-items": "center", | ||
| 218 | + }; | ||
| 219 | + }, | ||
| 220 | + /** | ||
| 221 | + * 动态计算适配的标记样式 - 未选中状态(垂直) | ||
| 222 | + * @returns {Object} 适配后的样式对象 | ||
| 223 | + */ | ||
| 224 | + adaptiveMarkerStyle1() { | ||
| 225 | + return { | ||
| 226 | + "padding": getAdaptivePadding(".5rem .2rem .5rem .2rem"), | ||
| 227 | + "border-color": "#fcfbfa", | ||
| 228 | + "border-radius": ".25rem", | ||
| 229 | + "background-color": "#DD7850", | ||
| 230 | + "font-size": getAdaptiveFontSize(0.8), | ||
| 231 | + "color": "white", | ||
| 232 | + "writing-mode": "vertical-rl", | ||
| 233 | + "text-orientation": "mixed", | ||
| 234 | + "display": "flex", | ||
| 235 | + "justify-content": "center", | ||
| 236 | + "align-items": "center", | ||
| 237 | + }; | ||
| 238 | + }, | ||
| 239 | + /** | ||
| 240 | + * 动态计算适配的标记样式 - 选中状态(水平) | ||
| 241 | + * @returns {Object} 适配后的样式对象 | ||
| 242 | + */ | ||
| 243 | + adaptiveMarkerStyle2Horizontal() { | ||
| 244 | + return { | ||
| 245 | + "padding": getAdaptivePadding(".2rem .5rem .2rem .5rem"), | ||
| 246 | + "border-color": "#DD7850", | ||
| 247 | + "border-radius": ".25rem", | ||
| 248 | + "background-color": "#FFF", | ||
| 249 | + "font-size": getAdaptiveFontSize(0.8), | ||
| 250 | + "color": "#DD7850", | ||
| 251 | + "writing-mode": "horizontal", | ||
| 252 | + "text-orientation": "mixed", | ||
| 253 | + "display": "flex", | ||
| 254 | + "justify-content": "center", | ||
| 255 | + "align-items": "center", | ||
| 256 | + }; | ||
| 257 | + }, | ||
| 258 | + /** | ||
| 259 | + * 动态计算适配的标记样式 - 未选中状态(水平) | ||
| 260 | + * @returns {Object} 适配后的样式对象 | ||
| 261 | + */ | ||
| 262 | + adaptiveMarkerStyle1Horizontal() { | ||
| 263 | + return { | ||
| 264 | + "padding": getAdaptivePadding(".2rem .5rem .2rem .5rem"), | ||
| 265 | + "border-color": "#fcfbfa", | ||
| 266 | + "border-radius": ".25rem", | ||
| 267 | + "background-color": "#DD7850", | ||
| 268 | + "font-size": getAdaptiveFontSize(0.8), | ||
| 269 | + "color": "white", | ||
| 270 | + "writing-mode": "horizontal", | ||
| 271 | + "text-orientation": "mixed", | ||
| 272 | + "display": "flex", | ||
| 273 | + "justify-content": "center", | ||
| 274 | + "align-items": "center", | ||
| 275 | + }; | ||
| 200 | } | 276 | } |
| 201 | }, | 277 | }, |
| 202 | data() { | 278 | data() { |
| ... | @@ -476,10 +552,10 @@ export default { | ... | @@ -476,10 +552,10 @@ export default { |
| 476 | // 根据选中状态和文字方向选择样式 | 552 | // 根据选中状态和文字方向选择样式 |
| 477 | let textStyle, containerStyle; | 553 | let textStyle, containerStyle; |
| 478 | if (textDirection === 'vertical') { | 554 | if (textDirection === 'vertical') { |
| 479 | - textStyle = isSelected ? this.markerStyle2 : this.markerStyle1; | 555 | + textStyle = isSelected ? this.adaptiveMarkerStyle2 : this.adaptiveMarkerStyle1; |
| 480 | containerStyle = 'flex-direction: column; align-items: center;'; | 556 | containerStyle = 'flex-direction: column; align-items: center;'; |
| 481 | } else { | 557 | } else { |
| 482 | - textStyle = isSelected ? this.markerStyle2_horizontal : this.markerStyle1_horizontal; | 558 | + textStyle = isSelected ? this.adaptiveMarkerStyle2Horizontal : this.adaptiveMarkerStyle1Horizontal; |
| 483 | containerStyle = 'flex-direction: row; align-items: center;'; | 559 | containerStyle = 'flex-direction: row; align-items: center;'; |
| 484 | } | 560 | } |
| 485 | 561 | ... | ... |
-
Please register or login to post a comment