2

I have two functions, one which scrolls to the bottom of my page and another which scrolls back to the top.

I'm looking to add another function which scrolls to a specific div which is almost at the bottom of my page. I have included my two functioning scroll functions below and also my third attempt which doesn't work properly. It scrolls to the bottom of my page and then jumps up to the div.

   //works
   $('.btn1').click(function(){
     $('html, body').animate({scrollTop:0}, 750);
   });

   //works
   $('.btn2').click(function(){
     $('html, body').animate({scrollTop:$(document).height()}, 750);
     return false;
   });

   //doesn't work
   $('.btn3').click(function(){
     $('html, body').animate({scrollTop:$(".myDiv").offset().top}, 750);
     return false;
   });

Any advice or suggestions would be greatly appreciated!

Daft
  • 10,277
  • 15
  • 63
  • 105
  • Why are you doing `animate` on `html` **and** `body`? – Shikiryu Apr 08 '13 at 15:49
  • 1
    Can we see the html? Are you sure `
    ` exists?
    – Rob Forrest Apr 08 '13 at 15:50
  • do also a console.log() of the offset().top and show us result – steo Apr 08 '13 at 15:52
  • I found a simple tut online which included ('html, body'). It works for me so I didn't think I should change it. And in my html the div is actually called .nameSpaceInfo. It definitely exists. I just called it .myDiv here so it would be easier to understand. – Daft Apr 08 '13 at 15:55

4 Answers4

1

Here is a fiddle and it appears fine to me.

i just added sample div

<span class="btn2">to bot</span>
<span class="btn3">to div</span>
<div class="myDiv"></div>
<span class="btn1">to top</span>
<span class="btn3">to div</span>
.myDiv
{
    height:500px;
    margin-top:500px;
    width:100%;
    background-color:#ccc;
}

i left your script as it is.

trajce
  • 1,480
  • 3
  • 23
  • 38
1

html

<div class="btns">
<button class="btn1">One</button>
<button class="btn2">Two</button>
<button class="btn3">Three</button>
</div>
<p>stuff</p><p>stuff</p><p>stuff</p>
<p>stuff</p><p>stuff</p><p>stuff</p>
<p>stuff</p><p>stuff</p><p>stuff</p>
<p>stuff</p><p>stuff</p><p>stuff</p>
<p>stuff</p><p>stuff</p><p>stuff</p>
<p>stuff</p><p>stuff</p><p>stuff</p>
<div class="myDiv">My Div</div>
<p>stuff</p><p>stuff</p><p>stuff</p>

css

.btns {
    position: fixed;
}

javascript

   //works
   $('.btn1').click(function(){
     $('html, body').animate({scrollTop:0}, 750);
   });

   //works
   $('.btn2').click(function(){
     $('html, body').animate({scrollTop:$(document).height()}, 750);
     return false;
   });

   // works
   $('.btn3').click(function(){
     $('html, body').animate({scrollTop:$(".myDiv").offset().top}, 750);
     return false;
   });

example

1

My guess (and that's all it is based on what we have) is that you've confused a div id for a class and have

<div id='myDiv'>

So there's no .myDiv to scroll to.

Either change your html to

<div class='myDiv'>

or your jQuery to (note the #myDiv in the selector)

//should work
$('.btn3').click(function(){
  $('html, body').animate({scrollTop:$("#myDiv").offset().top}, 750);
  return false;
});
Rob Forrest
  • 7,329
  • 7
  • 52
  • 69
0

If you have a div and you set an ID on it you can do this in your HTML:

<a href="#yourDiv">Scroll to Div</a>

That should just jump straight to the DIV, and it'll be at the top of the page.

Jonny Sooter
  • 2,417
  • 1
  • 24
  • 40