1 |
/* |
2 |
CrudeDoc Java Script |
3 |
Copyright (c) 2015 - 2017 Christian Schoenebeck. All rights reserved. |
4 |
http://www.crudebyte.com |
5 |
*/ |
6 |
|
7 |
function crdWindowHeight() { |
8 |
return window.innerHeight || |
9 |
document.documentElement.clientHeight || |
10 |
document.getElementsByTagName('body')[0].clientHeight; |
11 |
} |
12 |
|
13 |
function crdScrollTo(e) { |
14 |
$('html, body').animate({ |
15 |
scrollTop: $( "#" + $(e).attr("section") ).offset().top - 68 |
16 |
}, 300); |
17 |
var obj = $( "#" + $(e).attr("section") ); |
18 |
$(obj).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(120).fadeOut(100).fadeIn(120); |
19 |
} |
20 |
|
21 |
function crdScrollToHash(h) { |
22 |
if (typeof h === 'undefined') { |
23 |
h = location.hash; |
24 |
if (!h || h.length < 2) return; |
25 |
} |
26 |
$('html, body').animate({ |
27 |
scrollTop: $( h ).offset().top - 68 |
28 |
}, 300); |
29 |
$(h).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(120).fadeOut(100).fadeIn(120); |
30 |
} |
31 |
|
32 |
function crdOnScroll() { |
33 |
//var headers = $("body > article :header:in-viewport"); |
34 |
var headers = $("body > article :header"); |
35 |
var wst = $(window).scrollTop(); |
36 |
if (wst <= 0) { |
37 |
for (var i = 0; i < headers.length; ++i) { |
38 |
$("body > aside *[section]").removeClass("current"); |
39 |
var link = $("body > aside *[section='" + headers[i].id + "']"); |
40 |
$(link).addClass("current"); |
41 |
return; |
42 |
} |
43 |
} |
44 |
var wh = crdWindowHeight(); |
45 |
for (var i = headers.length - 1; i >= 0; --i) { |
46 |
var offset = headers[i].offsetTop - wst + wh/2; |
47 |
if (headers[i].id.length > 0 && (offset <= 0 || offset <= wh)) { |
48 |
$("body > aside *[section]").removeClass("current"); |
49 |
var link = $("body > aside *[section='" + headers[i].id + "']"); |
50 |
$(link).addClass("current"); |
51 |
break; |
52 |
} |
53 |
} |
54 |
} |
55 |
|
56 |
function crdOnWindowResize() { |
57 |
var wh = crdWindowHeight(); |
58 |
$("nav > ul > li ul").css("max-height", wh - 70); |
59 |
} |
60 |
|
61 |
function crdDetectFeatures() { |
62 |
if ('ontouchstart' in document.documentElement) { |
63 |
document.documentElement.className += ' touch'; |
64 |
} else { |
65 |
document.documentElement.className += ' no-touch'; |
66 |
} |
67 |
if ($.support["transform"]) { |
68 |
$("body").addClass("hasTransform"); |
69 |
} |
70 |
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; |
71 |
if (iOS) { |
72 |
$("body").addClass("iOS"); |
73 |
} |
74 |
} |
75 |
|
76 |
$(function() { |
77 |
crdDetectFeatures(); |
78 |
$(document).tooltip(); |
79 |
$("article img[caption]").each(function() { |
80 |
$(this).after( |
81 |
"<div class='imgcptn'>" + |
82 |
$(this).attr("caption") + "</div>" |
83 |
); |
84 |
}); |
85 |
$("body > aside *[section]").click(function(e) { |
86 |
//e.preventDefault(); |
87 |
crdScrollTo(this); |
88 |
return false; |
89 |
}); |
90 |
crdOnScroll(); |
91 |
$(window).resize(function() { |
92 |
crdOnWindowResize(); |
93 |
}); |
94 |
if ("onhashchange" in window) { |
95 |
window.onhashchange = function() { |
96 |
crdScrollToHash(); |
97 |
}; |
98 |
} |
99 |
$("article a[href]").each(function() { |
100 |
var href = $(this).attr("href"); |
101 |
if (href && href.length > 1 && href.charAt(0) == '#') { |
102 |
$(this).click(function(e) { |
103 |
var anch = $(this).attr("href"); |
104 |
crdScrollToHash(anch); |
105 |
}); |
106 |
} |
107 |
}); |
108 |
}); |
109 |
|
110 |
$(document).ready(function() { |
111 |
crdOnWindowResize(); |
112 |
crdScrollToHash(); |
113 |
}); |
114 |
|
115 |
var _crdScrollTimeout; |
116 |
|
117 |
$(window).scroll(function() { |
118 |
crdOnScroll(); |
119 |
}); |