JavaScript实现的3D旋转魔方动画效果实例代码
网络编程 2021-07-04 15:51www.168986.cn编程入门
在本篇文章里长沙网络推广给大家整理了关于JavaScript实现的3D旋转魔方动画效果实例代码,有兴趣的朋友们测试下。
JS1K是JavaScript编程竞赛——参加竞赛的规则很简单,脚本必须小于1K,竞赛网站开始也只是为了娱乐,却意外地收到了很多优秀的作品。
这是2016年JS1k上传的作品,用几十行代码实现的一个3D旋转魔方
代码如下
function z(t, e) { return t? e? t.appendChild(e) : "width:"+t+"px;height:"+t+" px;position:absolute;" : document.createElement("div") } function w() { ++t==360&&(t=0, x=++x%3) for (i in m) 2 == m[i][s[x]] ? m[i][u][a] = r+s[x]+"(" + t + "deg)" : 0; c[u][a] = r+"3d(1,1,1," + t + "deg)", requestAnimationFrame(w) } var a = "transform", p = "background-color:", y = a+"-style:preserve-3d;", u = "style", v = "cssText", B = z(), c = z(), t = x = 0, d, e, f, g, h, k, l, m = [], n, i, r="rotate", s = ["X","Y","Z"]; B[u][v] = "perspective:900px;"+z(600)+p+"#666"; c[u][v] = y + z(240) +":30%;left:30%", z(B, c), z(window.b, B); for (l = 27; l--; z(c, f)) { f = z(), f[u][v] = y + z(240), f.X = g = l % 3, f.Y = h = (l - g) % 9 / 3, f.Z = k = ~~(l / 9), e = z(), e[u][v] = y + z(80) +a+":translate3d(" + 80 g + "px," + 80 h + "px," + 80 (k-1) + "px)"; for (n = 6; n--; z(e, d)) d = z(), d[u][v] = y + z(72) + "border-radius:9px;border:4px solid #000;opacity:0.9;"+a+":"+r+"X(" + (4 > n ? 90 n : 0) + "deg)"+r+"Y(" + (4 > n ? 0 : 4 == n ? -90 : 90) + "deg)translateZ(40px);"+p+ (0==n&&2==k?"#05C":1==n&&0==h?"#FD0": 2==n&&0==k?"#0A6":3==n&&2==h?"#FFF":4==n&&0==g?"#F60":5==n&&2==g?"#C24":"#000"); z(f, e), m.push(f) } w();
知识点扩展
JS如何实现旋转的魔方,浏览器从其他标签页回来依然匀速旋转?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div class="cube" onclick="s()"> <div class="front">front</div> <div class="back">back</div> <div class="left">left</div> <div class="right">right</div> <div class=""></div> <div class="bottom">bottom</div> </div> </body> <style> .cube { width: 400px; height: 400px; transition: all 3s; margin: 300px auto; / 作用于所有3d转换的子元素 设置给父级 / / perspective: 1000px; / / transform: rotateX(30deg) rotateY(45deg); / / 平面到立方 默认值:平面flat/ transform-style: preserve-3d; position: relative; } / div:hover { transform: rotateY(90deg) translateZ(-200px); opacity: 0.5; } / .front, .back, .left, .right, ., .bottom { width: 100%; height: 100%; text-align: center; line-height: 400px; position: absolute; opacity: 0.9; left: 0; : 0; } .front { background-color: palevioletred; background-image: url('http://img1.gtimg./tj/pics/hv1/117/208/2153/140051982.jpg'); transform: translateZ(200px); } .back { background-color: yellow; transform: translateZ(-200px); background-image: url('https://ss1.bdstatic./70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=841408372,3004217725&fm=11&gp=0.jpg'); } .left { background-color: pink; background-image: url('https://ss0.bdstatic./70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2388632836,3966607624&fm=26&gp=0.jpg'); transform: rotateY(90deg) translateZ(-200px) } .right { background-color: yellowgreen; background-image: url('https://ss0.bdstatic./70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=192610795,467565159&fm=26&gp=0.jpg'); transform: rotateY(90deg) translateZ(200px) } . { background-color: skyblue; background-image: url("https://ss0.bdstatic./70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2936477803,4198185787&fm=15&gp=0.jpg"); transform: rotateX(90deg) translateZ(200px) } .bottom { background-color: orange; background-image: url('http://img0.pclady../pclady/1611/02/1612285_jyz.jpg'); transform: rotateX(90deg) translateZ(-200px) } </style> <script> window.onload = function () { let cube = document.querySelector('.cube') let timer = null let x = 0; y = 0; function rotate() { cube.style.transform = 'rotateX(' + x + 'deg)' + '' + 'rotateY(' + y + 'deg)'; x += 30 y += 45 } function s() { clearInterval(timer) } clearInterval(timer); timer = setInterval(() => { rotate(); }, 1000); // 监听是否在当前页,并置为已读 // document.addEventListener("visibilitychange", function () { // if (document.hidden) { //不处于当前页面 // // do something // clearInterval(timer) // } else { // timer; // } // }); var hiddenProperty = 'hidden' in document ? 'hidden' : 'webkitHidden' in document ? 'webkitHidden' : 'mozHidden' in document ? 'mozHidden' : null; var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange'); var onVisibilityChange = function () { if (!document[hiddenProperty]) { timer = setInterval(() => { rotate(); }, 1000); console.log(hiddenProperty); } else { clearInterval(timer) } } document.addEventListener(visibilityChangeEvent, onVisibilityChange); // document.addEventListener("visibilitychange", function () { // if (!document.hidden) { //处于当前页面 // timer = setInterval(() => { // rotate(); // }, 1000); // console.log('active'); // } else { // clearInterval(timer); // console.log('hidden'); // } // }); } </script> </html>
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程