JS实现骰子3D旋转效果


Posted in Javascript onOctober 24, 2019

本文实例为大家分享了JS实现骰子3D旋转效果展示的具体代码,供大家参考,具体内容如下

css部分代码:

.dice_box {
 width: 400px;
 height: 400px;
 position:relative;
 margin:0 auto;
 perspective: 900px;
 -moz-perspective: 900px;
 -webkit-perspective: 900px;
 perspective-origin: 50%, 50%;
 -moz-perspective-origin: 50%, 50%;
 -webkit-perspective-origin: 50%, 50%;
}
 
#dice1 {
 position: relative;
 -moz-transform-style: preserve-3d;
 -webkit-transform-style: preserve-3d;
 top:-90px;
 left:100px;
 width: 150px;
 height: 150px;
}
 
#dice2 {
 position: relative;
 -moz-transform-style: preserve-3d;
 -webkit-transform-style: preserve-3d;
 width: 150px;
 left: 120px;
 top: -150px; 
 height: 150px;
}
 
#dice3 {
 position: relative;
 top: -320px;
 left: 20px; 
 -moz-transform-style: preserve-3d;
 -webkit-transform-style: preserve-3d;
 width: 150px;
 height: 150px;
}
 
#dice3 ul li,#dice1 ul li,#dice2 ul li {
 position: absolute;
 list-style: none;
 width: 50px;
 height: 50px;
 line-height: 50px;
 text-align: center;
 font-size: 50px;
}
 
#dice1 ul li:nth-of-type(1),#dice2 ul li:nth-of-type(1),#dice3 ul li:nth-of-type(1) {
 top: 0px;
 left: 50px;
 -moz-transform-origin: bottom;
 -moz-transform: rotateX(-90deg);
 -webkit-transform-origin: bottom;
 -webkit-transform: rotateX(-90deg);
}
#dice1 ul li:nth-of-type(2),#dice2 ul li:nth-of-type(2),#dice3 ul li:nth-of-type(2) {
 top: 50px;
 left: 50px;
}
 
#dice1 ul li:nth-of-type(3),#dice2 ul li:nth-of-type(3),#dice3 ul li:nth-of-type(3) {
 top: 50px;
 left: 100px;
 -moz-transform-origin: left;
 -moz-transform: rotateY(-90deg);
 -webkit-transform-origin: left;
 -webkit-transform: rotateY(-90deg);
}
 
#dice1 ul li:nth-of-type(4),#dice2 ul li:nth-of-type(4),#dice3 ul li:nth-of-type(4) {
 top: 50px;
 left: 0px;
 -moz-transform-origin: right;
 -moz-transform: rotateY(90deg);
 -webkit-transform-origin: right;
 -webkit-transform: rotateY(90deg);
}
 
#dice1 ul li:nth-of-type(5),#dice2 ul li:nth-of-type(5),#dice3 ul li:nth-of-type(5) {
 top: 100px;
 left: 50px;
 -moz-transform-origin: top;
 -moz-transform: rotateX(90deg); 
 -webkit-transform-origin: top;
 -webkit-transform: rotateX(90deg);
}
 
#dice1 ul li:nth-of-type(6),#dice2 ul li:nth-of-type(6),#dice3 ul li:nth-of-type(6) {
 top: 50px;
 left: 50px;
 -moz-transform: translateZ(50px);
 -webkit-transform: translateZ(50px);
}

js部分代码:

function randomZeroOne(){
 var n=Math.random();
 if(n<0.5){
 return 0;
 }else{
 return 1;
 }
}
function calDice(a,b,c){
 var all=$("#k3_hz div.bet_k3_hz div");
 all.attr("class","k3_off");
 $("div.dice_box").show();
 $("#shadeDiv").show();
 var i=0;
 var k=0;
 var n=0;
 var index=1;
 var r= setInterval(function(){
 var x=randomZeroOne();
 var y=randomZeroOne();
 var z=randomZeroOne();
 if(n>1500){
 n=0;
 k=0;
 i=0;
 var box= document.getElementById("dice1");
 box.style.transform="rotate3d("+x+","+y+","+z+","+n+"deg)";
 var box1= document.getElementById("dice2");
 box1.style.transform="rotate3d("+x+","+y+","+z+","+k+"deg)"; 
 var box2= document.getElementById("dice3");
 box2.style.transform="rotate3d("+x+","+y+","+z+","+i+"deg)"; 
 $(box).css({"-webkit-transform":"rotate3d("+x+","+y+","+z+","+n+"deg)"});
 $(box1).css({"-webkit-transform":"rotate3d("+x+","+y+","+z+","+k+"deg)"});
 $(box2).css({"-webkit-transform":"rotate3d("+x+","+y+","+z+","+i+"deg)"});
 $("#dice1 li img").last().attr("src","/capricorn/resources/images/touch/"+a+".png");
 $("#dice2 li img").last().attr("src","/capricorn/resources/images/touch/"+b+".png");
 $("#dice3 li img").last().attr("src","/capricorn/resources/images/touch/"+c+".png");
 clearInterval(r);
 var m=setInterval(function(){
 $("div.dice_box").hide();
 $("#shadeDiv").hide();
 code=a+b+c;
 var all=$("#k3_hz div.bet_k3_hz div");
 all.attr("class","k3_off");
 $(all[code-4]).attr("class","k3_on");
 clearInterval(m);
 },1000);
 return;
 }
 index=index+0.01;
 i+=(120/index);
 k+=(100/index);
 n+=(60/index);
 var box= document.getElementById("dice1");
 $(box).css({"-webkit-transform":"rotate3d("+x+","+y+","+z+","+i+"deg)"});
 box.style.transform="rotate3d("+x+","+y+","+z+","+i+"deg)"; 
 var box1= document.getElementById("dice2");
 $(box1).css({"-webkit-transform":"rotate3d(1,0,1,"+i+"deg)"});
 box1.style.transform="rotate3d("+x+","+y+","+z+","+k+"deg)"; 
 var box2= document.getElementById("dice3");
 $(box2).css({"-webkit-transform":"rotate3d(0,1,1,"+i+"deg)"});
 box2.style.transform="rotate3d("+x+","+y+","+z+","+n+"deg)"; 
 },50);
}

html部分代码:

<div class="dice_box" style="display:none;z-index:110;">
 <div id="dice1">
 <ul>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/1.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/2.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/3.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/4.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/5.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/6.png"></li>
 </ul>
</div>
<div id="dice2">
 <ul>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/1.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/2.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/3.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/4.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/5.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/6.png"></li>
 </ul>
</div>
<div id="dice3">
 <ul>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/1.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/2.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/3.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/4.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/5.png"></li>
 <li><img src="<%=request.getAttribute("basePath")%>/resources/images/touch/6.png"></li>
 </ul>
 </div>
 </div>

这里引入了jquery ,支持火狐和谷歌,大家可以看下效果很简单。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
判断iframe是否加载完成的完美方法
Jan 07 Javascript
javascript 保存文件到本地实现方法
Nov 29 Javascript
javascript ajax的5种状态介绍
Aug 18 Javascript
jQuery菜单插件用法实例
Jul 25 Javascript
JS不用正则验证输入的字符串是否为空(包含空格)的实现代码
Jun 14 Javascript
BootStrap实现响应式布局导航栏折叠隐藏效果(在小屏幕、手机屏幕浏览时自动折叠隐藏)
Nov 30 Javascript
AngularJS 仿微信图片手势缩放的实例
Sep 28 Javascript
ionic3+Angular4实现接口请求及本地json文件读取示例
Oct 11 Javascript
Vue自定义指令封装节流函数的方法示例
Jul 09 Javascript
vue 实现在函数中触发路由跳转的示例
Sep 01 Javascript
jQuery 操作 HTML 元素和属性的方法
Nov 12 jQuery
VUE+Element实现增删改查的示例源码
Nov 23 Vue.js
Vue可自定义tab组件用法实例
Oct 24 #Javascript
js实现转动骰子模型
Oct 24 #Javascript
js实现固定区域内的不重叠随机圆
Oct 24 #Javascript
js实现随机div颜色位置 类似满天星效果
Oct 24 #Javascript
windows实现npm和cnpm安装步骤
Oct 24 #Javascript
JS实现简单随机3D骰子
Oct 24 #Javascript
JS合并两个数组的3种方法详解
Oct 24 #Javascript
You might like
针对初学PHP者的疑难问答(1)
2006/10/09 PHP
PHP_NETWORK_GETADDRESSES: GETADDRINFO FAILED问题解决办法
2014/05/04 PHP
深入解析PHP的Yii框架中的缓存功能
2016/03/29 PHP
javascript 点击整页变灰的效果(可做退出效果)。
2008/01/09 Javascript
Prototype Date对象 学习
2009/07/12 Javascript
九种js弹出对话框的方法总结
2013/03/12 Javascript
JQuery事件e参数的方法preventDefault()取消默认行为
2013/09/26 Javascript
javascript生成json数据简单示例分享
2014/02/14 Javascript
jquery手风琴特效插件
2015/02/04 Javascript
js弹出窗口返回值的简单实例
2016/05/28 Javascript
js中判断变量类型函数typeof的用法总结
2016/08/09 Javascript
bootstrapValidator自定验证方法写法
2016/12/01 Javascript
Js判断H5上下滑动方向及滑动到顶部和底部判断的示例代码
2017/11/15 Javascript
jquery 通过ajax请求获取后台数据显示在表格上的方法
2018/08/08 jQuery
node实现简单的增删改查接口实例代码
2019/08/22 Javascript
vue-router 中 meta的用法详解
2019/11/01 Javascript
微信小程序连接服务器展示MQTT数据信息的实现
2020/07/14 Javascript
原生JS实现京东查看商品点击放大
2020/12/21 Javascript
[02:02]2018DOTA2亚洲邀请赛Mineski赛前采访
2018/04/04 DOTA
python 自动化将markdown文件转成html文件的方法
2016/09/23 Python
详解python 字符串和日期之间转换 StringAndDate
2017/05/04 Python
pandas实现选取特定索引的行
2018/04/20 Python
Python+pyplot绘制带文本标注的柱状图方法
2019/07/08 Python
python爬虫开发之Beautiful Soup模块从安装到详细使用方法与实例
2020/03/09 Python
python matplotlib:plt.scatter() 大小和颜色参数详解
2020/04/14 Python
浅谈keras通过model.fit_generator训练模型(节省内存)
2020/06/17 Python
python 将列表里的字典元素合并为一个字典实例
2020/09/01 Python
法国在线药房:Shop Pharmacie
2019/11/26 全球购物
亚洲在线旅行门户网站:Expedia.com.hk(智游网)
2020/04/14 全球购物
后勤人员岗位职责
2013/12/17 职场文书
书法比赛获奖感言
2014/02/10 职场文书
最新优秀教师个人先进事迹材料
2014/05/06 职场文书
幼儿园老师个人总结
2015/02/28 职场文书
《钢铁是怎样炼成的》高中读后感
2019/08/07 职场文书
Python代码,能玩30多款童年游戏!这些有几个是你玩过的
2021/04/27 Python
python index() 与 rindex() 方法的使用示例详解
2022/12/24 Python