How do I get an element to scroll into view, using jQuery?

后端 未结 11 1427
夕颜
夕颜 2020-11-28 19:31

I have an HTML document with images in a grid format using

  • . The browser window has both vertical & horizontal scrolling.

11条回答
  •  不知归路
    2020-11-28 20:16

    My UI has a vertical scrolling list of thumbs within a thumbbar The goal was to make the current thumb right in the center of the thumbbar. I started from the approved answer, but found that there were a few tweaks to truly center the current thumb. hope this helps someone else.

    markup:

    jquery:

    // scroll the current thumb bar thumb into view
    heightbar   = $('#thumbbar').height();
    heightthumb = $('#thumbbar-' + pageid).height();
    offsetbar   = $('#thumbbar').scrollTop();
    
    
    $('#thumbbar').animate({
        scrollTop: offsetthumb.top - heightbar / 2 - offsetbar - 20
    });
    

提交回复
热议问题