Want to navigate to particular div / portion of html in the same page?
There are three ways to do this :-
There are three ways to do this :-
- Using anchor tag
- Using pure Javascript.
- Using JQuery Scrolltop function.
This is the native method, giving id to the element that needs to be focussed and in href of anchor tag calling the id with hash.
eg: <a href="#abc">scrolltop</a>
This will jump to that element directly.
Using pure Javascript.
FUNCTION:
function scrolltoDiv(_id)
{
document.getElementById(_id).scrollIntoView(true);
} CALLING THE ABOVE FUNCTION :-
You can call the above functions in following ways:
- <a href="javascript:scrolltoDiv('abc')">scroll top </a>
- <p onclick="scrolltoDiv('abc')">scrolltop</p>
Using JQuery Scrolltop function.
Below is the function that will navigate to whichever element you want.
FUNCTION :
function scrolltoDiv(_id)
{
$('html, body').animate({
scrollTop: $("#" + _id).offset().top
},1000,'easeInOutCirc');
}
CALLING THE ABOVE FUNCTION :-
You can call the above functions in following ways:
* where abc is the id of the element.FUNCTION :
function scrolltoDiv(_id)
{
$('html, body').animate({
scrollTop: $("#" + _id).offset().top
},1000,'easeInOutCirc');
}
CALLING THE ABOVE FUNCTION :-
You can call the above functions in following ways:
- <a href="javascript:scrolltoDiv('abc')">scroll top </a>
- <p onclick="scrolltoDiv('abc')">scrolltop</p>
No comments:
Post a Comment