Home > Web Front-end > JS Tutorial > js to achieve double color ball effect

js to achieve double color ball effect

coldplay.xixi
Release: 2020-08-06 16:39:48
forward
3095 people have browsed it

js to achieve double color ball effect

The example in this article shares the specific code for realizing the double-color ball effect in js for your reference. The specific content is as follows

Effect display:

Source code display:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>js实现双色球效果</title>
  <style>
    span{
      font-size: 20px;
    }
    .a {
      color: red;
    }
 
    .b {
      color: blue;
    }
  </style>
</head>
<body>
<button>点击获取今日双色球数</button>
<pre class="brush:php;toolbar:false">
  <span class="a"></span><span class="b"></span>
<script> var arrS = []; for (let i = 1; i < 34; i++) { arrS[i - 1] = i; } var spans = document.querySelectorAll("span"); document.querySelector("button").onclick = function () { /*es6 使用 set 集合会自动去除重复*/ /*获取红球*/ let set = new Set(); while (set.size < 6) { set.add(getR(arrS, arrS.length)); } /*获取篮球*/ let set1 = new Set(); while (set1.size < 1) { set1.add(getR(arrS, 16)); } var hong = ""; var lan = ""; for (let v of set) { hong += v; hong += " "; } for (let v of set1) { lan += v; lan += " "; } //将内容添加到页面上 spans[0].innerText = hong; spans[1].innerText = lan; } // 获取指定范围内的随机数 function getR(arr, al) { return arr[Math.floor(Math.random() * al)]; } </script>
Copy after login

Related learning recommendations: javascript tutorial

The above is the detailed content of js to achieve double color ball effect. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template