点击TOP按钮后3D立方体显示异常问题求助(CSS/JQuery)
问题分析与解决方案
嘿,我看了你的代码,发现几个导致点击TOP按钮后其他面错乱的问题,咱们一步步来修复:
核心问题
- 数值计算错误:你在计算
cubeHeight时用了字符串".5",这会让JavaScript把它当成字符串拼接,导致transform的translateZ值变成无效的字符串(比如"500.5px"),而不是正确的数值 - 样式未重置:点击TOP按钮时直接修改了
front/back/left/right的宽高和top/bottom的transform,但切换回其他面时没有恢复这些样式,导致后续的3D变换完全错位 - 事件冲突:你给所有按钮绑定了两次点击事件(一次是全局的
$("button").click,一次是单独的.show-top.click),这会导致逻辑混乱
修复后的完整代码
HTML
<section id="options"> <p id="show-buttons"> <button class="show-front">FRONT</button> <button class="show-back">BACK</button> <button class="show-right">RIGHT</button> <button class="show-left">LEFT</button> <button class="show-top">TOP</button> <button class="show-bottom">BOTTOM</button> </p> </section> <section class="container"> <div id="cube" class="show-front"> <figure class="front">1</figure> <figure class="back">2</figure> <figure class="right">3</figure> <figure class="left">4</figure> <figure class="top">5</figure> <figure class="bottom">6</figure> </div> </section> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
CSS
body { margin:0px; padding:0px; } #options { position:absolute; top:0px; z-index:500; } .container { position: relative; margin: 0px; padding:0px; -webkit-perspective: 1600px; -moz-perspective: 1000px; -o-perspective: 1000px; perspective: 1000px; -webkit-backface-visibility: visible; } #cube { width: 100%; height: 100%; position: absolute; -webkit-transform-style: preserve-3d; -moz-transform-style: preserve-3d; -o-transform-style: preserve-3d; transform-style: preserve-3d; -webkit-transition: -webkit-transform 5s; -moz-transition: -moz-transform 1s; -o-transition: -o-transform 1s; transition: transform 1s; } #cube figure { margin: 0px; display: block; position: absolute; line-height: 196px; font-size: 120px; font-weight: bold; color: white; text-align: center; } #cube.panels-backface-invisible figure { -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; -o-backface-visibility: hidden; backface-visibility: hidden; } #cube .front { background: hsla(0, 100%, 50%, .6); } #cube .back { background: hsla(60, 100%, 50%, .6); } #cube .right { background: hsla(120, 100%, 50%, .6); } #cube .left { background: hsla(180, 100%, 50%, .6); } #cube .top { background: hsla(240, 100%, 50%, .6); } #cube .bottom { background: hsla(300, 100%, 50%, .6); } /* 初始3D变换(会被JS动态覆盖) */ #cube .front { transform: translateZ(100px); } #cube .back { transform: rotateX(-180deg) translateZ(100px); } #cube .right { transform: rotateY(90deg) translateZ(100px); } #cube .left { transform: rotateY(-90deg) translateZ(100px); } #cube .top { transform: rotateX(90deg) translateZ(100px); } #cube .bottom { transform: rotateX(-90deg) translateZ(100px); } /* 立方体视角变换类 */ #cube.show-front { transform: translateZ(-100px); } #cube.show-back { transform: translateZ(-100px) rotateX(-180deg); } #cube.show-right { transform: translateZ(-100px) rotateY(-90deg); } #cube.show-left { transform: translateZ(-100px) rotateY(90deg); } #cube.show-top { transform: translateZ(-100px) rotateX(-90deg); } #cube.show-bottom { transform: translateZ(-100px) rotateX(90deg); }
JavaScript
$(document).ready(function () { // 统一处理所有按钮点击事件 $("button").click(function () { var currentSide = $(this).attr("class"); // 先重置所有面的样式到正确状态 dimensions(); // 再切换立方体的视角类 $("#cube").removeClass().addClass(currentSide); }); var dimensions = function() { var winWidth = $(window).width(); var winHeight = $(window).height(); var cubeWidth = winWidth * 0.5; var cubeHeight = winHeight * 0.5; var bottomTranslate = winHeight - cubeWidth; // 设置各面基础尺寸 $(".front, .back, .left, .right").css({ width: winWidth, height: winHeight }); $(".top, .bottom").css({ width: winWidth, height: winWidth }); $(".container").css({ width: winWidth, height: winHeight }); // 重置所有面的3D变换 $('#cube .front').css({ "transform": "translateZ(" + cubeWidth + "px)" }); $('#cube .back').css({ "transform": "rotateX(-180deg) translateZ(" + cubeWidth + "px)" }); $('#cube .right').css({ "transform": "rotateY(90deg) translateZ(" + cubeWidth + "px)" }); $('#cube .left').css({ "transform": "rotateY(-90deg) translateZ(" + cubeWidth + "px)" }); $('#cube .top').css({ "transform": "rotateX(90deg) translateZ(" + cubeWidth + "px)" }); $('#cube .bottom').css({ "transform": "rotateX(-90deg) translateZ(" + bottomTranslate + "px)" }); }; // TOP按钮的特殊逻辑(如果需要保持TOP面的尺寸变化,可调整这里) $('.show-top').click(function() { var winWidth = $(window).width(); var winHeight = $(window).height(); var cubeHeight = winHeight * 0.5; // TOP面切换时的特殊尺寸调整 $(".top, .bottom").animate({ width: winWidth, height: winHeight },{ duration:5000, queue:false }); $('#cube .top').css({ "transform": "rotateX(90deg) translateZ(" + cubeHeight + "px)" }); $('#cube .bottom').css({ "transform": "rotateX(-90deg) translateZ(" + cubeHeight + "px)" }); $(".front, .back, .left, .right").css({ width: winHeight, height: winHeight }); }); // 初始化和窗口resize时重置样式 dimensions(); $(window).resize(function () { dimensions(); }); });
修复关键点说明
- 修正数值计算:把所有类似
* ".5"的字符串乘法改成* 0.5,确保得到正确的数值 - 统一样式重置:每次点击任何按钮时,先调用
dimensions()函数重置所有面的尺寸和3D变换,再切换立方体的视角类,这样就不会因为之前的样式修改导致错位 - 合并事件逻辑:保留TOP按钮的特殊动画,但确保其他按钮点击时会先恢复初始样式
- 简化前缀写法:现代浏览器大多支持标准的
transform属性,可不用单独写-webkit-/-moz-/-o-前缀(如果需要兼容旧浏览器可以加回去)
这样修改后,点击TOP按钮后再切换其他面,立方体就能正常显示了!
内容的提问来源于stack exchange,提问作者Deadpool
相关产品推荐
相关产品推荐

