Search code examples
jquerymootools

jQuery equivalent of MooTools' Scroll().toElementCenter()


I am adapting someone else's script to my site. The use MooTools; I use jQuery. Their code has the following line:

new FX.Scroll(window).toElementCenter('obj');

How would I do this in jQuery?


Solution

  • If you check the docs you see this line

    new FX.Scroll(window).toElementCenter('obj');
    

    means basically:

    Do a animated scroll to the center of the element with the id "obj"

    And in jQuery that can be done with

    var obj = $('#obj'); // cache the element
    $('html, body').animate({
        scrollTop: obj.offset() + (obj.height() / 2) // scroll to top of element + half of its height
    }, 1000); // 1 second fast animation