Haai. I have a trouble in my little project. I've create <a href="slogannya">some icon</a>
and <blockquote id="slogannya">
but when I click the <a>
tag it doesn't work. Here is my code :
Html
<section class="to-content">
<a class="scrollTo" href="#slogannya">
<button type="button">
<i class="material-icons">keyboard_arrow_down</i>
</button>
</a>
</section>
<section class="slogan">
<blockquote id="slogannya">
<p>Wanna create an event? <br> or you want to give some advice to us? Use the form below </p>
</blockquote>
</section>
Jquery
$(document).ready(function () {
$(".scrollTo").on('click', function (e) {
e.preventDefault();
var target = $(this).attr('href');
$('html, body').animate({
scrollTop: ($(target).offset().top)
}, 2000);
});
});
How can I made this work? Please help me :)
EDIT
I know what my problems is. In my project, I have a css called 'general.css' (this css content css reset and other styling for any page). In this css, it makes any element to be box-sizing: border-box;
and others style. I don't know specifically what the problems. But when I remove my 'general.css', the smooth scroll is work. Thank you for all your answers friends :) , I'm appreciate it!!
All you need is HTML some
<a>
nchors and any element you want (even more<a>
nchors go crazy). BTW the reason targeting id wasn't working for you is because thehref
requires a prefix of#
to the url. ex.<a href="#ID"...
Another reason that it may not work is that the<a>
nchor and the destination element are too close to each other, so ideally a destination should be at least far enough that scrolling is needed to get there.For a destination element, ensure that it has an
#id
. ex<div id='ID'...
Next, for each destination, create one or more
<a>
nchors and set theirhref
values as#
+ID. ex.<a href='#ID...
If the destination element happens to be an
<a>
nchor, we can use thename
attribute as well. ex.<a name='NAME'...
Demo
Actually it works but the problem is that the blockquote tag are immediate after the anchor tag but if you make a space between them you'll see the animation in action
in brief : the animation will work only when the scroll bar appear on the window and there is a space between the two elements look at the example below and you'll see it works
It was working but the code supplied had no height to scroll, also wasn't a smooth scroll. If that isn't the case try this.