关于z-index与transform同时使用时的一些感想

本文探讨了在实现JavaScript点击翻牌效果时遇到的Z-index与transform冲突问题,并介绍了解决方案——通过使用backface-visibility:hidden属性来规避这一问题。

这篇文章是在我做一个效果是产生的。

<div class="card">
            <div class="card_front" style="transition:2s ease">
                <img class="frontImg" src="image/timg%20(1).gif" alt="">
                <p class="content">
                    <span class="title">陪我逛街卡</span>
                    <span>使用该卡,无条件陪逛街3小时</span>
                </p>
            </div>
            <div class="card_rear" style="transition:2s ease">
                <div class="card_bg"></div>
            </div>
        </div>

我要做的效果是js点击翻牌效果,将正面div设置为旋转180度,反面为负180度,同时设置正面z-index为1,反面为2;但是在我给背面添加hover效果时,发现触发鼠标事件时会出现bug,在刚开始我以为是我对卡牌正面一些子元素设置引起的,但是在我认真检查后没有发现,后来发现当我鼠标移入时,左边获取的是卡牌正面,右边获取的是卡牌背面;

在我搜索了之后发现是因为z-index与transform同时作用是会引起z-index失效,但是在我查看张鑫旭大神的博文时却发现好像只有safari浏览器有这个问题。我不知道为什么我也会出现这个问题。同时我测试使用推荐的方法去进行处理,比如将父级设置为overflow:hidden;这个时候虽然解决了这个问题,但是在点击翻牌时却失去了效果。也就是说overflow:hidden;使得transform-style:preserve-3d;这个效果失效了;因为在我测试中发现就是因为这个属性使得堆叠次序出现问题;这让我很绝望,然后我发现了一种解决方法;

backface-visibility:hidden;方法


这个是w3cschool的介绍,w3cschool都说很有用;这种方法是一种规避的方法,不管其在z轴方向的堆叠次序,直接将卡牌正面的可见性给隐藏掉。相当于间接解决了这个问题

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>圆形音乐播放器</title> <style> body { display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background-color: #f4f4f4; } #player { position: relative; width: 400px; height: 400px; border-radius: 50%; overflow: hidden; box-shadow: 0 0 15px rgba(0, 0, 0, 0.3); } #player img { width: 100%; height: 100%; object-fit: cover; opacity: 0.8; } #controls { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 15px; color: white; text-shadow: 0 0 8px rgba(0, 0, 0, 0.7); } #play-pause { background: transparent; border: none; font-size: 48px; cursor: pointer; transition: all 0.3s; width: 80px; height: 80px; display: flex; align-items: center; justify-content: center; border-radius: 50%; background-color: rgba(0, 0, 0, 0.3); } #play-pause:hover { background-color: rgba(0, 0, 0, 0.5); transform: scale(1.05); } #volume-control { -webkit-appearance: none; width: 160px; height: 12px; background: rgba(255, 255, 255, 0.3); border-radius: 6px; cursor: pointer; box-shadow: 0 0 8px rgba(0, 0, 0, 0.5); } #volume-control::-webkit-slider-thumb { -webkit-appearance: none; width: 24px; height: 24px; border-radius: 50%; background: white; box-shadow: 0 0 8px rgba(0, 0, 0, 0.5); transition: transform 0.3s; } #volume-control::-webkit-slider-thumb:hover { transform: scale(1.2); } #info { position: absolute; bottom: 20px; left: 0; right: 0; text-align: center; color: white; text-shadow: 0 0 8px rgba(0, 0, 0, 0.7); font-size: 18px; } #lyrics-container { position: absolute; bottom: 0; left: 0; right: 0; transition: all 0.3s ease-in-out; } #lyrics-toggle { display: none; } #lyrics-toggle-label { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); color: white; text-shadow: 0 0 8px rgba(0, 0, 0, 0.7); cursor: pointer; transition: all 0.3s; font-size: 20px; z-index: 2; } #lyrics { height: 120px; overflow-y: scroll; background-color: rgba(0, 0, 0, 0.5); color: white; text-shadow: 0 0 5px rgba(0, 0, 0, 0.5); font-size: 16px; padding: 15px 0; scrollbar-width: none; display: flex; flex-direction: column; align-items: center; transition: all 0.3s ease-in-out; } #lyrics::-webkit-scrollbar { display: none; } #lyrics p { margin: 6px 0; transition: all 0.2s; padding: 3px 15px; border-radius: 15px; width: 80%; text-align: center; } #lyrics p:hover { background-color: rgba(255, 255, 255, 0.1); transform: scale(1.02); } #lyrics-toggle:checked ~ #lyrics { height: 0; padding: 0; opacity: 0; } #lyrics-toggle:checked ~ #lyrics-toggle-label i { transform: rotate(180deg); } </style> </head> <body> <div id="player"> <audio id="audio-player" src="陪你度过漫长岁月.mp3"></audio> <img src="封面图片地址(需替换)" alt="封面图片"> <div id="controls"> <button id="play-pause">▶</button> <input type="range" min="0" max="1" step="0.01" value="0.5" id="volume-control"> </div> <div id="info"> <p><a href="#">陪你度过漫长岁月</a></p> <p><a href="#">陈奕迅</a></p> </div> <div id="lyrics-container"> <input type="checkbox" id="lyrics-toggle"> <label for="lyrics-toggle" id="lyrics-toggle-label">▼</label> <div id="lyrics"> <p>陪你把沿路感想 活出了答案</p> <p>陪你把独自孤单 变成了勇敢</p> <p>一次次失去 又重来 我没离开</p> <p>陪伴是 最长情的告白</p> <p></p> <p>陪你把想念的酸 拥抱成温暖</p> <p>陪你把彷徨 写出情节来</p> <p>未来多漫长 再漫长 还有期待</p> <p>陪伴你 一直到 故事给说完</p> <p></p> <p>让我们静静分享 此刻难得的坦白</p> <p>只是无声地交谈 都感觉幸福 感觉不孤单</p> <p></p> <p>陪你把沿路感想 活出了答案</p> <p>陪你把独自孤单 变成了勇敢</p> <p>一次次失去 又重来 我没离开</p> <p>陪伴是 最长情的告白</p> <p></p> <p>陪你把想念的酸 拥抱成温暖</p> <p>陪你把彷徨 写出情节来</p> <p>未来多漫长 再漫长 还有期待</p> <p>陪伴你 一直到 故事给说完</p> <p></p> <p>陪你把沿路感想 活出了答案</p> <p>陪你把独自孤单 变成了勇敢</p> <p>一次次失去 又重来 我没离开</p> <p>陪伴是 最长情的告白</p> <p></p> <p>陪你把想念的酸 拥抱成温暖</p> <p>陪你把彷徨 写出情节来</p> <p>未来多漫长 再漫长 还有期待</p> <p>陪伴你 一直到 这世界说完</p> </div> </div> </div> <script> document.addEventListener('DOMContentLoaded', function() { const audio = document.getElementById('audio-player'); const playPauseBtn = document.getElementById('play-pause'); const volumeControl = document.getElementById('volume-control'); function updateButtonState() { if (audio.paused || audio.ended) { playPauseBtn.textContent = '▶'; playPauseBtn.title = '播放'; } else { playPauseBtn.textContent = '❚❚'; playPauseBtn.title = '暂停'; } } updateButtonState(); function playPause() { if (audio.paused || audio.ended) { audio.play(); updateButtonState(); } else { audio.pause(); updateButtonState(); } } function setVolume() { audio.volume = volumeControl.value; } audio.addEventListener('play', updateButtonState); audio.addEventListener('pause', updateButtonState); audio.addEventListener('ended', updateButtonState); playPauseBtn.addEventListener('click', playPause); volumeControl.addEventListener('input', setVolume); }); </script> </body> </html> 帮我做暂停播放的功能 html 帮我插入歌曲,ch01项目中的audio项目中的陪你度过漫长岁月歌曲,完善音量调整功能
最新发布
05-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值