Let me do it first. Effect: https://linrunzheng.github.io/3Dclock/3Dclock/new.html
As for the 3D cube What to do here is not elaborated, you can look at my previous blog.
Here by default you have done 6 cubes, directly on the JS code:
When the page comes in, first give 6 cubes to the current time, because the cube is relatively small, left and right 2 faces can not see clearly and affect the effect, where the left and right 2 faces will not be assigned to date;
Get element
var Oul = Document.queryselectorall ("ul");
var back = Document.queryselectorall (". Back");
var front = Document.queryselectorall (". Front");
var Top = Document.queryselectorall (". Top");
var bot = Document.queryselectorall (". Bot");
var btn = document.queryselector ("button");
Gets the current time, put into an array of arrays for easy cyclic assignment var arr = [];
Arr[0] = Math.floor (new Date (). GetHours ()/10);
ARR[1] = new Date (). GetHours ()% 10;
ARR[2] = Math.floor (new Date (). getminutes ()/10);
ARR[3] = new Date (). getminutes ()% 10;
ARR[4] = Math.floor (new Date (). getseconds ()/10);
ARR[5] = new Date (). getseconds ()% 10;
When the page comes in, loop to the 6 cubes to assign a value time, (due to the existence of the timer, otherwise there will be 0.5 seconds interval is no time)
Corresponding before and after each other
for (var i = 0; i < arr.length; i++) {
front[i].innerhtml = Arr[i];
back[i].innerhtml = Arr[i] + 2 >= 10? (Arr[i] + 2)% 10:arr[i] + 2;
T op[i].innerhtml = arr[i] + 3 >= 10? (Arr[i] + 3)% 10:arr[i] + 3;
bot[i].innerhtml = Arr[i] + 1 >= 10? (Arr[i] + 1)% 10:arr[i] + 1;
}
We only focus on the front side of the front, and the other faces are just modifiers.
Key: Function of timer execution
Timer function Start:
function Time () {
Re-fetch the current time
var arr2 = [];
Arr2[0] = Math.floor (new Date (). GetHours ()/10);
ARR2[1] = new Date (). GetHours ()% 10;
ARR2[2] = Math.floor (new Date (). getminutes ()/10);
ARR2[3] = new Date (). getminutes ()% 10;
ARR2[4] = Math.floor (new Date (). getseconds ()/10);
ARR2[5] = new Date (). getseconds ()% 10;
Circular Judgment Cube Front number when there are changes, if there are changes to add class, to achieve the flip effect, CSS code written in the transition class,
CSS Key classes
. Transition {
Transition:all 0.5s;
Transform:rotatey ( -7deg) Rotatex (95deg);
}
The key: Judging whether the number changes
for (var i = 0; i < oul.length; i++) {
if (front[i].innerhtml! = Arr2[i]) {
Oul[i].classlist.add ("transition");
}
Add a transition end event to each cube Webkittransitionend (other browsers have different ways of writing, only Chrome is written here)
Oul[i].index = i;
Oul[i].addeventlistener ("Webkittransitionend", function () {
Assigning numbers to several faces
front[this.index].innerhtml = Arr2[this.index];
back[this.index].innerhtml = Arr2[this.index] + 2 < 10? Arr2[this.index] + 2: (Arr2[this.index] + 2)% 10;
top[this.index].innerhtml = Arr2[this.index] + 3 < 10? Arr2[this.index] + 3: (Arr2[this.index] + 3)% 10;
bot[this.index].innerhtml = Arr2[this.index] + 1 < 10? Arr2[this.index] + 1: (Arr2[this.index] + 1)% 10;
Remove transition events so that the cube returns to the effect before flipping
This.classList.remove ("transition");
})
}
}
Turn on timer
SetInterval (time, 500)
Now that you have finished, open the Web page to see the effect.
Source code See: Https://github.com/linrunzheng/3Dclock
Imitation Intelligence Club official website: Native JS for simple and cool 3D cubic clock