From 4314fd421420d04d6f2c32219aec1151d705cb1d Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 22 Apr 2020 22:04:52 +0200 Subject: [PATCH] [FIX] Seite im IE falsch dargestellt --- script.js | 81 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 16 deletions(-) diff --git a/script.js b/script.js index 31f8f0b..c6aad63 100644 --- a/script.js +++ b/script.js @@ -10,31 +10,80 @@ document.addEventListener('DOMContentLoaded', function () { }); var mailcypts = document.querySelectorAll(".cryptmail"); - mailcypts.forEach(element => { - element.onclick = function () { + for (var i = 0; i < mailcypts.length; i++) { + mailcypts[i].onclick = function () { window.location.href = 'mailto:' + this.dataset.name + '@' + this.dataset.domain + '.' + this.dataset.tld; - return false; } - }); + } var hashAnchors = document.querySelectorAll("a[href^='#']"); - hashAnchors.forEach(element => { - element.onclick = function () { + for (var i = 0; i < hashAnchors.length; i++) { + hashAnchors[i].onclick = function () { var target = this.getAttribute("href"); scrollToElement(target); - return false; } - }); + } }); -function scrollToElement(name) { - var elementToScrollTo = document.querySelector(name); - var top = elementToScrollTo.getBoundingClientRect().top + window.pageYOffset - 48; +const navbarScrollOffset = -48; +// native smooth scrolling for Chrome, Firefox & Opera +// @see: https://caniuse.com/#feat=css-scroll-behavior +const nativeSmoothScrollTo = function (elem) { + window.scroll({ + behavior: 'smooth', + left: 0, + top: elem.getBoundingClientRect().top + window.pageYOffset + navbarScrollOffset + }); +}; - window.scrollTo({ - top: top, - behavior: "smooth" - }) -} \ No newline at end of file +// polyfilled smooth scrolling for IE, Edge & Safari +const smoothScrollTo = function (to, duration) { + const element = document.scrollingElement || document.documentElement, + start = element.scrollTop, + change = to - start, + startDate = +new Date(); + + // t = current time + // b = start value + // c = change in value + // d = duration + const easeInOutQuad = function (t, b, c, d) { + t /= d / 2; + if (t < 1) return c / 2 * t * t + b; + t--; + return -c / 2 * (t * (t - 2) - 1) + b; + }; + + const animateScroll = function () { + const currentDate = +new Date(); + const currentTime = currentDate - startDate; + element.scrollTop = parseInt(easeInOutQuad(currentTime, start, change, duration)); + if (currentTime < duration) { + requestAnimationFrame(animateScroll); + } else { + element.scrollTop = to; + } + }; + animateScroll(); +}; + +// detect support for the behavior property in ScrollOptions +const supportsNativeSmoothScroll = 'scrollBehavior' in document.documentElement.style; + +// smooth scrolling stub +const scrollToElement = function (elemSelector) { + if (!elemSelector) { + return; + } + + const elem = document.querySelector(elemSelector); + if (elem) { + if (supportsNativeSmoothScroll) { + nativeSmoothScrollTo(elem); + } else { + smoothScrollTo(elem.offsetTop + navbarScrollOffset, 600); + } + } +}; \ No newline at end of file