add some js

This commit is contained in:
Simon Bruder 2017-11-05 11:19:46 +00:00
parent 9f9f076ad4
commit 46546bd86e
2 changed files with 64 additions and 0 deletions

View file

@ -5,5 +5,7 @@
<p>Icons: <a href="https://material.io/icons">material.io</a>, CSS-Framework: <a href="http://getskeleton.com">Skeleton</a>, Generator: <a href="https://gohugo.io/">Hugo</a></p>
</footer>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js"></script>
<script src="{{ "js/site.js" | relURL }}"></script>
</body>
</html>

View file

@ -0,0 +1,62 @@
$(document).ready(function() {
// Variables
var $nav = $('.navbar'),
$body = $('body'),
$window = $(window),
navOffsetTop = $nav.offset().top,
$document = $(document),
entityMap = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': '&quot;',
"'": '&#39;',
"/": '&#x2F;'
}
function init() {
$window.on('scroll', onScroll)
$window.on('resize', resize)
$('a[href^="#"]').on('click', smoothScroll)
buildSnippets();
}
function smoothScroll(e) {
e.preventDefault();
$(document).off("scroll");
var target = this.hash,
menu = target;
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top-40
}, 0, 'swing', function () {
window.location.hash = target;
$(document).on("scroll", onScroll);
});
}
function resize() {
$body.removeClass('has-docked-nav')
navOffsetTop = $nav.offset().top
onScroll()
}
function onScroll() {
if(navOffsetTop < $window.scrollTop() && !$body.hasClass('has-docked-nav')) {
$body.addClass('has-docked-nav')
}
if(navOffsetTop > $window.scrollTop() && $body.hasClass('has-docked-nav')) {
$body.removeClass('has-docked-nav')
}
}
function escapeHtml(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
}
init();
});