blog/assets/js/main.js

88 lines
2.5 KiB
JavaScript
Raw Normal View History

// Fix DOM matches function
if (!Element.prototype.matches) {
Element.prototype.matches =
Element.prototype.matchesSelector ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector ||
Element.prototype.oMatchesSelector ||
Element.prototype.webkitMatchesSelector ||
function(s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i = matches.length;
while (--i >= 0 && matches.item(i) !== this) {}
return i > -1;
};
}
// Get Scroll position
function getScrollPos() {
var supportPageOffset = window.pageXOffset !== undefined;
var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
var x = supportPageOffset ? window.pageXOffset : isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft;
var y = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;
return { x: x, y: y };
}
var _scrollTimer = [];
// Smooth scroll
function smoothScrollTo(y, time) {
time = time == undefined ? 500 : time;
var scrollPos = getScrollPos();
var count = 60;
var length = (y - scrollPos.y);
function easeInOut(k) {
return .5 * (Math.sin((k - .5) * Math.PI) + 1);
}
for (var i = _scrollTimer.length - 1; i >= 0; i--) {
clearTimeout(_scrollTimer[i]);
}
for (var i = 0; i <= count; i++) {
(function() {
var cur = i;
_scrollTimer[cur] = setTimeout(function() {
window.scrollTo(
scrollPos.x,
scrollPos.y + length * easeInOut(cur/count)
);
}, (time / count) * cur);
})();
}
}
2020-07-10 16:27:56 +02:00
// Init highlight js
document.addEventListener('DOMContentLoaded', function(event) {
2020-10-03 06:23:29 +02:00
var els = document.querySelectorAll('pre code')
function handle(block) {
2020-08-18 08:02:07 +02:00
var outer = block.parentElement.parentElement.parentElement;
2020-07-10 16:27:56 +02:00
var lang = block.getAttribute('data-lang');
2020-10-03 06:23:29 +02:00
for (var i = 0; i < outer.classList.length; i++) {
var cls = outer.classList[i];
2020-08-18 08:02:07 +02:00
if (cls.startsWith('language-')) {
lang = cls;
break;
}
}
2020-08-24 05:13:39 +02:00
if (!lang) {
cls = block.getAttribute('class');
lang = cls ? cls.replace('hljs ', '') : '';
2020-07-10 16:27:56 +02:00
}
2020-08-18 08:02:07 +02:00
if (lang.startsWith('language-')) {
lang = lang.substr(9);
}
2020-08-24 05:13:39 +02:00
block.setAttribute('class', 'hljs ' + lang);
2020-07-10 16:27:56 +02:00
block.parentNode.setAttribute('data-lang', lang);
2020-08-18 08:02:07 +02:00
hljs.highlightBlock(block);
2020-10-03 06:23:29 +02:00
}
for (var i = 0; i < els.length; i++) {
var el = els[i];
handle(el);
}
2020-07-10 16:27:56 +02:00
});