From 94a9f488723b0b70981f1bbc5c7f78f571fdaebb Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 10 Apr 2020 18:45:52 +0200 Subject: [PATCH] =?UTF-8?q?[TASK]=20Navigationsleiste=20hinzugef=C3=BCgt?= =?UTF-8?q?=20-=20scrollen=20per=20Javascript=20(wenn=20m=C3=B6glich)=20-?= =?UTF-8?q?=20wenn=20nicht=20dann=20per=20Hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 26 ++++++++++++++++++-------- script.js | 22 +++++++++++++++++++++- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index b1b166c..4cec92f 100644 --- a/index.html +++ b/index.html @@ -15,25 +15,35 @@ - +
-

Ferienwohnung "Wanderlust"

Entspannen in gemütlicher Atmosphäre

-
-
Eindrücke unserer Ferienwohnung
-
-
+
+

Willkommen

@@ -57,8 +67,8 @@ Wir freuen uns auf Ihren Besuch.

-
-
+ +
diff --git a/script.js b/script.js index 8a4569b..c7b0e35 100644 --- a/script.js +++ b/script.js @@ -10,4 +10,24 @@ document.addEventListener('DOMContentLoaded', function () { return false; } }); -}); \ No newline at end of file + + var hashAnchors = document.querySelectorAll("a[href^='#']"); + hashAnchors.forEach(element => { + element.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; + + window.scrollTo({ + top: top, + behavior: "smooth" + }) +} \ No newline at end of file