-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flucss.js
105 lines (58 loc) · 2.38 KB
/
flucss.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
/* ==========================================================================
Flucss script by veli.ee
CSS styles based on sunset and sunrise for visitor location. Like F.lux, for the web.
http://veli.ee/flucss
========================================================================== */
(function ( $ ) {
$.fn.flucss = function( options ) {
// default options
var options = $.extend({
black: false
}, options );
// check localstorage support, get state from localstorage to avoid flickering when navigating or reloading
if (typeof(Storage) !== "undefined") {
var storeddaytimename = localStorage.getItem("daytimename")
if(storeddaytimename){
$("html").addClass(storeddaytimename);
}
} else { }
// get users location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else { } // no geolocation support
// the main function to get active (closest) sun state
// you MUST INCLUDE https://github.com/mourner/suncalc for calculating the sun states
function showPosition(position) {
// get all sun states, displayed in an object
var times = SunCalc.getTimes(new Date(), position.coords.latitude, position.coords.longitude);
var now = new Date();
var currenttime = now.getTime();
var daytimename = ""; // will get the active name for the sun state, all possible values:
// sunrise, sunriseEnd, goldenHourEnd, solarNoon, goldenHour, sunsetStart, sunset, dusk, nauticalDusk, night, nadir, nightEnd, nauticalDawn, dawn
// variables to store current time and sun state differences
var curr = times.sunrise.getTime();
var diff = Math.abs (currenttime - curr);
for (var property in times) {
if (times.hasOwnProperty(property)) {
var newdiff = Math.abs (currenttime - times[property].getTime());
if (newdiff < diff) {
diff = newdiff;
curr = times[property].getTime();
daytimename = property;
localStorage.setItem("daytimename", property); // set localstorage, to avoid flickering when geolocation is reached
}
}
}
// adds the appropriate class to root HTML element
// add the class ".fullblack" to your "HTML" element, if you want night to be completely black
}
this.each(function() {
var element = $( this );
element.addClass(localStorage.getItem("daytimename"));
if(options.black){
element.addClass("fullblack");
}
});
return this;
};
}( jQuery ));