欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js仿微信抢红包功能

程序员文章站 2022-09-08 16:33:02
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下

本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>仿微信抢红包</title>
    <style>
      html,body,div{margin:0;padding:0;}
      body{background:#eaeaea;font:16px/1.8 "微软雅黑";padding-bottom:20px}
      input{margin:0;display:inline-block;border:1px solid #ddd;padding:4px 2px;font-size:16px;font-family:"微软雅黑";margin-right: 5px;}
      input:focus{border-color:#3c9bd1;outline:none;}
      
      .wrap,.list { margin: 50px auto; width: 300px;}
      .title{  font-size: 42px;  color: #464646;text-align: center;}
      .line{height:40px;line-height:40px;text-align: center;}
      .btn{display:block;background:#3c9bd1;color:#fff;line-height: 40px;height:40px;text-align: center;width:200px;margin:0 auto;margin-top:50px;text-decoration: none;transition:all .3s linear;border-radius: 2px;}
      .btn:hover{opacity:.9;}
      .list{width:500px;border:1px solid #dbdbdb;padding:10px;background:#f5f5f5;text-align: center;}
      .list p span {color: red; padding: 0 8px;}
      .list p:empty{background: #000000;}
      .list:empty{display: none;}
      .link{position:fixed;height:20px;font-size: 12px;color:#999;text-align: center;width: 100%;bottom:0;line-height:20px;margin:0;padding:0;  background: #eaeaea;padding:5px 0;}
      .link a{font-size:12px;color:#999;}
    </style>
  </head>
  <body>
    <h1 class="title">javascript实现仿微信抢红包</h1>
    <div class="wrap">
      <div class="line">红包个数:<input type="text" name="packetnumber" value="5" onkeyup="this.value=this.value.replace(/\d/g,'')" onafterpaste="this.value=this.value.replace(/\d/g,'')" maxlength="10">个</div>
      <div class="line">总 金 额:<input type="text" name="money" value="5" onkeyup="if(isnan(value))execcommand('undo')" onafterpaste="if(isnan(value))execcommand('undo')" maxlength="10">元</div>
      <div class="line"><a class="btn" href="javascript:;" rel="external nofollow" >发红包</a></div>
    </div>
    <div class="list"></div>
    <p class="link">参考<a target="_blank" href="https://www.zybuluo.com/yulin718/note/93148">《微信红包的架构设计简介》</a>文章</p>

<script>
 "use strict";

var _createclass = function() {
  function defineproperties(target, props) {
    for (var i = 0; i < props.length; i++) {
      var descriptor = props[i];
      descriptor.enumerable = descriptor.enumerable || false;
      descriptor.configurable = true;
      if ("value" in descriptor)
        descriptor.writable = true;
      object.defineproperty(target, descriptor.key, descriptor);
    }
  }
  return function(constructor, protoprops, staticprops) {
    if (protoprops)
      defineproperties(constructor.prototype, protoprops);
    if (staticprops)
      defineproperties(constructor, staticprops);
    return constructor;
  }
  ;
}();

function _classcallcheck(instance, constructor) {
  if (!(instance instanceof constructor)) {
    throw new typeerror("cannot call a class as a function");
  }
}

var moneypacket = function() {
  function moneypacket(packnumber, money) {
    _classcallcheck(this, moneypacket);
    
    this.min = 0.01;
    this.flag = true;
    this.packnumber = packnumber;
    this.money = money;
    if (!this.checkpackage()) {
      this.flag = false;
      return {
        "flag": this.flag
      };
    }
  }
  
  _createclass(moneypacket, [{
    key: "checkpackage",
    value: function checkpackage() {
      if (this.packnumber == 0) {
        alert("至少需要设置1个红包");
        return false;
      }
      if (this.money <= 0) {
        alert("红包总金额不能小于0");
        return false;
      }
      if (this.packnumber * this.min > this.money) {
        alert("单个红包金额不可低于0.01元");
        return false;
      }
      return true;
    }
  }]);
  
  return moneypacket;
}();

var getrandommoney = function getrandommoney(packet) {
  if (packet.packnumber == 0) {
    return;
  }
  if (packet.packnumber == 1) {
    var _lastmoney = math.round(packet.money * 100) / 100;
    packet.packnumber--;
    packet.money = 0;
    return _lastmoney;
  }
  var min = 0.01
   , 
  max = packet.money / packet.packnumber * 2
   , 
  money = math.random() * max;
  money = money < min ? min : money;
  money = math.floor(money * 100) / 100;
  packet.packnumber--;
  packet.money = math.round((packet.money - money) * 100) / 100;
  return money;
}
;

(function() {
  var obtn = document.queryselector(".btn");
  var olist = document.queryselector(".list");
  
  obtn.onclick = function() {
    var packetnumber = +document.queryselector("input[name=packetnumber]").value;
    var money = +document.queryselector("input[name=money]").value;
    var str = "";
    
    var packet = new moneypacket(packetnumber,money)
     , 
    num = packet.flag && packet.packnumber || 0;
    console.log("========================================================================");
    for (var i = 0; i < num; i++) {
      var _pack = getrandommoney(packet);
      str += "<p>第<span>" + i + "</span>个红包:: <span>" + _pack.tofixed(2) + "</span>元  ==剩余红包:: <span>" + packet.money.tofixed(2) + "</span> 元<p>";
      console.log("第", i, "个红包::", _pack.tofixed(2), "元   ==剩余红包::", packet.money.tofixed(2), "元");
    }
    str !== "" && (olist.innerhtml = str);
  }
  ;
})();

</script>
  </body>
</html>


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