解决‘底层的图层经过transform:rotate转换后,覆盖住高图层’的问题

本文介绍了解决H5页面中红旗图片使用transform旋转导致层级错乱问题的方法。通过应用translateZ调整,实现了预期的视觉效果并修复了z-index失效的问题。

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


一、问题:

         问题背景:在写H5的时候,因为要使z-index:0的红旗这张图片达到飘摇的动效,所以需要加transform:rotateX()rotateY()进行角度旋转转换;

         问题出现:动效是有了,但是层级感觉变了,有种变成了z-index:无穷大 的感觉,z-index好像已经失效了。

二、思路:

      知道是层级的问题,就找和z-index差不多作用的,此时transform: translateZ()应该是最佳的选择。

三、解决:

      把红旗这张图片遮住的图层,添加上transform: translateZ(数值看着给)这个样式,就OK了。

import styled, { keyframes } from 'styled-components'; import useStore from '../../store/store'; const LoadingPage = () => { const { loading } = useStore(); return ( <> <LoadingContainer className={loading ? '' : 'loaded'}> {/* <Loader /> <div>Loading...</div> */} <LoadingLeft className={loading ? '' : 'loaded'} /> <LoadingRight className={loading ? '' : 'loaded'} /> <SpinnerBox className={loading ? '' : 'loaded'}> <div className='configure-border-1'> <div className='configure-core'></div> </div> <div className='configure-border-2'> <div className='configure-core'></div> </div> <div className='loading-word'>加载中...</div> </SpinnerBox> </LoadingContainer> </> ) } // 定义加载动画 const spin = keyframes` 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } `; // Loading 组件样式 const LoadingContainer = styled.div` display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100%; width: 100%; font-size: 24px; z-index: 999; transition: all 2s; opacity: 1; position: fixed; &.loaded { opacity: 0; position: absolute; z-index: -1001; } `; const Loader = styled.div` border: 8px solid rgba(255, 255, 255, 0.3); border-top: 8px solid white; border-radius: 50%; width: 60px; height: 60px; animation: ${spin} 1s linear infinite; `; const LoadingLeft = styled.div` position: fixed; z-index: 1000; width: 52%; height: 100%; background: #37474f; left: 0; transition: all 1s; &.loaded { transform: translate(-100%, 0); } ` const LoadingRight = styled.div` position: fixed; z-index: 1000; width: 52%; height: 100%; background: #37474f; right: 0; transition: all 1s; &.loaded { transform: translate(100%, 0); } ` const SpinnerBox = styled.div` position: fixed; z-index: 1001; opacity: 1; transition: all .3s; &.loaded { opacity: 0; } .configure-border-1 { position: absolute; padding: 3px; width: 115px; height: 115px; background: #ffab91; animation: configure-clockwise 3s ease-in-out 0s infinite alternate; .configure-core { width: 100%; height: 100%; background: #37474f; } } .configure-border-2 { left: -115px; padding: 3px; width: 115px; height: 115px; background: #3ff9dc; transform: rotate(45deg); animation: configure-xclockwise 3s ease-in-out 0s infinite alternate; .configure-core { width: 100%; height: 100%; background: #37474f; } } .loading-word { position: absolute; color: #eff; font-size: 16px; top: 50%; left: 50%; transform: translate(-50%, -50%); white-space: nowrap; } ` export default LoadingPage出现这个bug tree hydrated but some attributes of the server rendered HTML didn't match the client properties. This won't be patched up. This can happen if a SSR-ed Client Component used:
03-15
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值