29

How can I use the scrolltop without an animation

This code works:

var offTop = $('#box').offset().top;
offTop  = offTop-43;
$('#mainCt').animate({scrollTop: '+=' + offTop + 'px'}, 400);

And here are my (not working solutions):

$("#mainCt").scrollTop('+=' + offTop + 'px');                 // doesn't work
$("#mainCt").scrollTop('+='+offTop);                          // doesn't work
hhh = setTimeout(' $("#mainCt").scrollTop('+offTop+');',800); // doesn't work

DEMO
http://jsfiddle.net/DNNFF/9/

Marc
  • 4,661
  • 3
  • 40
  • 62
user970727
  • 1,947
  • 4
  • 22
  • 28

9 Answers9

24

Try this:

var offTop = $('#box').offset().top - 43;
$('#mainCt').scrollTop(offTop);

The scrollTop property accepts just an integer, no suffixes or units required.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
19

Skip jQuery. Just use JavaScript:

window.scroll(0, 0);
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Jeroen Flamman
  • 965
  • 6
  • 10
18

maybe if you don't want an animation or anything fancy just use an anchor

<a name="top"></a>

Place it where you need to scroll

and in your function where you are calling use

document.location.href="#top";

You could also create a function to append the anchor before the element, do the document.location thing and later remove that anchor.

http://jsfiddle.net/fSrxr/1/

chepe263
  • 2,774
  • 22
  • 38
  • 9
    This works, but how can i use jQuery ScrollTop without an animation? – user970727 Apr 18 '12 at 06:56
  • 4
    Whilst this answer "works", it messes with the browser history. $(element).scrollTop(offset) is better for the reposition without animation, and without messing with the browser history. Especially $(window).scrollTop(offset) for repositioning a jQuery UI Accordion section after the accordion has loaded. – Reuben Apr 12 '16 at 22:59
12

http://api.jquery.com/scrollTop/

$(window).scrollTop(offTop)
sod
  • 3,804
  • 5
  • 22
  • 28
  • if mainCt is a layer with a scrollbar (overflow:auto) then you can use it on mainCt. If you want to scroll the browser viewport, then you have to use window. – sod Apr 13 '12 at 15:36
  • right, mainCt is a div with overflow-x: hidden; overflow-y: scroll; – user970727 Apr 13 '12 at 15:37
3

Why not use it with less duration. I fiddled it and it had no animation. SInce there will be no time to see the animation, there will be no animation at all.

var offTop = $('#box').offset().top;
offTop  = offTop-43;
$('#mainCt').animate({scrollTop: '+=' + offTop + 'px'}, 50); //50 added here as duartion
1

Can't you play with the duration ?

var offTop = $('#box').offset().top;
offTop  = offTop-43;
$('#mainCt').delay('800').animate({scrollTop: '+=' + offTop + 'px'}, 1);
Mr_DeLeTeD
  • 825
  • 1
  • 6
  • 18
0

Demo with keep position on refresh page using jQuery:

//------KEEP SCROLL POSITION
        $(window).scroll(function () {
            sessionStorage.scrollTop = $(this).scrollTop();
        });
        $(document).ready(function () {
            if (sessionStorage.scrollTop != "undefined") {
                setTimeout(() => window.scrollTo({
                    top: sessionStorage.scrollTop,
                    left: 0,
                    behavior: 'auto'
                }), 5);
            }
        });
O Thạnh Ldt
  • 1,103
  • 10
  • 11
0

[EDITED] This code will reset the view to its original starting position with its height equal to the device height temporarily excluding and disabling scroll and overflow. In short, it has the same function as scrolling to top without animation.

$('#yourButton').on('click', function(){
    $('html, body').css({ overflow: 'hidden', height: '100%' });
});
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Jun 12 '22 at 13:35
-1
var offTop = $('#box').offset().top;
$(window).scrollTop(parseInt(offTop))