Scroll to element on click in Angular 4

后端 未结 12 1034
逝去的感伤
逝去的感伤 2020-11-29 18:57

I want to be able to scroll to a target when a button is pressed. I was thinking something like this.

12条回答
  •  没有蜡笔的小新
    2020-11-29 19:23

    You can do this by using jquery :

    ts code :

        scrollTOElement = (element, offsetParam?, speedParam?) => {
        const toElement = $(element);
        const focusElement = $(element);
        const offset = offsetParam * 1 || 200;
        const speed = speedParam * 1 || 500;
        $('html, body').animate({
          scrollTop: toElement.offset().top + offset
        }, speed);
        if (focusElement) {
          $(focusElement).focus();
        }
      }
    

    html code :

    
    

    Apply this on elements you want to scroll :

    some content

    Here is a stackblitz sample.

提交回复
热议问题