惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
U
Unit 42
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Full Disclosure
月光博客
月光博客
Engineering at Meta
Engineering at Meta
博客园_首页
The Register - Security
The Register - Security
G
Google Developers Blog
The Cloudflare Blog
博客园 - Franky
K
Kaspersky official blog
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
C
Check Point Blog
NISL@THU
NISL@THU
AI
AI
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Vercel News
Vercel News
T
Tor Project blog
P
Privacy International News Feed
D
Docker
I
Intezer
L
LangChain Blog
P
Proofpoint News Feed
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
博客园 - 聂微东
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
V
V2EX
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog

博客园 - 蓓蕾心晴

语雀思维导图如何导入到飞书文档 js 实现点击触发复制口令到剪贴板,并跳转 css 背景模糊在真机测试会出现黑色蒙层闪现问题解决 华为鸿蒙手机通过Chrome DevTools调试App内WebView页面 vscode左侧搜索栏搜索时排除不参与搜索的文件夹 css动画已经执行过一次如何再次执行? vscode设置单击选中带连字符的单词 移动端盒子元素实现左右可滑动且竖向页面可滑动 js 判断设备类型包括异形屏 element ui 日期组件实现仅显示日期选择但值包含固定的时间 master远端代码更新,本地拉取不到 css 判断在支持某些属性的情况下再添加样式 vue3 provide的值 在回调函数中改变,inject 如何获取到最新的值? vue3如何将 app 全局变量对象变为响应式并监听到某个属性的改变 ResizeObserver loop completed with undelivered notifications. 报错 git 修改本地仓库的远程仓库地址 git突然无法推送到远程仓库 css实现图片等比例完全展示,背景加图片 200%放大虚化 element-ui 使用 el-date-picker 如何限制时间选择范围? element-ui 使用 el-date-picker 如何监听数据变更?
css 实现刘海屏样式兼容并支持 js 获取刘海屏高度后动态修改
蓓蕾心晴 · 2024-11-27 · via 博客园 - 蓓蕾心晴

css

:root {
  --safe-area-inset-top: 0px;
  --safe-area-inset-right: 0px;
  --safe-area-inset-bottom: 0px;
  --safe-area-inset-left: 0px;
  --safe-area-inset-constant-top: 0px;
  --safe-area-inset-constant-right: 0px;
  --safe-area-inset-constant-bottom: 0px;
  --safe-area-inset-constant-left: 0px;
}

@supports (top: env(safe-area-inset-top, 44px)) {
  :root {
    --safe-area-inset-top: env(safe-area-inset-top, 44px);
    --safe-area-inset-right: env(safe-area-inset-right, 34px);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 34px);
    --safe-area-inset-left: env(safe-area-inset-left, 34px);
  }
}

@supports (top: constant(safe-area-inset-top)) {
  :root {
    --safe-area-inset-constant-top: constant(safe-area-inset-top);
    --safe-area-inset-constant-right: constant(safe-area-inset-right);
    --safe-area-inset-constant-bottom: constant(safe-area-inset-bottom);
    --safe-area-inset-constant-left: constant(safe-area-inset-left);
  }
}

首先设置 css 根属性变量值,如果是 less,通过 calc 计算出增加刘海屏高度后的值,注意 calc 计算不支持不带单位的数字相加,会导致结果为 0 ,所以一定要处理不带单位的场景。

由于安卓不支持 constant css 函数,以及安卓 9 以下低版本系统不支持 env css 函数,会导致获取的结果为 0 从而导致 calc 计算结果也为 0 ,所以要在初始化写为 0px,则通过 css  @supports 来判断支持 constant 和 env 函数的情况下再赋值刘海屏高度值。

js 通过与iOS 和安卓的接口获取到客户端返回的实际刘海屏的高度,当返回的高度存在时,则重新赋值 root 跟元素的变量,否则用浏览器默认的。

这样实现的目的是为了解决部分机型下,env 函数和 constant 函数都获取失败导致无法处理刘海屏高度的场景。

js vue3 代码

import { readonly, reactive, watch, ref } from 'vue'
import { setRootProperty } from '@/common/util'

export default {
  install: (app) => {
    const config = reactive({})

    const insets = ref({ top: 0, left: 0, bottom: 0, right: 0 })
    if (window.getSafeAreaInsets && typeof window.getSafeAreaInsets === 'function') {
      insets.value = window.getSafeAreaInsets() // 初始化获取刘海屏值
    }
    const setConfigAreaInsets = () => {
      config.areaInsets = insets.value
    }
    const updateInsets = (top, left, bottom, right) => {
      insets.value = { top, left, bottom, right }
    }

    function applySafeInsets() {
      if (!window.EVENTS.safeAreaInsetsChange) {
        return false
      }
      setConfigAreaInsets()
      ;['top', 'right', 'bottom', 'left'].forEach(prop => {
        setRootProperty(`--safe-area-inset-${prop}`, `${insets.value[prop]}px`)
        setRootProperty(`--safe-area-inset-constant-${prop}`, `${insets.value[prop]}px`)
      })
    }
    watch(insets, (val) => {
      applySafeInsets()
    })
    app.mixin({
      mounted() {
        applySafeInsets()
        if (this === this.$root) {
          window.mraid.addEventListener('safeAreaInsetsChange', updateInsets)
        }
      },
      beforeUnmount() {
        if (this === this.$root) {
          window.mraid.removeEventListener('safeAreaInsetsChange', updateInsets)
        }
      }
    })

    app.config.globalProperties.$config = config
    app.provide('config', readonly(config))
  }
}