CSS3的aria-hidden学习

[复制链接]
发表于 2025-11-9 23:38:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
前言

aria-hidden 属性可用于埋伏非交互内容,使其在无停滞 API 中不可见。即当aria-hidden="true" 添加到一个元素会将该元素及其全部子元素从无停滞树中移除,这可以通过埋伏来改善辅助技能用户的体验:

  • 纯装饰性内容,如图标或图片
  • 重复的内容,如重复的文本
  • 屏幕外或折叠的内容,如菜单
tips: 不要在可以得到核心的元素上利用 aria-hidden="true"。因元素的埋伏状态基于其是否被渲染通常由 CSS 控制。
  1. [aria-hidden="true"] {
  2.   display: none;
  3. }
复制代码
弹窗显隐demo

  1. <template>
  2.   <!-- 弹窗内容 -->
  3.   <div id="myModal" class="modal" aria-hidden="true" style="display: none;">
  4.     <div class="modal-content">
  5.       <span class="close-button" @click="closeModal">&times;</span>
  6.       <p>这是一个弹窗内容。</p>
  7.     </div>
  8.   </div>
  9.   <!-- 触发弹窗的按钮 -->
  10.   <button @click="openModal">打开弹窗</button>
  11. </template>
  12. <script setup>
  13. const openModal = () => {
  14.   var modal = document.getElementById('myModal');
  15.   modal.style.display = 'flex'; // 显示弹窗
  16.   modal.setAttribute('aria-hidden', 'false'); // 使弹窗对辅助技术可见
  17. }
  18. const closeModal = () => {
  19.   var modal = document.getElementById('myModal');
  20.   modal.style.display = 'none'; // 显示弹窗
  21.   modal.setAttribute('aria-hidden', 'true'); // 使弹窗对辅助技术可见
  22. }
  23. </script>
  24. <style lang="scss">
  25. .modal {
  26.   position: fixed;
  27.   left: 0;
  28.   top: 0;
  29.   width: 100%;
  30.   height: 100%;
  31.   background-color: rgba(0, 0, 0, 0.5);
  32.   display: flex;
  33.   justify-content: center;
  34.   align-items: center;
  35. }
  36. .modal-content {
  37.   background-color: #fff;
  38.   padding: 20px;
  39.   border-radius: 5px;
  40.   position: relative;
  41. }
  42. .close-button {
  43.   position: absolute;
  44.   top: 10px;
  45.   right: 10px;
  46.   cursor: pointer;
  47.   font-size: 20px;
  48. }
  49. </style>
复制代码
input获取核心提示demo

  1. <template>
  2.   <div class="input-container">
  3.     <input type="text" v-model="inputValue" :aria-describedby="tooltipId" @focus="showTooltip = true"
  4.       @blur="showTooltip = false" />
  5.     <transition name="fade">
  6.       <div v-show="showTooltip" class="tooltip" :id="tooltipId" ref="tooltip" role="tooltip">
  7.         这是当输入聚焦时出现的工具提示。
  8.       </div>
  9.     </transition>
  10.   </div>
  11. </template>
  12. <script>
  13. import { ref, computed } from 'vue';
  14. export default {
  15.   setup () {
  16.     const inputValue = ref('');
  17.     const showTooltip = ref(false);
  18.     const tooltip = ref(null);
  19.     const tooltipId = computed(() => showTooltip.value ? 'tooltip' : '');
  20.     return {
  21.       inputValue,
  22.       showTooltip,
  23.       tooltip,
  24.       tooltipId
  25.     };
  26.   }
  27. };
  28. </script>
  29. <style scoped>
  30. .input-container {
  31.   position: relative;
  32.   display: inline-block;
  33. }
  34. .tooltip {
  35.   position: absolute;
  36.   bottom: 100%;
  37.   left: 0;
  38.   top: 25px;
  39.   background-color: green;
  40.   border: 1px solid green;
  41.   color: #fff;
  42.   padding: 10px;
  43.   border-radius: 5px;
  44.   height: 80px;
  45.   z-index: 999;
  46.   width: 200px;
  47. }
  48. .fade-enter-active,
  49. .fade-leave-active {
  50.   transition: opacity 0.1s ease-in-out;
  51. }
  52. .fade-enter-from,
  53. .fade-leave-to {
  54.   opacity: 0;
  55. }
  56. </style>
复制代码

追更

  1. <template>
  2.   <input type="text" v-model="inputValue" @focus="showTooltip" @blur="hideTooltip" placeholder="单击或聚焦我" ref="inputRef">
  3.   <transition name="fade">
  4.     <div v-if="isTooltipVisible" class="tooltip" :style="tooltipStyle">这是一个工具提示!</div>
  5.   </transition>
  6. </template>
  7. <script setup>
  8. import { ref, onMounted, reactive } from 'vue';
  9. const inputValue = ref('');
  10. const isTooltipVisible = ref(false);
  11. const inputRef = ref(null);
  12. const tooltipStyle = reactive({
  13.   left: '0px',
  14.   top: '0px'
  15. });
  16. const showTooltip = () => {
  17.   isTooltipVisible.value = true;
  18.   positionTooltip();
  19. };
  20. const hideTooltip = () => {
  21.   isTooltipVisible.value = false;
  22. };
  23. const positionTooltip = () => {
  24.   if (inputRef.value) {
  25.     const inputRect = inputRef.value.getBoundingClientRect();
  26.     tooltipStyle.left = `${inputRect.left + window.scrollX}px`;
  27.     tooltipStyle.top = `${inputRect.bottom + window.scrollY + 10}px`; // 10px的间距
  28.   }
  29. };
  30. onMounted(() => {
  31.   // 可以在这里做一些额外的初始化工作,比如监听窗口滚动事件来重新定位tooltip等
  32.   // 但在这个简单示例中,我们不需要这样做
  33. });
  34. </script>
  35. <style scoped>
  36. .tooltip {
  37.   position: absolute;
  38.   background-color: #333;
  39.   color: #fff;
  40.   text-align: center;
  41.   border-radius: 5px;
  42.   padding: 5px;
  43.   z-index: 1000;
  44.   white-space: nowrap;
  45.   /* 防止文本换行 */
  46. }
  47. .fade-enter-active,
  48. .fade-leave-active {
  49.   transition: opacity 0.3s;
  50. }
  51. .fade-enter,
  52. .fade-leave-to
  53. /* .fade-leave-active in <2.1.8 */
  54.   {
  55.   opacity: 0;
  56. }
  57. </style>
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表