(function() {
/* Station Style Clock - [email protected] */
/* ^^^^^^^^^^^^^^^ Config below ^^^^^^^^^^^^^^^ */
var clockSize = 400;
var clockColour = 'rgb(255,255,255)';
var secHandColour = 'rgb(255,255,255)';
/* ^^^^^^^^^^^^^^^^^ End config ^^^^^^^^^^^^^^^^ */
var d = document;
var mcon = [];
var mrkrs = [];
var e = 360/60;
var degr = -6;
var mls = 100;
var prev = performance.now();
var radi = Math.PI/180;
var offs = 90 * radi;
var rndId = 'id'+Math.random() * 1;
var secSpan = '.8s';
var minSpan = '1s';
var houSpan = '1s';
var secIncr = 0;
var minIncr = 0;
var houIncr = 0;
var secDeg, minDeg, houDeg, preSec, preMin, preHou;
var idx = d.getElementsByTagName('div').length;
var shdcol = 'rgba(0,0,0,0.6)';
var eiatf = ' translateZ(0); animation-timing-function: ease-in';
var eoatf = ' translateZ(0); animation-timing-function: ease-out';
d.write('<div id = "'+rndId+'" style="display:inline-block;line-height:0px;"></div>');
function xy (a) {
return (a * (clockSize * 2)/100);
}
function shdw(s, h) {
var depth = xy(h);
var angle = s * radi;
var vsa = depth * Math.cos(angle);
var hsa = depth * Math.sin(angle);
return {vsa:vsa, hsa:hsa}
}
var dial = d.createElement('div');
dial.setAttribute('style', 'display: inline-block;'
+'position: relative;'
+'height: '+xy(100)+'px;'
+'width: '+xy(100)+'px;'
+'background-color:transparent;'
+'border-radius: 50%;'
+'margin: -'+xy(24)+'px -'+xy(24)+'px -'+xy(24)+'px -'+xy(24)+'px;');
d.getElementById(rndId).style.transform = 'scale3d(.5,.5,1)';
d.getElementById(rndId).appendChild(dial);
for (var i = 0; i < 60; i++) {
var mky = shdw(i * 6, 0.5).vsa;
var mkx = shdw(i * 6, 0.5).hsa;
var len = (i % 5) ? 4 : 8;
var wid = (i % 5) ? .8 : 4;
mcon[i] = d.createElement('div');
mcon[i].setAttribute('style', 'display: block;'
+'position: absolute;'
+'width: '+xy(4)+'px;'
+'height: '+xy(8)+'px;'
+'margin: auto; top: 0;bottom: 0;left: 0;right: 0;'
+'font-size: 0px;line-height: 0px;padding: 0;'
+'color: '+clockColour+';');
mrkrs[i] = d.createElement('div');
mrkrs[i].setAttribute('style', 'display: block;'
+'position: absolute;'
+'width: '+xy(wid)+'px;'
+'height: '+xy(len)+'px;'
+'margin: auto; top: 0;left: 0;right: 0;'
+'font-size: 0px;line-height: 0px;padding: 0;'
+'box-shadow:'+mkx+'px '+mky+'px 0px 0px rgba(0,0,0,0.7);'
+'background-color:'+clockColour+';');
mcon[i].appendChild(mrkrs[i]);
dial.appendChild(mcon[i]);
degr += 6;
mcon[i].style.top = xy(0) + xy(86) * Math.sin(-offs + e * i * radi) + 'px';
mcon[i].style.left= xy(0) + xy(86) * Math.cos(-offs + e * i * radi) + 'px';
mcon[i].style.transform = 'rotate(' + (degr) + 'deg)';
mcon[i].style.transformOrigin = 'center center';
}
/* Generic container div for all hands */
var handContainers = 'display: block;'
+'position: absolute;'
+'height: '+xy(100)+'px;'
+'width: '+xy(16)+'px;'
+'font-size: 0px; line-height: 0px; padding: 0;'
+'margin: auto; top: 0;bottom: 0; left: 0; right: 0;'
+'transform-origin: center center;';
/* Hour hand CSS */
var houClone = handContainers;
var houHand = d.createElement('div');
houHand.setAttribute('style', houClone);
dial.appendChild(houHand);
var hh = d.createElement('div');
hh.setAttribute('style', 'display: block;'
+'position: absolute;'
+'height: '+xy(48)+'px;'
+'width: '+xy(5)+'px;'
+'top: '+xy(15)+'px;'
+'margin: auto; left: 0; right: 0;'
+'outline: 1px solid transparent;'
+'background-color: '+clockColour+';');
houHand.appendChild(hh);
var houShad = houHand.cloneNode(true);
houShad.childNodes[0].style.backgroundColor = shdcol;
/* Minute hand CSS */
var minClone = handContainers;
var minHand = d.createElement('div');
minHand.setAttribute('style', minClone);
dial.appendChild(minHand);
var mh = d.createElement('div');
mh.setAttribute('style', 'display:block;'
+'position: absolute;'
+'height: '+xy(58)+'px;'
+'width: '+xy(3)+'px;'
+'top: '+xy(5)+'px;'
+'margin: auto; left: 0; right: 0;'
+'outline: 1px solid transparent;'
+'background-color: '+clockColour+';');
minHand.appendChild(mh);
var minShad = minHand.cloneNode(true);
minShad.childNodes[0].style.backgroundColor = shdcol;
/* Seconds hand CSS */
var secClone = handContainers;
var secHand = d.createElement('div');
secHand.setAttribute('style', secClone);
dial.appendChild(secHand);
var sh = d.createElement('div');
var svgsec = '<svg height="'+xy(100)+'" width="'+xy(16)+'">'
+'<g>'
+'<rect id="hd" x="'+xy(7.45)+'" y="'+xy(3)+'" width="'+xy(1)+'" height="'+xy(60)+'" stroke-width="0" fill="'+secHandColour+'"/>'
+'<circle id="cw" cx="50%" cy="'+xy(67)+'" r="'+xy(4)+'" stroke-width="0" stroke="'+secHandColour+'" fill="'+secHandColour+'"/>'
+'</g>'
+'</svg>';
sh.innerHTML = svgsec;
secHand.appendChild(sh);
var secShad = secHand.cloneNode(true);
var newColor = '"'+shdcol+'"';
var clnshd = svgsec.split(secHandColour).join("");
clnshd = clnshd.replace(/""/g, newColor);
secShad.innerHTML = clnshd;
var nut = d.createElement('div');
nut.setAttribute('style', 'display: block;'
+'position: absolute;'
+'height: '+xy(4)+'px;'
+'width: '+xy(4)+'px;'
+'border-radius: 50%;'
+'margin: auto;top: 0;bottom: 0;left: 0;right: 0;'
+'background-color: '+secHandColour+';'
+'box-shadow:'+xy(0)+'px '+xy(0.5)+'px 0px 0px rgba(0,0,0,0.7);'
+'z-index: 105;');
dial.appendChild(nut);
function houKeyFrames() {
var houSheet = (d.getElementById('tmphouSheet'+idx));
if (houSheet) {
houSheet.parentNode.removeChild(houSheet);
}
houClone = handContainers;
var p1 = houDeg;
var p2 = houDeg+1;
var p3 = houDeg+0.4;
var p4 = houDeg+1;
var p5 = houDeg+0.5;
var p6 = houDeg+1;
var houframes = '@keyframes hou'+idx+'gen'+houIncr+' { '
+'0% { transform: rotate('+p1+'deg) '+eiatf+';}'
+'30% { transform: rotate('+p2+'deg) '+eoatf+';}'
+'45% { transform: rotate('+p3+'deg) '+eiatf+';}'
+'60% { transform: rotate('+p4+'deg) '+eoatf+';}'
+'70% { transform: rotate('+p5+'deg) '+eiatf+';}'
+'80%,100% { transform: rotate('+p6+'deg) '+eoatf+';}}';
var hs = document.createElement('style');
hs.setAttribute('id', 'tmphouSheet'+idx);
hs.innerHTML = houframes;
d.getElementsByTagName('head')[0].appendChild(hs);
var houAni = 'animation: hou'+idx+'gen'+houIncr+' '+houSpan+' 1 forwards;';
houClone += houAni;
houHand.setAttribute('style', houClone);
houHand.style.zIndex = 100;
dial.appendChild(houHand);
houShad.setAttribute('style', houClone);
houShad.style.top = xy(2.5)+'px';
houShad.style.zIndex = 99;
dial.appendChild(houShad);
}
function minKeyFrames() {
var minSheet = (d.getElementById('tmpMinSheet'+idx));
if (minSheet) {
minSheet.parentNode.removeChild(minSheet);
}
minClone = handContainers;
var p1 = minDeg;
var p2 = minDeg+6;
var p3 = minDeg+4;
var p4 = minDeg+6;
var p5 = minDeg+5;
var p6 = minDeg+6;
var minframes = '@keyframes min'+idx+'gen'+minIncr+' { '
+'0% { transform: rotate('+p1+'deg) '+eiatf+';}'
+'30% { transform: rotate('+p2+'deg) '+eoatf+';}'
+'45% { transform: rotate('+p3+'deg) '+eiatf+';}'
+'60% { transform: rotate('+p4+'deg) '+eoatf+';}'
+'70% { transform: rotate('+p5+'deg) '+eiatf+';}'
+'80%,100% { transform: rotate('+p6+'deg) '+eoatf+';}}';
var ms = document.createElement('style');
ms.setAttribute('id', 'tmpMinSheet'+idx);
ms.innerHTML = minframes;
d.getElementsByTagName('head')[0].appendChild(ms);
var minAni = 'animation: min'+idx+'gen'+minIncr+' '+minSpan+' 1 forwards;';
minClone += minAni;
minHand.setAttribute('style', minClone);
minHand.style.zIndex = 102;
dial.appendChild(minHand);
minShad.setAttribute('style', minClone);
minShad.style.top = xy(3)+'px';
minShad.style.zIndex = 101;
dial.appendChild(minShad);
}
function secKeyFrames() {
var secSheet = (d.getElementById('tmpSecSheet'+idx));
if (secSheet) {
secSheet.parentNode.removeChild(secSheet);
}
secClone = handContainers;
var p1 = secDeg;
var p2 = secDeg+6;
var p3 = secDeg+4;
var p4 = secDeg+6;
var p5 = secDeg+5;
var p6 = secDeg+6;
var secframes = '@keyframes sec'+idx+'gen'+secIncr+' { '
+'0% { transform: rotate('+p1+'deg) '+eiatf+';}'
+'30% { transform: rotate('+p2+'deg) '+eoatf+';}'
+'45% { transform: rotate('+p3+'deg) '+eiatf+';}'
+'60% { transform: rotate('+p4+'deg) '+eoatf+';}'
+'70% { transform: rotate('+p5+'deg) '+eiatf+';}'
+'80%,100% { transform: rotate('+p6+'deg) '+eoatf+';}}';
var ss = document.createElement('style');
ss.setAttribute('id', 'tmpSecSheet'+idx);
ss.innerHTML = secframes;
document.getElementsByTagName('head')[0].appendChild(ss);
var secAni = 'animation: sec'+idx+'gen'+secIncr+' '+secSpan+' 1 forwards;';
secClone += secAni;
secHand.setAttribute('style', secClone);
secHand.style.zIndex = 104;
dial.appendChild(secHand);
secShad.setAttribute('style', secClone);
secShad.style.top = xy(3.5)+'px';
secShad.style.zIndex = 103;
dial.appendChild(secShad);
}
function clock() {
var x = new Date();
var seconds = x.getSeconds();
var minutes = x.getMinutes();
var hours = (x.getHours() * 30) + (x.getMinutes()/2);
if (seconds !== preSec) {
secIncr++;
secDeg = (seconds-1) * 6;
secHand.removeAttribute('style');
secKeyFrames();
if (secIncr > 59) {
secIncr = 0;
}
}
if (minutes !== preMin) {
minIncr++;
minDeg = (minutes-1) * 6;
minHand.removeAttribute('style');
minKeyFrames();
if (minIncr > 59) {
minIncr = 0;
}
}
if (hours !== preHou) {
houIncr++;
houDeg = (hours-1) * 1;
houHand.removeAttribute('style');
houKeyFrames();
if (houIncr > 59) {
houIncr = 0;
}
}
preSec = seconds;
preMin = minutes;
preHou = hours;
}
function cyc() {
var pres = performance.now();
if ((pres - prev) > mls) {
clock();
prev = performance.now();
}
window.requestAnimationFrame(cyc);
}
window.addEventListener('load', cyc, false);
})();
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Clock</title>
<style type="text/css">
html {
height: 100%;
}
body {
background-color: #ffff00;
text-align: center;
}
</style>
</head>
<body>
</body>
</html>
Для меня это просто не имеет смысла - все это пишу JavaScript для создания CSS3 анимации. Просто используйте ['jQuery.animate()'] (http://api.jquery.com/animate/) и покончите с этим. – dave
@dave и что не так с тем, как научиться правильно писать код. Говорите, как говорит OP. –
@Griggy Я не уверен, если вы имеете в виду, что он работает не так, как ожидалось, или что он делает, но вы боитесь, что не делаете это правильно. –