forked from wteam-xq/testDemo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer(jq).html
690 lines (600 loc) · 24.1 KB
/
timer(jq).html
1
2
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
<!-- 主流浏览器最小化时会停止timeOut计时,如需考虑前后端同步秒数 必须使用计时器写法2(通过时间戳计时) -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
计时器、时钟
</title>
<style>
.bg{
background-color:#ff4a00;
height:100px;
color:#FFF;
font-size:14px;
margin:10px 0px 0px 0px;
padding: 10px;
border-radius: 10px;
}
.time-val{
color:'#fff';
font-size:30px;
}
.count_down{
font-size:20px;
line-height: 100px;
width:100px;
margin:0 auto;
text-align: center;
}
html{
background: url(./images/timer_bg.jpg) #eb9ba7;
}
/*-------------------------
The clocks
--------------------------*/
#clock{
width:370px;
padding:40px;
margin:30px auto 50px;
position:relative;
}
#clock:after{
content:'';
position:absolute;
width:400px;
height:20px;
border-radius:100%;
left:50%;
margin-left:-200px;
bottom:2px;
z-index:-1;
}
#clock .display{
text-align:center;
padding: 40px 20px 20px;
border-radius:6px;
position:relative;
height: 54px;
}
/*-------------------------
Light color theme
--------------------------*/
#clock.light{
background-color:#f3f3f3;
color:#272e38;
}
#clock.light:after{
box-shadow:0 4px 10px rgba(0,0,0,0.15);
}
#clock.light .digits div span{
background-color:#272e38;
border-color:#272e38;
}
#clock.light .digits div.dots:before,
#clock.light .digits div.dots:after{
background-color:#272e38;
}
#clock.light .display{
background-color:#dddddd;
box-shadow:0 1px 1px rgba(0,0,0,0.08) inset, 0 1px 1px #fafafa;
}
/*-------------------------
Dark color theme
--------------------------*/
#clock.dark{
background-color:#272e38;
color:#cacaca;
}
#clock.dark:after{
box-shadow:0 4px 10px rgba(0,0,0,0.3);
}
#clock.dark .digits div span{
background-color:#cacaca;
border-color:#cacaca;
}
#clock.dark .display{
background-color:#0f1620;
box-shadow:0 1px 1px rgba(0,0,0,0.08) inset, 0 1px 1px #2d3642;
}
#clock.dark .digits div.dots:before,
#clock.dark .digits div.dots:after{
background-color:#cacaca;
}
/*-------------------------
The Digits
--------------------------*/
#clock .digits div{
text-align:left;
position:relative;
width: 28px;
height:50px;
display:inline-block;
margin:0 4px;
}
#clock .digits div span{
opacity:0;
position:absolute;
-webkit-transition:0.25s;
-moz-transition:0.25s;
transition:0.25s;
}
#clock .digits div span:before,
#clock .digits div span:after{
content:'';
position:absolute;
width:0;
height:0;
border:5px solid transparent;
}
#clock .digits .d1{ height:5px;width:16px;top:0;left:6px;}
#clock .digits .d1:before{ border-width:0 5px 5px 0;border-right-color:inherit;left:-5px;}
#clock .digits .d1:after{ border-width:0 0 5px 5px;border-left-color:inherit;right:-5px;}
#clock .digits .d2{ height:5px;width:16px;top:24px;left:6px;}
#clock .digits .d2:before{ border-width:3px 4px 2px;border-right-color:inherit;left:-8px;}
#clock .digits .d2:after{ border-width:3px 4px 2px;border-left-color:inherit;right:-8px;}
#clock .digits .d3{ height:5px;width:16px;top:48px;left:6px;}
#clock .digits .d3:before{ border-width:5px 5px 0 0;border-right-color:inherit;left:-5px;}
#clock .digits .d3:after{ border-width:5px 0 0 5px;border-left-color:inherit;right:-5px;}
#clock .digits .d4{ width:5px;height:14px;top:7px;left:0;}
#clock .digits .d4:before{ border-width:0 5px 5px 0;border-bottom-color:inherit;top:-5px;}
#clock .digits .d4:after{ border-width:0 0 5px 5px;border-left-color:inherit;bottom:-5px;}
#clock .digits .d5{ width:5px;height:14px;top:7px;right:0;}
#clock .digits .d5:before{ border-width:0 0 5px 5px;border-bottom-color:inherit;top:-5px;}
#clock .digits .d5:after{ border-width:5px 0 0 5px;border-top-color:inherit;bottom:-5px;}
#clock .digits .d6{ width:5px;height:14px;top:32px;left:0;}
#clock .digits .d6:before{ border-width:0 5px 5px 0;border-bottom-color:inherit;top:-5px;}
#clock .digits .d6:after{ border-width:0 0 5px 5px;border-left-color:inherit;bottom:-5px;}
#clock .digits .d7{ width:5px;height:14px;top:32px;right:0;}
#clock .digits .d7:before{ border-width:0 0 5px 5px;border-bottom-color:inherit;top:-5px;}
#clock .digits .d7:after{ border-width:5px 0 0 5px;border-top-color:inherit;bottom:-5px;}
/* 1 */
#clock .digits div.one .d5,
#clock .digits div.one .d7{
opacity:1;
}
/* 2 */
#clock .digits div.two .d1,
#clock .digits div.two .d5,
#clock .digits div.two .d2,
#clock .digits div.two .d6,
#clock .digits div.two .d3{
opacity:1;
}
/* 3 */
#clock .digits div.three .d1,
#clock .digits div.three .d5,
#clock .digits div.three .d2,
#clock .digits div.three .d7,
#clock .digits div.three .d3{
opacity:1;
}
/* 4 */
#clock .digits div.four .d5,
#clock .digits div.four .d2,
#clock .digits div.four .d4,
#clock .digits div.four .d7{
opacity:1;
}
/* 5 */
#clock .digits div.five .d1,
#clock .digits div.five .d2,
#clock .digits div.five .d4,
#clock .digits div.five .d3,
#clock .digits div.five .d7{
opacity:1;
}
/* 6 */
#clock .digits div.six .d1,
#clock .digits div.six .d2,
#clock .digits div.six .d4,
#clock .digits div.six .d3,
#clock .digits div.six .d6,
#clock .digits div.six .d7{
opacity:1;
}
/* 7 */
#clock .digits div.seven .d1,
#clock .digits div.seven .d5,
#clock .digits div.seven .d7{
opacity:1;
}
/* 8 */
#clock .digits div.eight .d1,
#clock .digits div.eight .d2,
#clock .digits div.eight .d3,
#clock .digits div.eight .d4,
#clock .digits div.eight .d5,
#clock .digits div.eight .d6,
#clock .digits div.eight .d7{
opacity:1;
}
/* 9 */
#clock .digits div.nine .d1,
#clock .digits div.nine .d2,
#clock .digits div.nine .d3,
#clock .digits div.nine .d4,
#clock .digits div.nine .d5,
#clock .digits div.nine .d7{
opacity:1;
}
/* 0 */
#clock .digits div.zero .d1,
#clock .digits div.zero .d3,
#clock .digits div.zero .d4,
#clock .digits div.zero .d5,
#clock .digits div.zero .d6,
#clock .digits div.zero .d7{
opacity:1;
}
/* The dots */
#clock .digits div.dots{
width:5px;
}
#clock .digits div.dots:before,
#clock .digits div.dots:after{
width:5px;
height:5px;
content:'';
position:absolute;
left:0;
top:14px;
}
#clock .digits div.dots:after{
top:34px;
}
/*-------------------------
Weekdays
--------------------------*/
#clock .weekdays{
font-size:12px;
position:absolute;
width:100%;
top:10px;
left:0;
text-align:center;
}
#clock .weekdays span{
opacity:0.2;
padding:0 10px;
}
#clock .weekdays span.active{
opacity:1;
}
/*-------------------------
AM/PM
--------------------------*/
#clock .ampm{
position:absolute;
bottom:20px;
right:20px;
font-size:12px;
}
/*-------------------------
Button
--------------------------*/
.button-holder{
text-align:center;
padding-bottom:100px;
}
a.button{
background-color:#f6a7b3;
background-image:-webkit-linear-gradient(top, #f6a7b3, #f0a3af);
background-image:-moz-linear-gradient(top, #f6a7b3, #f0a3af);
background-image:linear-gradient(top, #f6a7b3, #f0a3af);
border:1px solid #eb9ba7;
border-radius:2px;
box-shadow:0 2px 2px #ccc;
color:#fff;
text-decoration: none !important;
padding:15px 20px;
display:inline-block;
cursor:pointer;
position:relative;
z-index: 1;
}
a.button:hover{
opacity:0.9;
}
</style>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<div id="con_apa" , class='bg'>
倒计时
<span> 时间(秒):</span>
<span id='time_val' class='time-val'> </span>
<input type="button" class='on' id='timeBtn' value='启动'>
<input type="button" id='resetTimeBtn' value='复位时间'>
</div>
<!-- <div id="con_apa2" , class='bg'>
</div> -->
<div id="clock" class="light">
<div class="display">
<div class="weekdays"></div>
<div class="ampm"></div>
<div class="digits"></div>
</div>
</div>
<div class="button-holder">
<a id="switch-theme" class="button">Switch Theme</a>
</div>
<script src="lib/jquery-1.10.2.min.js", type="text/javascript"></script>
<script src="lib/moment.min.js"></script>
<script>
$(function() {
// 计时器写法1
function countDown1_init() {
var val_time = 60,
isFirstStart = true,
startTime = 0,
timer = null,
flag = true;
$('#time_val').text(val_time);
$('#timeBtn').on('click', startOrStopTime)
$('#resetTimeBtn').on('click', resetTime)
// 启动、关闭时间
function startOrStopTime(){
var _val_time = 0;
$this = $(this);
if ( $this.hasClass('on') ){
$this.val('关闭');
$this.removeClass('on').addClass('off');
flag = true;
if(isFirstStart){
startTime = new Date().getTime();
isFirstStart = false;
}
_val_time = parseInt( $('#time_val').text(), 10 );
// 获取相对于上次停止时间的时间值
if (_val_time < 60){
startTime = new Date().getTime() - (60 - _val_time) * 1000;
}
timer = setTimeout(updateTime, 1000);
}else if( $this.hasClass('off') ){
$this.val('启动');
$this.removeClass('off').addClass('on');
flag = false;
}
}
// 复位时间
function resetTime(){
val_time = 60;
// isFirstStart = true;
startTime = new Date().getTime();
$('#time_val').text(val_time);
}
function updateTime(){
var nowTime = new Date().getTime();
val_time = Math.floor( (nowTime - startTime)/1000 );
if (val_time > 60 || val_time < 0){
val_time = 0;
}
val_time = 60 - val_time;
if (val_time == 0 || !flag) {
clearTimeout(timer);
return;
}
$('#time_val').text(val_time);
timer = setTimeout(updateTime, 1000);
}
}
// var $countDownWrap = $('#con_apa2');
/**
* 3分钟的倒计时(计时器写法2): 实际项目中, 计时器与业务逻辑解耦优雅实现
*/
var CountDown2 = {
//倒计时的总时间
totalTime : 3 * 60,
count : 0,
//启动倒计时的初始时间
startTime : 0,
// 计时器的速度
time : 1000,
countDownObj : null,
/**
* 初始化
* 初始化节点,需要传父节点对象作为参数
* @param {object} parent 计时器容器节点,jq对象
*/
init : function(parent) {
if (parent && parent.length > 0) {
this.initDom(parent);
}
},
minObj : null,
secObj : null,
initDom : function(parent) {
var dom_str = '',
$countDownDom = null;
dom_str = '<div class="count_down">' +
'<span class="min">03</span>' +
'<span class="separate">:</span>' +
'<span class="sec">00</span>' +
'</div>';
$countDownDom = $(dom_str);
parent.append($countDownDom);
this.countDownObj = parent.find('.count_down');
this.minObj = this.countDownObj.find('.min');
this.secObj = this.countDownObj.find('.sec');
},
callback : null,
start : function(callback) {
var This = this, date = new Date();
this.callback = callback;
this.startTime = date.getTime();
window.setTimeout(function() {
This.startTimer();
}, this.time);
},
/**
* 开始 倒计时
*/
startTimer : function() {
var This = this, timeObj = null, nowTime = new Date().getTime();
this.count = parseInt( (nowTime - this.startTime)/1000, 10 );
if (this.count >= this.totalTime) {//到时间了
if ( typeof this.callback === 'function') {
this.callback();
}
this.reset();
this.startTimer();
return false;
}
timeObj = this.calculateTime(this.totalTime - this.count);
// 根据数字更新 节点信息, 分钟 跟 秒数各一个变量即可
this.updateDom(timeObj);
this.timeout = window.setTimeout(function() {
This.startTimer();
}, this.time);
},
stopTimer : function() {
window.clearTimeout(this.timeout);
},
calculateTime: function(sec){
// 根据传入的秒数计算出 分钟及秒数
var min, sec, result;
if (sec == 0){
result = {
'min': '00',
'sec': '00'
};
}
min = Math.floor( sec/60 );
sec = sec % 60;
result = {
'min': doubleNum(min),
'sec': doubleNum(sec)
};
return result;
function doubleNum(num){
var rel = 0;
if (num >= 10) {
rel = num;
}else {
rel = '0' + num;
}
return rel;
}
},
/**
* 重新设置 初始的时间
*/
reset : function() {
this.count = 0;
this.startTime = new Date().getTime();
this.minObj.html('00');
this.secObj.html('00');
},
/**
* 更新 节点
*/
updateDom : function(timeObj) {
this.minObj.html(timeObj.min);
this.secObj.html(timeObj.sec);
}
};
// 时间到了后的业务函数
function timeOutFn(){
alert('倒计时间到,处理业务吧!');
}
// 计时器写法3(xxx天xxx时xxx分xxx秒)
// opt.type(int 非必填)
// 0: 默认方式 单一dom节点显示“xxx天xxx时xxx分xxx秒”
// 1: 扩展方式 xxx天 xxx时 xxx分 xxx秒 四个dom节点显示
// opt.startTime(int 非必填) 倒计时起始时间默认为当前系统时间(业务上一般传入服务器获取的北京时间)的时间戳
// opt.endTime(int 必填) 倒计时结束时间的时间戳
// opt.countdownEvent(非必填) 每过一秒回调函数,回调函数包含参数{'d': 'xxx','h':'xxx','m':'xxx','s':'xxx'}
var CountDown3 = {
startTime : 0,
endTime: 0,
init: function(opt){},
start: function(){},
stop: function(){},
reset: function(){}
};
// 计时器3 私有方法(待更新:将使用寄生组合式继承实现:https://www.zhihu.com/question/22232912)
// 计时器1启动
countDown1_init();
// 计时器2启动
// CountDown2.init($countDownWrap);
// CountDown2.start(timeOutFn);
// 数字时钟启动(该数字时钟效果代码来自互联网:http://www.cnblogs.com/i7758/p/5081602.html)
initDigitsTimer();
function initDigitsTimer(){
var clock = $('#clock'),
ampm = clock.find('.ampm');
// Map digits to their names (this will be an array)
var digit_to_name = 'zero one two three four five six seven eight nine'.split(' ');
// This object will hold the digit elements
var digits = {};
// Positions for the hours, minutes, and seconds
var positions = [
'h1', 'h2', ':', 'm1', 'm2', ':', 's1', 's2'
];
// Generate the digits with the needed markup,
// and add them to the clock
var digit_holder = clock.find('.digits');
$.each(positions, function(){
var digit_item_str = '';
if(this == ':'){
digit_holder.append('<div class="dots">');
} else {
var pos = $('<div>');
for(var i=1; i<8; i++){
digit_item_str += '<span class="d' + i + '"></span>';
}
pos.append(digit_item_str);
// Set the digits as key:value pairs in the digits object
digits[this] = pos;
// Add the digit elements to the page
digit_holder.append(pos);
}
});
// Add the weekday names
var weekday_names = 'MON TUE WED THU FRI SAT SUN'.split(' '),
weekday_holder = clock.find('.weekdays');
$.each(weekday_names, function(){
weekday_holder.append('<span>' + this + '</span>');
});
var weekdays = clock.find('.weekdays span');
// Run a timer every second and update the clock
(function update_time(){
// Use moment.js to output the current time as a string
// hh is for the hours in 12-hour format,
// mm - minutes, ss-seconds (all with leading zeroes),
// d is for day of week and A is for AM/PM
var now = moment().format("hhmmssdA");
digits.h1.attr('class', digit_to_name[now[0]]);
digits.h2.attr('class', digit_to_name[now[1]]);
digits.m1.attr('class', digit_to_name[now[2]]);
digits.m2.attr('class', digit_to_name[now[3]]);
digits.s1.attr('class', digit_to_name[now[4]]);
digits.s2.attr('class', digit_to_name[now[5]]);
// The library returns Sunday as the first day of the week.
// Stupid, I know. Lets shift all the days one position down,
// and make Sunday last
var dow = now[6];
dow--;
// Sunday!
if(dow < 0){
// Make it last
dow = 6;
}
// Mark the active day of the week
weekdays.removeClass('active').eq(dow).addClass('active');
// Set the am/pm text:
ampm.text(now[7]+now[8]);
// Schedule this function to be run again in 1 sec
setTimeout(update_time, 1000);
})();
// Switch the theme
$('#switch-theme').click(function(){
clock.toggleClass('light dark');
});
}
});
</script>
</body>
</html>