0

I have fullscreen fixed div, with content from ajax query. Div is scrollable, because has a lot of content. And now, on bottom, I need a button to scroll to top of div. I try scroll with <a href="#divId"></a> but, this looks like doesn't work, similiary ScrollTop() to div don't work too.

shilovk
  • 11,718
  • 17
  • 75
  • 74
Michaell94
  • 484
  • 1
  • 5
  • 14
  • 1
    I'm wondering, where's your code (HTML/JS)? Also, I believe this has been answered before - http://stackoverflow.com/questions/5846595/how-to-scroll-an-item-inside-a-scrollable-div-to-the-top – hungerstar Sep 15 '16 at 15:55
  • Well for one, `ScrollTop()` will only work with a lowercase `s`. Additionally, you don't want to `scrollTop()` *to* `div`, you want to set the `scrollTop()` *of* div (to 0). Please see my answer with working example. – Tyler Roper Sep 15 '16 at 16:04
  • @Santi yes, thats works perfect! – Michaell94 Sep 16 '16 at 10:52

2 Answers2

5

Replace longDiv with your div id.

<div id="longDiv">Bla bla bla</div>
<button>Scroll to top</button>

<script>
   $(document).ready(function() {
       $('button').click(function() {
           $('#longDiv').scrollTop(0);
       });
   });
</script>

Working JSFiddle: https://jsfiddle.net/8hasscb6/

If you prefer it to be animated, you can use .animate():

<script>
    $(document).ready(function() {
        $('button').click(function() {
            $('#longDiv').animate({ scrollTop: "0px" });
        });
    });
</script>

JSFiddle: https://jsfiddle.net/8hasscb6/1/

Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
-2

Try this

<a onclick="scroll(0,0)">

This should go to top

O.Rares
  • 1,031
  • 1
  • 16
  • 19
  • @hungerstar Yes and div is fixed so he can change the input values – O.Rares Sep 15 '16 at 16:02
  • @hungerstar Put the code in a .html file and run it in browser.It works,I tested it,I think that jsfiddle accepts js code that is written only in its box – O.Rares Sep 15 '16 at 16:24
  • @hungerstar No problem,he can also use jquery for animation because he has it already – O.Rares Sep 15 '16 at 17:06