JS实现简单随机3D骰子


Posted in Javascript onOctober 24, 2019

本文实例为大家分享了JS实现简单随机3D骰子的具体代码,供大家参考,具体内容如下

描述:

JS——实现简单的随机3D骰子。

效果:

JS实现简单随机3D骰子

JS实现简单随机3D骰子

实现:

html文件:

<!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>
<link rel="stylesheet" href="css/T1.css" >
 
<body>
 <div class="dice">
  <div class="face">
   <div class="item1"></div>
  </div>
  <div class="face">
   <div class="item2"></div>
   <div class="item2"></div>
  </div>
  <div class="face">
   <div class="item3"></div>
   <div class="item3"></div>
   <div class="item3"></div>
  </div>
  <div class="face">
   <div class="face_0">
    <div class="item4"></div>
    <div class="item4"></div>
   </div>
   <div class="face_0">
    <div class="item4"></div>
    <div class="item4"></div>
   </div>
  </div>
  <div class="face">
   <div class="face_0">
    <div class="item5"></div>
    <div class="item5"></div>
   </div>
   <div class="face_0">
    <div class="item5"></div>
   </div>
   <div class="face_0">
    <div class="item5"></div>
    <div class="item5"></div>
   </div>
  </div>
  <div class="face">
   <div class="face_0">
    <div class="item6"></div>
    <div class="item6"></div>
   </div>
   <div class="face_0">
    <div class="item6"></div>
    <div class="item6"></div>
   </div>
   <div class="face_0">
    <div class="item6"></div>
    <div class="item6"></div>
   </div>
  </div>
 </div>
 
 <p class="view"></p>
 
 <p>
  <button id="play">PLAY</button>
 </p>
</body>
<script src="js/T1.js"></script>
 
</html>

css文件:

@keyframes rotate {
 to {
  transform: rotateX(360deg) rotateY(360deg);
 }
}
 
@keyframes domo{
 20%{
  transform: rotateX(20deg);
 }
 60%{
  transform: rotateX(20deg) rotateY(200deg);
 }
 100%{
  transform: rotateX(100deg) rotateY(1000deg) rotateZ(-100deg);
 }
}
 
 
.dice {
 width: 300px;
 height: 300px;
 margin: 100px auto 200px;
 position: relative;
 transform-style: preserve-3d;
 /* animation: rotate 2s; */
 animation-duration: 3s;
 animation-timing-function: linear;
 transition: all 1s;
}
 
.dice:hover{
 animation: domo 5s linear 0s infinite alternate;
}
 
.dice .face {
 width: 100%;
 height: 100%;
 display: flex;
 position: absolute;
 left: 0;
 top: 0;
 border: 1px solid #000;
 background: rgb(253, 250, 250);
 border-radius: 20px;
 font-size: 100px;
 color: #fff;
 line-height: 300px;
 text-align: center;
}
 
.item1{
 width: 100px;
 height: 100px;
 background: red;
 border-radius: 50%;
}
 
.item2{
 width: 75px;
 height: 75px;
 background: blue;
 border-radius: 50%;
}
 
.item3{
 width: 75px;
 height: 75px;
 background: blue;
 border-radius: 50%;
}
 
.item4{
 width: 75px;
 height: 75px;
 background: rgb(240, 54, 54);
 border-radius: 50%;
}
 
.item5{
 width: 75px;
 height: 75px;
 background: blue;
 border-radius: 50%;
}
 
.item6{
 width: 75px;
 height: 75px;
 background: blue;
 border-radius: 50%;
}
 
.face:nth-child(1) {
 justify-content: center;
 align-items: center;
 transform: translateZ(150px);
}
 
.face:nth-child(2) {
 justify-content: space-around;
 align-items: center;
 transform: rotateX(-90deg) translateZ(150px);
}
 
.face:nth-child(3) {
 justify-content: space-around;
 align-items: center;
 flex-wrap: wrap;
 transform: rotateY(90deg) translateZ(150px);
}
 
.face:nth-child(4) {
 flex-wrap: wrap;
 justify-content: center;
 align-items: center;
 transform: rotateY(-90deg) translateZ(150px);
}
 
.face:nth-child(4) .face_0{
 width: 300px;
 height: 75px;
 display: flex;
 justify-content: space-around;
}
 
.face:nth-child(5) {
 flex-wrap: wrap;
 justify-content: center;
 align-items: center;
 transform: rotateX(90deg) translateZ(150px);
}
 
.face:nth-child(5) .face_0{
 width: 300px;
 height: 75px;
 display: flex;
 justify-content: space-around;
}
 
.face:nth-child(6) {
 flex-wrap: wrap;
 justify-content: center;
 align-items: center;
 transform: translateZ(-150px);
}
 
.face:nth-child(6) .face_0{
 width: 300px;
 height: 75px;
 display: flex;
 justify-content: space-around;
}
 
p {
 text-align: center;
}

js文件:

(function () {
 
 let odice = document.querySelector('.dice')
 let oview = document.querySelector('.view')
 let oplay = document.querySelector('#play') // 按钮
 oplay.onclick = playTheGame
 
 // 当骰子动画执行后
 odice.addEventListener('webkitAnimationEnd', () => {
  odice.style.animationName = 'none' // 更改动画属性,以待下一次动画的正常执行
  // 可能出现的情况集合
  let _posible = [
   { value: 1, x: 0, y: 0 },
   { value: 6, x: 0, y: 180 },
   { value: 3, x: 0, y: -90 },
   { value: 4, x: 0, y: 90 },
   { value: 5, x: -90, y: 0 },
   { value: 2, x: 90, y: 0 },
  ]
  // 准备抽取的随机数
  let _random = Math.floor(Math.random() * 6)
  // 抽取的随机结果
  let _result = _posible[_random]
  setTimeout(() => { // 浏览器反应不过来加过渡
   // 让骰子旋转到正确的角度
   odice.style.transform = ` 
    rotateX(${ _result.x }deg) rotateY(${ _result.y }deg)
   ` 
   renderView(_result.value) // 渲染视图
  }, 0);
  
 })
 
 function playTheGame() { // 游戏方法
  // 骰子转起来 
  
  // 有的时候浏览器在连续使用js操作css的时候会出现问题(反应不过来),比如,效果不能正常显示,此时可以尝试利用setTimeout-0来将目标代码放入到异步队列中等待执行
  // setTimeout(() => {
   odice.style.animationName = 'rotate'
  // }, 0);     
 }
 
 
 function renderView(result) { // 渲染结果到页面视图
  oview.innerHTML = '您此次的投掷结果为:' + result   
 }
 
})();

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

Javascript 相关文章推荐
js loading加载效果实现代码
Nov 24 Javascript
javascript-表格排序(降序/反序)实现介绍(附图)
May 30 Javascript
ExtJS判断IE浏览器类型的方法
Feb 10 Javascript
Node.js安装教程和NPM包管理器使用详解
Aug 16 Javascript
javascript字符串与数组转换汇总
May 26 Javascript
微信小程序学习之数据处理详解
Jul 05 Javascript
解决html-jquery/js引用外部图片时遇到看不了或出现403的问题
Sep 22 jQuery
layer实现关闭弹出层刷新父界面功能详解
Nov 15 Javascript
浅析JS抽象工厂模式
Dec 14 Javascript
利用原生的JavaScript实现简单拼图游戏
Nov 18 Javascript
TypeScript的安装、使用、自动编译的实现
Apr 10 Javascript
vue-cropper组件实现图片切割上传
May 27 Vue.js
JS合并两个数组的3种方法详解
Oct 24 #Javascript
js实现简单掷骰子小游戏
Oct 24 #Javascript
js实现GIF图片的分解和合成
Oct 24 #Javascript
js实现掷骰子小游戏
Oct 24 #Javascript
Element-Ui组件 NavMenu 导航菜单的具体使用
Oct 24 #Javascript
js实现GIF动图分解成多帧图片上传
Oct 24 #Javascript
js实现简单掷骰子效果
Oct 24 #Javascript
You might like
如何使用动态共享对象的模式来安装PHP
2006/10/09 PHP
echo(),print(),print_r()之间的区别?
2006/11/19 PHP
利用PHP制作简单的内容采集器的代码
2007/11/28 PHP
基于PHP的简单采集数据入库程序
2014/07/30 PHP
PHP 反射(Reflection)使用实例
2015/05/12 PHP
Yii框架用户登录session丢失问题解决方法
2017/01/07 PHP
VBScript版代码高亮
2006/06/26 Javascript
jquery判断单选按钮radio是否选中的方法
2015/05/05 Javascript
javascript实现表单验证
2016/01/29 Javascript
jQuery验证插件validate使用方法详解
2020/09/13 Javascript
使用do...while的方法输入一个月中所有的周日(实例代码)
2016/07/22 Javascript
CSS3 3D 技术手把手教你玩转
2016/09/02 Javascript
JS数字千分位格式化实现方法总结
2016/12/16 Javascript
关于Vue实现组件信息的缓存问题
2017/08/23 Javascript
浅谈node.js 命令行工具(cli)
2018/05/10 Javascript
ES6箭头函数和扩展实例分析
2020/05/23 Javascript
解决vue+elementui项目打包后样式变化问题
2020/08/03 Javascript
使用python实现strcmp函数功能示例
2014/03/25 Python
在Python程序中操作文件之flush()方法的使用教程
2015/05/24 Python
Python中threading模块join函数用法实例分析
2015/06/04 Python
Python常用库推荐
2016/12/04 Python
python2.7读取文件夹下所有文件名称及内容的方法
2018/02/24 Python
python逐行读写txt文件的实例讲解
2018/04/03 Python
解决Python print 输出文本显示 gbk 编码错误问题
2018/07/13 Python
Python实现根据日期获取当天凌晨时间戳的方法示例
2019/04/09 Python
Django中使用CORS实现跨域请求过程解析
2019/08/05 Python
python批量读取文件名并写入txt文件中
2020/09/05 Python
使用jupyter notebook运行python和R的步骤
2020/08/13 Python
荷兰照明、灯具和配件网上商店:dmlights
2019/08/25 全球购物
公司活动策划方案
2014/01/13 职场文书
企业军训感言
2014/02/08 职场文书
抗洪抢险事迹材料
2014/05/06 职场文书
运动会跳远广播稿
2015/08/19 职场文书
会计做账心得体会
2016/01/22 职场文书
用Python爬虫破解滑动验证码的案例解析
2021/05/06 Python
MySQL优化之慢日志查询
2022/06/10 MySQL