实现带hover效果的斜向分屏图时右半图错位,如何修正使其与左半布局一致
对角线分屏错位问题修复
问题根因
- 背景定位计算未区分左右半区的容器位置,右侧半区沿用了左侧的
background-position-x规则,导致图片偏移 - 定位计算使用相对
.half-inner自身宽度的百分比,未结合该容器为 200% 宽度的特性,出现逻辑偏差 - 按钮定位规则左右颠倒,左侧按钮被放置在右半区可视范围内
- 通用覆盖层(
::before)颜色未区分左右半区的蒙层需求,左侧白色蒙层被黑色覆盖层抵消
核心调整方案
- 背景定位改用视口单位(vw)计算,避免容器宽度带来的百分比偏差
- 拆分通用覆盖层的样式,区分左右半区的蒙层颜色
- 修正按钮的左右定位规则,对应到所属半区的中心位置
- 统一左右半区的
background-size规则,保证图片缩放比例一致
修复后代码
.wrapper { position: relative; width: 100%; height: 100vh; font-size: 0; overflow: hidden; } .half, .half-inner { width: 100%; height: 100%; display: inline-block; transition: background .2s ease-in; } .half-inner { width: 200%; background-size: 100vw 100vh; background-repeat: no-repeat; } .half { position: absolute; overflow: hidden; transform: skew(-15deg); } .half-inner { transform: skew(15deg); position: relative; } /* 左侧半区样式 */ .half.left { left: -50%; background: rgba(255,255,255,.75); } .half.left .half-inner { /* 替换图片路径为本地路径即可 */ background: #f0f0f0 url('your-left-image-path.jpg'); background-position-x: 50vw; background-position-y: 0; } .half.left .half-inner::before { content: ""; position: absolute; top: 0; left: 0; bottom: 0; right: 0; background: rgba(255,255,255,.75); transition: background .2s ease-in; } /* 右侧半区样式 */ .half.right { left: 50%; background: rgba(0,0,0,.75); } .half.right .half-inner { /* 替换图片路径为本地路径即可 */ background: #333 url('your-right-image-path.jpg'); background-position-x: 0vw; background-position-y: 0; } .half.right .half-inner::before { content: ""; position: absolute; top: 0; left: 0; bottom: 0; right: 0; background: rgba(0,0,0,.75); transition: background .2s ease-in; } /* hover 交互样式 */ .half.right:hover .half-inner::before { background: rgba(0,0,0,.55); } .half.left:hover .half-inner::before { background: rgba(255,255,255,.55); } .half.right:hover { background: rgba(0,0,0,.55); } .half.left:hover { background: rgba(255,255,255,.55); } /* 按钮样式 */ .button { font-size: 16px; font-family: oswald; font-weight: 700; display: inline-block; padding: 0 50px 0 25px; height: 51px; width: 80px; text-decoration: none; text-align: center; line-height: 51px; text-transform: uppercase; position: absolute; top: calc(50% - 25px); } .button.left { background: rgba(255,255,255,.75); color: #000; } .button.right { background: rgba(0,0,0,.75); color: #fff; } .button::before, .button::after { content: ""; display: block; position: absolute; top: 0; right: 0; } .button::before { border-right: 51px solid #777; border-bottom: 51px solid transparent; opacity: 0.25; } .button::after { border-right: 51px solid #EEE; border-top: 51px solid transparent; opacity: 0.3; } /* 修正按钮定位 */ .half.left .button.left { left: calc(25vw - 50px); } .half.right .button.right { left: calc(75vw - 50px); }
<main class="wrapper"> <section class="half left"> <section class="half-inner"> <a class="button left" href="#">Codepen</a> </section> </section> <section class="half right"> <section class="half-inner"> <a class="button right" href="#">Github</a> </section> </section> </main>
效果说明
- 左右半区背景图完全对齐对应可视区域,无中间错位三角形
- hover 时蒙层透明度变化符合预期,按钮位置对应所属半区
- 替换本地图片路径后即可恢复原有设计的视觉效果
内容的提问来源于stack exchange,提问作者kmb67116
相关产品推荐
相关产品推荐

