-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdark-knight.js
71 lines (54 loc) · 1.77 KB
/
dark-knight.js
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
/**
* DARK-KNIGHT PLUGIN
* This is a vanilla javascript plugin
* that sets the mode of your website.
*
* License: MIT
*/
function initialiseMode(config={}){
const DAY ='Day'
const NIGHT ='Night'
const LIGHT = 'white'
const DARK = 'black'
const body = document.querySelector('body')
const btn = document.createElement('button')
btn.innerText = NIGHT
detectTimeMode('load',body, btn, 12, DARK,LIGHT, DAY)
activateMode(btn, NIGHT, DAY, 'click', body, DARK, LIGHT)
body.appendChild(btn)
// Styling BTN
btn.style.position = config.position || 'fixed'
btn.style.top = config.positionTop || '90vh'
btn.style.right = config.positionRight || '2vh'
btn.style.border = config.borderThickness || 'none'
btn.style.borderRadius =config.borderRadius || '25px'
btn.style.width =config.width || '50px'
btn.style.height =config.height || '50px'
btn.style.background = config.background || 'pink'
btn.style.appearance ='none'
btn.style.color = config.color || 'black'
}
function activateMode(btn,nmode, dmode, evt, body,ncolor, dcolor){
btn.addEventListener(evt, function(){
if(this.innerText == nmode){
this.innerText = dmode
body.style.background = ncolor
body.style.color = dcolor
}else{
this.innerText = nmode
body.style.background = dcolor
body.style.color = ncolor
}
})
}
function detectTimeMode(evt,body, btn, time, ncolor, dcolor, dmode){
const timeOfDay = new Date().getHours()
addEventListener(evt, function(){
if(timeOfDay > time){
body.style.background = ncolor
body.style.color = dcolor
btn.innerText = dmode
}
})
}
// initialiseMode()