滚动动动动动

94 阅读1分钟

做一个记录

function scrollTo(el) {
   const elOffset = el.offset().top;
   const elHeight = el.height();
   const windowHeight = $(window).height();
   let offset;
   if (elHeight < windowHeight) {
      offset = elOffset - (windowHeight / 2 - elHeight / 2);
   } else {
    offset = elOffset;
   }
   $('html, body).animate({
      {
        scrollTop: offset;
   }, 500)
}

调用:scrollTo($('xx')),你想要跳转到的目标元素