jQuery - ScrollTop without animation

前端 未结 7 2101
鱼传尺愫
鱼传尺愫 2020-12-30 23:20

How can I use the scrolltop without an animation

This code works:

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


        
相关标签:
7条回答
  • 2020-12-30 23:24

    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/

    0 讨论(0)
  • 2020-12-30 23:28
    var offTop = $('#box').offset().top;
    $(window).scrollTop(parseInt(offTop))
    
    0 讨论(0)
  • 2020-12-30 23:30

    Try this:

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

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

    0 讨论(0)
  • 2020-12-30 23:34

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

    $(window).scrollTop(offTop)
    
    0 讨论(0)
  • 2020-12-30 23:37

    Skip jQuery. Just use JavaScript:

    window.scroll(0, 0);
    
    0 讨论(0)
  • 2020-12-30 23:37

    Can't you play with the duration ?

    var offTop = $('#box').offset().top;
    offTop  = offTop-43;
    $('#mainCt').delay('800').animate({scrollTop: '+=' + offTop + 'px'}, 1);
    
    0 讨论(0)
提交回复
热议问题