scrollTop jquery, scrolling to div with id?
up vote
31
down vote
favorite
So this is the current code I have
$(document).ready(function()
$('.abouta').click(function()
$('html, body').animate(scrollTop:308, 'slow');
return false;
);
$('.portfolioa').click(function()
$('html, body').animate(scrollTop:708, 'slow');
return false;
);
$('.contacta').click(function()
$('html, body').animate(scrollTop:1108, 'slow');
return false;
);
);
When you click a link e.g. 'abouta' it scrolls to that specific section of the page. I'd rather do a scrollTo and then the id of a div so that I don't have to keep changing the scrollTo position if I change padding etc. Is there any way?
Thanks
javascript jquery css scroll position
add a comment |
up vote
31
down vote
favorite
So this is the current code I have
$(document).ready(function()
$('.abouta').click(function()
$('html, body').animate(scrollTop:308, 'slow');
return false;
);
$('.portfolioa').click(function()
$('html, body').animate(scrollTop:708, 'slow');
return false;
);
$('.contacta').click(function()
$('html, body').animate(scrollTop:1108, 'slow');
return false;
);
);
When you click a link e.g. 'abouta' it scrolls to that specific section of the page. I'd rather do a scrollTo and then the id of a div so that I don't have to keep changing the scrollTo position if I change padding etc. Is there any way?
Thanks
javascript jquery css scroll position
add a comment |
up vote
31
down vote
favorite
up vote
31
down vote
favorite
So this is the current code I have
$(document).ready(function()
$('.abouta').click(function()
$('html, body').animate(scrollTop:308, 'slow');
return false;
);
$('.portfolioa').click(function()
$('html, body').animate(scrollTop:708, 'slow');
return false;
);
$('.contacta').click(function()
$('html, body').animate(scrollTop:1108, 'slow');
return false;
);
);
When you click a link e.g. 'abouta' it scrolls to that specific section of the page. I'd rather do a scrollTo and then the id of a div so that I don't have to keep changing the scrollTo position if I change padding etc. Is there any way?
Thanks
javascript jquery css scroll position
So this is the current code I have
$(document).ready(function()
$('.abouta').click(function()
$('html, body').animate(scrollTop:308, 'slow');
return false;
);
$('.portfolioa').click(function()
$('html, body').animate(scrollTop:708, 'slow');
return false;
);
$('.contacta').click(function()
$('html, body').animate(scrollTop:1108, 'slow');
return false;
);
);
When you click a link e.g. 'abouta' it scrolls to that specific section of the page. I'd rather do a scrollTo and then the id of a div so that I don't have to keep changing the scrollTo position if I change padding etc. Is there any way?
Thanks
javascript jquery css scroll position
javascript jquery css scroll position
edited Aug 29 '11 at 20:52
jondavidjohn
50.1k1898143
50.1k1898143
asked Dec 30 '10 at 18:57
Jake
5243814
5243814
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
up vote
100
down vote
accepted
instead of
$('html, body').animate(scrollTop:xxx, 'slow');
use
$('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
this will return the absolute top position of whatever element you select as #div_id
1
Works perfectly, cheers :D
– Jake
Dec 30 '10 at 21:40
3
I am surprised no one has commented in so long - the question as it stands, really asks for.offset()
rather than.position()
as a solution, right? The target div could be anywhere on the page, and.position()
returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
– sameers
Jul 22 '16 at 1:54
Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
– Dan382
Sep 24 '17 at 19:33
add a comment |
up vote
0
down vote
try this:
$('html, body').animate(scrollTop:$('#xxx').position().top, 'slow');
$('#xxx').focus();
add a comment |
up vote
0
down vote
My solution was the following:
document.getElementById("agent_details").scrollIntoView();
add a comment |
up vote
-2
down vote
if you want to scroll just only some div, can use the div id instead of 'html, body'
$('html, body').animate(...
use
$('#mydivid').animate(...
3
Nope,$('#div').animate(scrollTop:$('#div').position().top, 'slow');
doesn't work. (Chrome 23). Better use$('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
– bicycle
Dec 14 '12 at 17:48
add a comment |
up vote
-2
down vote
try this
$('#div_id').animate(scrollTop:0, '500', 'swing');
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
100
down vote
accepted
instead of
$('html, body').animate(scrollTop:xxx, 'slow');
use
$('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
this will return the absolute top position of whatever element you select as #div_id
1
Works perfectly, cheers :D
– Jake
Dec 30 '10 at 21:40
3
I am surprised no one has commented in so long - the question as it stands, really asks for.offset()
rather than.position()
as a solution, right? The target div could be anywhere on the page, and.position()
returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
– sameers
Jul 22 '16 at 1:54
Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
– Dan382
Sep 24 '17 at 19:33
add a comment |
up vote
100
down vote
accepted
instead of
$('html, body').animate(scrollTop:xxx, 'slow');
use
$('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
this will return the absolute top position of whatever element you select as #div_id
1
Works perfectly, cheers :D
– Jake
Dec 30 '10 at 21:40
3
I am surprised no one has commented in so long - the question as it stands, really asks for.offset()
rather than.position()
as a solution, right? The target div could be anywhere on the page, and.position()
returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
– sameers
Jul 22 '16 at 1:54
Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
– Dan382
Sep 24 '17 at 19:33
add a comment |
up vote
100
down vote
accepted
up vote
100
down vote
accepted
instead of
$('html, body').animate(scrollTop:xxx, 'slow');
use
$('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
this will return the absolute top position of whatever element you select as #div_id
instead of
$('html, body').animate(scrollTop:xxx, 'slow');
use
$('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
this will return the absolute top position of whatever element you select as #div_id
edited Feb 15 '11 at 3:56
answered Dec 30 '10 at 19:05
jondavidjohn
50.1k1898143
50.1k1898143
1
Works perfectly, cheers :D
– Jake
Dec 30 '10 at 21:40
3
I am surprised no one has commented in so long - the question as it stands, really asks for.offset()
rather than.position()
as a solution, right? The target div could be anywhere on the page, and.position()
returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
– sameers
Jul 22 '16 at 1:54
Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
– Dan382
Sep 24 '17 at 19:33
add a comment |
1
Works perfectly, cheers :D
– Jake
Dec 30 '10 at 21:40
3
I am surprised no one has commented in so long - the question as it stands, really asks for.offset()
rather than.position()
as a solution, right? The target div could be anywhere on the page, and.position()
returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
– sameers
Jul 22 '16 at 1:54
Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
– Dan382
Sep 24 '17 at 19:33
1
1
Works perfectly, cheers :D
– Jake
Dec 30 '10 at 21:40
Works perfectly, cheers :D
– Jake
Dec 30 '10 at 21:40
3
3
I am surprised no one has commented in so long - the question as it stands, really asks for
.offset()
rather than .position()
as a solution, right? The target div could be anywhere on the page, and .position()
returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?– sameers
Jul 22 '16 at 1:54
I am surprised no one has commented in so long - the question as it stands, really asks for
.offset()
rather than .position()
as a solution, right? The target div could be anywhere on the page, and .position()
returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?– sameers
Jul 22 '16 at 1:54
Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
– Dan382
Sep 24 '17 at 19:33
Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
– Dan382
Sep 24 '17 at 19:33
add a comment |
up vote
0
down vote
try this:
$('html, body').animate(scrollTop:$('#xxx').position().top, 'slow');
$('#xxx').focus();
add a comment |
up vote
0
down vote
try this:
$('html, body').animate(scrollTop:$('#xxx').position().top, 'slow');
$('#xxx').focus();
add a comment |
up vote
0
down vote
up vote
0
down vote
try this:
$('html, body').animate(scrollTop:$('#xxx').position().top, 'slow');
$('#xxx').focus();
try this:
$('html, body').animate(scrollTop:$('#xxx').position().top, 'slow');
$('#xxx').focus();
answered Mar 14 '14 at 15:33
PauLo Guajardo Torrealba
823
823
add a comment |
add a comment |
up vote
0
down vote
My solution was the following:
document.getElementById("agent_details").scrollIntoView();
add a comment |
up vote
0
down vote
My solution was the following:
document.getElementById("agent_details").scrollIntoView();
add a comment |
up vote
0
down vote
up vote
0
down vote
My solution was the following:
document.getElementById("agent_details").scrollIntoView();
My solution was the following:
document.getElementById("agent_details").scrollIntoView();
edited Nov 11 at 9:10
DaFois
1,74031418
1,74031418
answered Nov 11 at 8:16
Alexis Cabrera Mondeja
61
61
add a comment |
add a comment |
up vote
-2
down vote
if you want to scroll just only some div, can use the div id instead of 'html, body'
$('html, body').animate(...
use
$('#mydivid').animate(...
3
Nope,$('#div').animate(scrollTop:$('#div').position().top, 'slow');
doesn't work. (Chrome 23). Better use$('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
– bicycle
Dec 14 '12 at 17:48
add a comment |
up vote
-2
down vote
if you want to scroll just only some div, can use the div id instead of 'html, body'
$('html, body').animate(...
use
$('#mydivid').animate(...
3
Nope,$('#div').animate(scrollTop:$('#div').position().top, 'slow');
doesn't work. (Chrome 23). Better use$('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
– bicycle
Dec 14 '12 at 17:48
add a comment |
up vote
-2
down vote
up vote
-2
down vote
if you want to scroll just only some div, can use the div id instead of 'html, body'
$('html, body').animate(...
use
$('#mydivid').animate(...
if you want to scroll just only some div, can use the div id instead of 'html, body'
$('html, body').animate(...
use
$('#mydivid').animate(...
answered Jul 5 '12 at 5:52
Fawas
132
132
3
Nope,$('#div').animate(scrollTop:$('#div').position().top, 'slow');
doesn't work. (Chrome 23). Better use$('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
– bicycle
Dec 14 '12 at 17:48
add a comment |
3
Nope,$('#div').animate(scrollTop:$('#div').position().top, 'slow');
doesn't work. (Chrome 23). Better use$('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
– bicycle
Dec 14 '12 at 17:48
3
3
Nope,
$('#div').animate(scrollTop:$('#div').position().top, 'slow');
doesn't work. (Chrome 23). Better use $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
– bicycle
Dec 14 '12 at 17:48
Nope,
$('#div').animate(scrollTop:$('#div').position().top, 'slow');
doesn't work. (Chrome 23). Better use $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
– bicycle
Dec 14 '12 at 17:48
add a comment |
up vote
-2
down vote
try this
$('#div_id').animate(scrollTop:0, '500', 'swing');
add a comment |
up vote
-2
down vote
try this
$('#div_id').animate(scrollTop:0, '500', 'swing');
add a comment |
up vote
-2
down vote
up vote
-2
down vote
try this
$('#div_id').animate(scrollTop:0, '500', 'swing');
try this
$('#div_id').animate(scrollTop:0, '500', 'swing');
answered Feb 26 '15 at 9:17
pirate
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4565381%2fscrolltop-jquery-scrolling-to-div-with-id%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown