Google闭包相当于jQuery $('html,body')。animate({scrollTop:800});

Nic*_*ick 3 javascript jquery animation scroll google-closure

所讨论的代码使页面滚动动画到页面上的特定点。如标题所示,我希望Google Closure等效于以下jQuery:

$('html,body')。animate({scrollTop:800});

在此处表示html, body允许浏览器不一致,这$(document)是等效的。

我尝试了以下方法:

var anim = new goog.fx.dom.Scroll(document, [0, 0], [0, 800], 400);
anim.play();
Run Code Online (Sandbox Code Playgroud)

我也尝试过document.body

网络上没有演示程序,信息令人沮丧goog.fx.dom.Scroll

wil*_*nco 5

使goog.fx.Scroll类与文档(正文或HTML)一起使用的方法是通过以下方式传递文档scroll元素:

/**
 * Document scroll element obtained from goog.dom
 * @type {Element}
 * @private
 */
let documentScrollElement_ = goog.dom.getDocumentScrollElement()

/**
 * Function triggered by any target to start scrolling
 * @param  {Event} e Triggered event
 * @private
 */
function scrollTrigger_(e) {
  let googScroll = new goog.fx.dom.Scroll(
    this.documentScrollElement_,
    [0, 0],
    [0, 800],
    400);

  googScroll.play();
}
Run Code Online (Sandbox Code Playgroud)

就像您知道Scroll类具有此可选参数一样,它可以向滚动添加缓动。您应该像这样新建课程:

let googScroll = new goog.fx.dom.Scroll(
    this.documentScrollElement_,
    [0, 0],
    [0, 800],
    400,
    goog.fx.easing.inAndOut(t)); // Note that you need to import or require the goog.fx.easing object
Run Code Online (Sandbox Code Playgroud)

我知道这是一个老问题,但是我遇到了同样的问题并设法使其起作用,因此我认为值得回答这个问题。

希望能帮助到你!