1-add jQuery.countdown
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/*! * The Final Countdown for jQuery v2.2.0 (http://hilios.github.io/jQuery.countdown/) * Copyright (c) 2016 Edson Hilios * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ !function(a){"use strict";"function"==typeof define&&define.amd?define(["jquery"],a):a(jQuery)}(function(a){"use strict";function b(a){if(a instanceof Date)return a;if(String(a).match(g))return String(a).match(/^[0-9]*$/)&&(a=Number(a)),String(a).match(/\-/)&&(a=String(a).replace(/\-/g,"/")),new Date(a);throw new Error("Couldn't cast `"+a+"` to a date object.")}function c(a){var b=a.toString().replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1");return new RegExp(b)}function d(a){return function(b){var d=b.match(/%(-|!)?[A-Z]{1}(:[^;]+;)?/gi);if(d)for(var f=0,g=d.length;f<g;++f){var h=d[f].match(/%(-|!)?([a-zA-Z]{1})(:[^;]+;)?/),j=c(h[0]),k=h[1]||"",l=h[3]||"",m=null;h=h[2],i.hasOwnProperty(h)&&(m=i[h],m=Number(a[m])),null!==m&&("!"===k&&(m=e(l,m)),""===k&&m<10&&(m="0"+m.toString()),b=b.replace(j,m.toString()))}return b=b.replace(/%%/,"%")}}function e(a,b){var c="s",d="";return a&&(a=a.replace(/(:|;|\s)/gi,"").split(/\,/),1===a.length?c=a[0]:(d=a[0],c=a[1])),Math.abs(b)>1?c:d}var f=[],g=[],h={precision:100,elapse:!1,defer:!1};g.push(/^[0-9]*$/.source),g.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source),g.push(/[0-9]{4}([\/\-][0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source),g=new RegExp(g.join("|"));var i={Y:"years",m:"months",n:"daysToMonth",d:"daysToWeek",w:"weeks",W:"weeksToMonth",H:"hours",M:"minutes",S:"seconds",D:"totalDays",I:"totalHours",N:"totalMinutes",T:"totalSeconds"},j=function(b,c,d){this.el=b,this.$el=a(b),this.interval=null,this.offset={},this.options=a.extend({},h),this.instanceNumber=f.length,f.push(this),this.$el.data("countdown-instance",this.instanceNumber),d&&("function"==typeof d?(this.$el.on("update.countdown",d),this.$el.on("stoped.countdown",d),this.$el.on("finish.countdown",d)):this.options=a.extend({},h,d)),this.setFinalDate(c),this.options.defer===!1&&this.start()};a.extend(j.prototype,{start:function(){null!==this.interval&&clearInterval(this.interval);var a=this;this.update(),this.interval=setInterval(function(){a.update.call(a)},this.options.precision)},stop:function(){clearInterval(this.interval),this.interval=null,this.dispatchEvent("stoped")},toggle:function(){this.interval?this.stop():this.start()},pause:function(){this.stop()},resume:function(){this.start()},remove:function(){this.stop.call(this),f[this.instanceNumber]=null,delete this.$el.data().countdownInstance},setFinalDate:function(a){this.finalDate=b(a)},update:function(){if(0===this.$el.closest("html").length)return void this.remove();var b,c=void 0!==a._data(this.el,"events"),d=new Date;b=this.finalDate.getTime()-d.getTime(),b=Math.ceil(b/1e3),b=!this.options.elapse&&b<0?0:Math.abs(b),this.totalSecsLeft!==b&&c&&(this.totalSecsLeft=b,this.elapsed=d>=this.finalDate,this.offset={seconds:this.totalSecsLeft%60,minutes:Math.floor(this.totalSecsLeft/60)%60,hours:Math.floor(this.totalSecsLeft/60/60)%24,days:Math.floor(this.totalSecsLeft/60/60/24)%7,daysToWeek:Math.floor(this.totalSecsLeft/60/60/24)%7,daysToMonth:Math.floor(this.totalSecsLeft/60/60/24%30.4368),weeks:Math.floor(this.totalSecsLeft/60/60/24/7),weeksToMonth:Math.floor(this.totalSecsLeft/60/60/24/7)%4,months:Math.floor(this.totalSecsLeft/60/60/24/30.4368),years:Math.abs(this.finalDate.getFullYear()-d.getFullYear()),totalDays:Math.floor(this.totalSecsLeft/60/60/24),totalHours:Math.floor(this.totalSecsLeft/60/60),totalMinutes:Math.floor(this.totalSecsLeft/60),totalSeconds:this.totalSecsLeft},this.options.elapse||0!==this.totalSecsLeft?this.dispatchEvent("update"):(this.stop(),this.dispatchEvent("finish")))},dispatchEvent:function(b){var c=a.Event(b+".countdown");c.finalDate=this.finalDate,c.elapsed=this.elapsed,c.offset=a.extend({},this.offset),c.strftime=d(this.offset),this.$el.trigger(c)}}),a.fn.countdown=function(){var b=Array.prototype.slice.call(arguments,0);return this.each(function(){var c=a(this).data("countdown-instance");if(void 0!==c){var d=f[c],e=b[0];j.prototype.hasOwnProperty(e)?d[e].apply(d,b.slice(1)):null===String(e).match(/^[$A-Z_][0-9A-Z_$]*$/i)?(d.setFinalDate.call(d,e),d.start()):a.error("Method %s does not exist on jQuery.countdown".replace(/\%s/gi,e))}else new j(this,b[0],b[1])})}}); |
2-add html template and counter place holder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<script type="text/template" id="main-example-template"> <div class="time <%= label %>"> <span class="count curr top"><%= curr %></span> <span class="count next top"><%= next %></span> <span class="count next bottom"><%= next %></span> <span class="count curr bottom"><%= curr %></span> <span class="label"><%= label.length < 6 ? label : label.substr(0, 3) %></span> </div> </script> <div class="main-example"> <p> Offer end soon! </p> <div class="countdown-container"> <div id="main-example"></div></div></div> |
3-add css for the counter
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 |
.time.weeks.flip { display: none; } .time.اسبوع.flip { display: none; } .time.اسبوع{ display: none; } .main-example { margin: 0 auto; width: 355px; } .main-example .countdown-container { height: 130px; } .main-example .time { border-radius: 5px; box-shadow: 0 0 10px 0 rgba(0,0,0,0.5); display: inline-block; text-align: center; position: relative; height: 95px; width: 65px; -webkit-perspective: 479px; -moz-perspective: 479px; -ms-perspective: 479px; -o-perspective: 479px; perspective: 479px; -webkit-backface-visibility: hidden; -moz-backface-visibility: hidden; -ms-backface-visibility: hidden; -o-backface-visibility: hidden; backface-visibility: hidden; -webkit-transform: translateZ(0); -moz-transform: translateZ(0); -ms-transform: translateZ(0); -o-transform: translateZ(0); transform: translateZ(0); -webkit-transform: translate3d(0,0,0); -moz-transform: translate3d(0,0,0); -ms-transform: translate3d(0,0,0); -o-transform: translate3d(0,0,0); transform: translate3d(0,0,0); } .main-example .count { background: #202020; color: #f8f8f8; display: block; font-family: 'Oswald', sans-serif; font-size: 2em; line-height: 2.85em; overflow: hidden; position: absolute; text-align: center; text-shadow: 0 0 10px rgba(0, 0, 0, 0.8); top: 0; width: 100%; -webkit-transform: translateZ(0); -moz-transform: translateZ(0); -ms-transform: translateZ(0); -o-transform: translateZ(0); transform: translateZ(0); -webkit-transform-style: flat; -moz-transform-style: flat; -ms-transform-style: flat; -o-transform-style: flat; transform-style: flat; } .main-example .count.top { border-top: 1px solid rgba(255,255,255,0.2); border-bottom: 1px solid rgba(255,255,255,0.1); border-radius: 5px 5px 0 0; height: 50%; -webkit-transform-origin: 50% 100%; -moz-transform-origin: 50% 100%; -ms-transform-origin: 50% 100%; -o-transform-origin: 50% 100%; transform-origin: 50% 100%; } .main-example .count.bottom { background-image: linear-gradient(rgba(255,255,255,0.1), transparent); background-image: -webkit-linear-gradient(rgba(255,255,255,0.1), transparent); background-image: -moz-linear-gradient(rgba(255,255,255,0.1), transparent); background-image: -ms-linear-gradient(rgba(255,255,255,0.1), transparent); background-image: -o-linear-gradient(rgba(255,255,255,0.1), transparent); border-top: 1px solid #000; border-bottom: 1px solid #000; border-radius: 0 0 5px 5px; line-height: 0; height: 50%; top: 50%; -webkit-transform-origin: 50% 0; -moz-transform-origin: 50% 0; -ms-transform-origin: 50% 0; -o-transform-origin: 50% 0; transform-origin: 50% 0; } .main-example .count.next { } .main-example .label { font-size: normal; margin-top: 5px; display: block; position: absolute; top: 95px; width: 100%; } .main-example .label { right: 0px !important; } /* Animation start */ .main-example .count.curr.top { -webkit-transform: rotateX(0deg); -moz-transform: rotateX(0deg); -ms-transform: rotateX(0deg); -o-transform: rotateX(0deg); transform: rotateX(0deg); z-index: 3; } .main-example .count.next.bottom { -webkit-transform: rotateX(90deg); -moz-transform: rotateX(90deg); -ms-transform: rotateX(90deg); -o-transform: rotateX(90deg); transform: rotateX(90deg); z-index: 2; } /* Animation end */ .main-example .flip .count.curr.top { -webkit-transition: all 250ms ease-in-out; -moz-transition: all 250ms ease-in-out; -ms-transition: all 250ms ease-in-out; -o-transition: all 250ms ease-in-out; transition: all 250ms ease-in-out; -webkit-transform: rotateX(-90deg); -moz-transform: rotateX(-90deg); -ms-transform: rotateX(-90deg); -o-transform: rotateX(-90deg); transform: rotateX(-90deg); } .main-example .flip .count.next.bottom { -webkit-transition: all 250ms ease-in-out 250ms; -moz-transition: all 250ms ease-in-out 250ms; -ms-transition: all 250ms ease-in-out 250ms; -o-transition: all 250ms ease-in-out 250ms; transition: all 250ms ease-in-out 250ms; -webkit-transform: rotateX(0deg); -moz-transform: rotateX(0deg); -ms-transform: rotateX(0deg); -o-transform: rotateX(0deg); transform: rotateX(0deg); } @media screen and (max-width: 48em) { .main-example { width: 100%; } .main-example .countdown-container { height: 100px; } .main-example .time { height: 70px; width: 48px; } .main-example .count { font-size: 1.5em; line-height: 70px; } .main-example .label { font-size: 0.8em; top: 72px; } } |
4-configure and call the counter
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 |
<script> $(window).on('load', function() { var labels = [ 'يوم', 'ساعة', 'دقيقة', 'ثانية'], nextYear = (new Date().getFullYear()) + '/01/01', template = _.template($('#main-example-template').html()), currDate = '00:00:00:00:00', nextDate = '00:00:00:00:00', parser = /([0-9]{2})/gi, $example = $('#main-example'); // Parse countdown string to an object function strfobj(str) { var parsed = str.match(parser), obj = {}; labels.forEach(function(label, i) { obj[label] = parsed[i] }); return obj; } // Return the time components that diffs function diff(obj1, obj2) { var diff = []; labels.forEach(function(key) { if (obj1[key] !== obj2[key]) { diff.push(key); } }); return diff; } // Build the layout var initData = strfobj(currDate); labels.forEach(function(label, i) { $example.append(template({ curr: initData[label], next: initData[label], label: label })); }); // Starts the countdown /* $example.countdown(nextYear, function(event) { var newDate = event.strftime('%w:%d:%H:%M:%S'), data; if (newDate !== nextDate) { currDate = nextDate; nextDate = newDate; // Setup the data data = { 'curr': strfobj(currDate), 'next': strfobj(nextDate) }; // Apply the new values to each node that changed diff(data.curr, data.next).forEach(function(label) { var selector = '.%s'.replace(/%s/, label), $node = $example.find(selector); // Update the node $node.removeClass('flip'); $node.find('.curr').text(data.curr[label]); $node.find('.next').text(data.next[label]); // Wait for a repaint to then flip _.delay(function($node) { $node.addClass('flip'); }, 50, $node); }); } }); */ $example.countdown('<?php echo $year; ?>/<?php echo $m; ?>/<?php echo $days; ?>', function(event) { var newDate = event.strftime('%d:%H:%M:%S'), data; if (newDate !== nextDate) { currDate = nextDate; nextDate = newDate; // Setup the data data = { 'curr': strfobj(currDate), 'next': strfobj(nextDate) }; // Apply the new values to each node that changed diff(data.curr, data.next).forEach(function(label) { var selector = '.%s'.replace(/%s/, label), $node = $example.find(selector); // Update the node $node.removeClass('flip'); $node.find('.curr').text(data.curr[label]); $node.find('.next').text(data.next[label]); // Wait for a repaint to then flip _.delay(function($node) { $node.addClass('flip'); }, 50, $node); }); } }); }); </script> |
can you pls guide me where to paste the 4th code?
in the same html page