简体   繁体   English

角度js动画以滚动到视口顶部

[英]angular js animate to scroll to top of viewport

I am new to angular and trying to do something that is already working in Jquery. 我对angular并不陌生,正在尝试做一些已经在Jquery中工作的东西。 I do not want to use jquery and angular together and wanted just code it in angular and need some help using this in a directive. 我不想将jquery和angular一起使用,只想用angular对其进行编码,并且需要在指令中使用它的一些帮助。

// Calculate the current scroll position...
    scrTop = (window.scrollY || window.pageYOffset);

    newTop = $elemt.offset().top - 20;
    //$elemt: an Object representing a jQuery selected page element.
    diff = newTop - scrTop;

    if (Math.abs(diff) > 20) {
        newScrTop = scrTop + diff;
        //where NAV_HEIGHT of div id corresponding to the element in the top navigation
        if (diff < 0) newScrTop -= NAV_HEIGHT;
        if (newScrTop < 0) newScrTop = 20;
        jQuery('html,body').animate({scrollTop: newScrTop + 'px'}, 1000);

Angular基于轻量级版本的jQuery(jqLit​​e),因此即使从页面中删除了显式的jQuery导入,您也可以简单地在Angular中使用代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM