用原生写随机点名案例

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #name_box li {
      margin: 10px;
      padding: 10px;
      border: 1px solid white;
      border-radius: 10px;
    }

    .active {
      background-color: white;
      color: black;
      /* 2D转换的属性名 */
      transform: scale(1.3);
      /* 过渡效果的属性名 */
      transition: transform 0.3s;
      /* 位移,属于transform的属性值 */
      /* translate */
    }
  </style>
</head>

<body style="background-color: black; color: white;">

  <div style="display: flex; flex-flow: column; align-items: center;">
    <div>总人数:<span id="total">?</span> 人,抽取: <input type="text" id="size"></div>
    <div>
      <button id="btn" type="button" style="height: 50px; font-size: 20px; margin-top: 10px;">点击随机抽取 ?</button>
    </div>
    <div>
      <ul id="name_box" style="list-style: none; margin: 0; padding: 0; display: flex; flex-flow: row wrap;">
      </ul>
    </div>
  </div>

  <script>

    (() => {

      const btn = document.querySelector('#btn')
      const name_box = document.querySelector('#name_box')
      const sizeInput = document.querySelector('#size')
      const total = document.querySelector('#total')

      console.log();

      let size = sizeInput.value ? parseInt(sizeInput.value) : 1;

      btn.innerHTML = `点击随机抽取 ${size} 位`

      const students = [
      '张三',
            '李四',
            '王五',
            '阿福',
            '旺财',
            '小米',
            '小红',
            '白白',
            '小天',
            '赵六',
            '晓晴',
            '吴浩',
            '阿勇',
            '王武',
            '江山',
            '天下',
            '阮天',
            '萌萌',
            '芒果',
            '卿苑',
            '青柠',
            '青鸢',
            '苹芃',
            '小妮',
            '子依',
            '点点',
            '贤贤',
            '软甜',
            '温柔',
            '御姐',
            '萝莉',
            '嘻嘻',
            '瞅瞅',
            '精灵',
            '鲁班',
            '小智',
            '月月',
            '软软',
            '冉冉',
            '小琪',
            '佳瑞',
            '丫头',
            
      ]


      const studentIndexs = students.map((v, i) => i)

      total.innerHTML = students.length

      students.sort(() => 0.5 - Math.random())


      students.forEach(v => {
        const li = document.createElement('li')
        li.innerHTML = v
        name_box.appendChild(li)
      })


      let intervalId = null;

      btn.addEventListener('click', () => {
        // console.log(students.map((v, i) => i)) // .sort(() => 0.5 - Math.random())
        // console.log(students.map((v, i) => i).sort(() => 0.5 - Math.random()))
        // 已开启,就停止
        if (intervalId) {
          btn.innerHTML = `点击随机抽取 ${size} 位`
          clearInterval(intervalId)
          intervalId = null
        }
        // 未开启,就开启
        else {
          btn.innerHTML = '确定选择'
          intervalId = setInterval(() => {
            [...name_box.children].forEach(li => {
              li.classList.remove('active')
            })
            const selectStudentIndexs = studentIndexs.sort(() => 0.5 - Math.random()).slice(0, size)
            selectStudentIndexs.forEach(v => {
              name_box.children[v].classList.add('active')
            })
          }, 100);
        }
      })


      sizeInput.addEventListener('blur', (e) => {
        // console.log(e.target.value)
        size = parseInt(e.target.value)
        btn.innerHTML = `点击随机抽取 ${size} 位`
      })

    })();

  </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 H5 中调用 Unity 的原生方法,需要通过 WebGL 将 Unity 嵌入到 H5 页面中。下面是一个简单的案例: 1. 创建 Unity 项目并设置 发布 2. 在 Unity 项目中定义要暴露给 H5 的函数,并使用 [DllImport] 特性将其标记为导出函数。例如: ``` public class MyUnityScript : MonoBehaviour { [DllImport("__Internal")] private static extern void MyNativeFunction(string message); public void CallMyNativeFunction(string message) { MyNativeFunction(message); } } ``` 3. 在 H5 页面中插入 Unity Web Player,例如: ``` <div id="unityPlayer"></div> <script type="text/javascript"> var unityInstance = UnityLoader.instantiate("unityPlayer", "Build/UnityWebGL.json"); </script> ``` 4. 在 H5 中调用导出函数,例如: ``` <button onclick="unityInstance.SendMessage('MyGameObject', 'CallMyNativeFunction', 'Hello from H5!')">Call Unity Function</button> ``` 这里通过 UnityInstance.SendMessage() 方法调用了 MyUnityScript.CallMyNativeFunction() 函数,并将消息参数传递给它。 5. 最后在 Unity 中实现 MyNativeFunction() 函数,例如: ``` [DllImport("__Internal")] private static extern void MyNativeFunction(string message); void MyNativeFunction(string message) { Debug.Log("Native function called with message: " + message); } ``` 这样,当 H5 页面中的按钮被点击时,就会调用 MyUnityScript.CallMyNativeFunction() 函数,该函数又会调用 MyNativeFunction() 函数,并在 Unity 控制台中输出消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值