| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 
 |   <!DOCTYPE html><html lang="zh">
 
 <head>
 <meta charset="UTF-8">
 <title>Document</title>
 </head>
 <style>
 #wrapper {
 position: absolute;
 left: 50%;
 top: 50%;
 margin-left: -150px;
 margin-top: -100px;
 width: 300px;
 height: 200px;
 text-align: center;
 }
 
 .count-box label,
 .amount-box label {
 display: inline-block;
 width: 70px;
 }
 
 .send {
 margin-top: 10px;
 padding: 5px 0;
 background-color: #63B8FF;
 width: 140px;
 color: white;
 cursor: pointer;
 }
 /* 红包 */
 #red-box {
 display: none;
 overflow: hidden;
 position: absolute;
 left: 50%;
 top: 50%;
 width: 300px;
 height: 450px;
 transform: translate(-50%, -50%);
 background-color: #B22222;
 border-radius: 10px;
 }
 .close {
 position: absolute;
 right: 20px;
 top: 20px;
 z-index: 1000;
 cursor: pointer;
 user-select: none;
 color: #fff;
 }
 .user-info,
 .show-info {
 width: 100%;
 text-align: center;
 }
 .show-info {
 display: none;
 }
 .user-info img,
 .show-info img {
 height: 80px;
 width: 80px;
 border-radius: 40px;
 margin-top: 20px;
 }
 .slide {
 position: absolute;
 top: 0;
 left: 0;
 height: 300px;
 width: 300px;
 background-color: #EE6A50;
 border-radius: 0 0 50px 50px;
 }
 .slide-animation {
 animation: slideTop .5s linear forwards;
 }
 @keyframes slideTop {
 0% { height: 300px; }
 100% { height: 60px; }
 }
 .open {
 position: absolute;
 bottom: -40px;
 left: 50%;
 margin-left: -40px;
 width: 80px;
 height: 80px;
 line-height: 80px;
 text-align: center;
 background: orange;
 border-radius: 40px;
 border: 1px solid #fff;
 cursor: pointer;
 animation: openSlide 1s infinite forwards;
 
 }
 @keyframes openSlide {
 0% { transform: rotateY(0deg); }
 100% { transform: rotateY(380deg); }
 }
 .title {
 color: #fff;
 font-weight: bold;
 }
 .own-money {
 color: #fff;
 font-size: 20px;
 font-weight: bold;
 }
 .money-rank {
 color: #fff;
 font-size: 12px;
 font-weight: bold;
 }
 /* 随机产生红包 */
 .user-receive {
 background-color: rgba(255, 165, 0, .5);
 margin: 10px auto;
 height: 200px;
 width: 200px;
 border-radius: 10px;
 padding: 2px 5px;
 overflow: auto;
 }
 .user-receive p {
 text-align: left;
 color: #fff;
 font-size: 12px;
 margin: 5px;
 }
 .user-receive span {
 margin-left: 5px;
 color: yellow;
 font-size: 8px;
 }
 
 
 
 
 <div id="wrapper">
 <h1>红包模拟器</h1>
 <div class="count-box">
 <label for="count">红包个数</label>
 
 </div>
 <div class="amount-box">
 <label for="amount">总金额</label>
 
 </div>
 <button class="send">发红包</button>
 </div>
 
 <div id="red-box">
 <div class="close">X</div>
 <div class="slide">
 <div class="user-info">
 <img src="http://s3a.pstatp.com/cg_growth/resource/boilerplate/images/redpacket/avatar.png">
 <p class="title">JScoder</p>
 <p>给您发送了一个红包</p>
 <p class="title">恭喜发财 大吉大利</p>
 </div>
 <div class="show-info">
 <img src="http://s3a.pstatp.com/cg_growth/resource/boilerplate/images/redpacket/avatar.png">
 <p class="own-money"></p>
 <p class="money-rank">红包领取榜</p>
 
 <div class="user-receive"></div>
 </div>
 <div class="open">拆红包</div>
 </div>
 </div>
 
 let send = document.querySelector('.send')
 let close = document.querySelector('.close')
 let redBox = document.querySelector('#red-box')
 let open = document.querySelector('.open')
 let slide = document.querySelector('.slide')
 let sendRed = document.querySelector('.send-red')
 let userInfo = document.querySelector('.user-info')
 let showInfo = document.querySelector('.show-info')
 
 send.onclick = function () {
 // 当红包个数或者总金额为0时,提示需要输入
 let count = document.querySelector('#count').value
 let amount = document.querySelector('#amount').value
 if (count == '' || amount == '') {
 alert('输入有问题!请输入红包个数或者总金额数不能为空')
 return
 }
 if (count == '0' || amount == '0') {
 alert('输入有问题!输入的红包个数或者总金额数不能为0')
 return
 }
 redBox.style.display = 'block'
 }
 close.onclick = function () {
 redBox.style.display = 'none'
 userInfo.style.display = 'block'
 showInfo.style.display = 'none'
 open.style.display = 'block'
 slide.className = 'slide'
 }
 // 拆红包
 open.onclick = function () {
 randomMoney()
 this.style.display = 'none'
 userInfo.style.display = 'none'
 showInfo.style.display = 'block'
 slide.className = 'slide slide-animation'
 }
 // 随机产生金额
 function randomMoney () {
 let count = document.querySelector('#count').value
 let amount = document.querySelector('#amount').value
 let ownMoney = document.querySelector('.own-money')
 let userReceive = document.querySelector('.user-receive')
 let oSpan = document.createElement('span')
 let max = 0.01 // 记录运气最佳
 let key = 0 // 记录运气最佳的下标
 let everyMoney = randomFn(amount, count)
 userReceive.innerHTML = '' // 点击关闭时对他进行一个重置
 everyMoney.forEach((item, index) => {
 if (max <= item) {
 max = item
 key = index
 }
 let p = document.createElement('p')
 p.innerText = `用户${index+1}:${item}`
 userReceive.appendChild(p)
 })
 ownMoney.innerText = max
 oSpan.innerText = '运气最佳'
 userReceive.children[key].appendChild(oSpan)
 }
 // 随机分配函数
 // 思路:每次0.01<=得出的红包值<=剩余金额-剩余人数*0.01,最后一个人获得剩余全部
 function randomFn (amount, count) {
 let total = amount
 let arr = []
 for (let i = 0 ; i < count - 1 ; i++) {
 // 改为向上取整,保证每个红包的值至少为0.01
 let one = Math.ceil(Math.random() * (total - (count - 1 - i) * 0.01) * 100) / 100
 arr.push(one)
 total -= one
 }
 arr.push(Number(total.toFixed(2)))
 return arr
 }
 
 
 
 
 
 |