Hi I included jquerys smooth scrolling to my website and it doesn't work, i turned my site around that you can scroll to the right. I already tried smooth-behaviour and this does work.
Javascript:
$(document).ready(function () {
// Add smooth scrolling to all links
$("a").on("click", function (event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$("html, body").animate(
{
scrollTop: $(hash).offset().top,
},
800,
function () {
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
}
);
} // End if
});
});
Html:
<img src="icon/logo.svg" alt="logo" class="logo">
<div class="navbar">
<a href="#home" class="nav-a">Home</a>
<a href="#about" class="nav-a">About</a>
<a href="#arbeiten" class="nav-a">Projekte</a>
<a href="#dokumentation" class="nav-a">Dokumentation</a>
</div>
please try this instead,
use this scroll-behaviour
html{
scroll-behavior: smooth;
}
And make each section with id like this,
<div id="home">
<p>home</p>
<p>home</p>
<p>home</p>
<p>home</p>
</div>
html{
scroll-behavior: smooth;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://i.sstatic.net/pDKtO.png" alt="logo" class="logo">
<div class="navbar">
<a href="#home" class="nav-a">Home</a>
<a href="#about" class="nav-a">About</a>
<a href="#arb" class="nav-a">Projekte</a>
<a href="#doc" class="nav-a">Dokumentation</a>
<div id="home">
<p>home</p>
<p>home</p>
<p>home</p>
<p>home</p>
</div>
<div id="about">
<p>about</p>
<p>about</p>
<p>about</p>
<p>about</p>
</div>
<div id="arb">
<p>arbeiten</p>
<p>arbeiten</p>
<p>arbeiten</p>
<p>arbeiten</p>
</div>
<div id="doc">
<p>dokumentation</p>
<p>dokumentation</p>
<p>dokumentation</p>
<p>dokumentation</p>
</div>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
</div>
Using .animate() method in body,html
element.
I added a attribute for links target-toggle
<a href="#" target-toggle="#home" class="nav-a">Home</a>
if($('[target-toggle]').length) {
$('[target-toggle]').on("click",function () {
var $target_value = $(this).attr("target-toggle");
if($($target_value).length) {
$('html, body').stop().animate({
scrollTop: ($($target_value).offset().top)
}, 1000);
}
event.preventDefault();
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://i.sstatic.net/pDKtO.png" alt="logo" class="logo">
<div class="navbar">
<a href="#" target-toggle="#home" class="nav-a">Home</a>
<a href="#" target-toggle="#about" class="nav-a">About</a>
<a href="#" target-toggle="#arb" class="nav-a">Projekte</a>
<a href="#" target-toggle="#doc" class="nav-a">Dokumentation</a>
<div id="home">
<p>home</p>
<p>home</p>
<p>home</p>
<p>home</p>
</div>
<div id="about">
<p>about</p>
<p>about</p>
<p>about</p>
<p>about</p>
</div>
<div id="arb">
<p>arbeiten</p>
<p>arbeiten</p>
<p>arbeiten</p>
<p>arbeiten</p>
</div>
<div id="doc">
<p>dokumentation</p>
<p>dokumentation</p>
<p>dokumentation</p>
<p>dokumentation</p>
</div>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
<p>navbar</p>
</div>