1

我正在尝试检查deltaYin framer 滚动事件并仅在deltaY == 0.

看来成帧器(用coffeescript编写)没有办法检查这一点。还有另一种说法(用伪代码):

if the change of the Y scrolling has been zero for 30 frames, execute function

framer 滚动事件有这个方法:

scrollComp.isMoving

在此页面上发现: https ://framer.com/docs/#scroll.scrollcomponent

但是如果我尝试这个,语句的 else 部分不会打印任何内容

if scrollComp.isMoving
    print 'moving'
else if scrollComp.isMoving == false
    print 'stopped'

///或者这也不起作用:

if scrollComp.isMoving
    print 'moving'
else 
    print 'stopped'
4

1 回答 1

2

Coffeescript 等效于==is is,实际上等效于===(检查值和类型)。

话虽这么说,if scrollComp.isMoving == false说起来有点尴尬,在 JS中说类似unless scrollComp.isMoving或这样的东西更有意义。if(!scrollComp.isMoving)

好的,对于您的问题的解决方案(我不相信上述两件事中的任何一个都可以真正解决),当您执行这些print语句时,您很可能是在脚本启动时这样做而不是在事件处理程序中的异步。当您的页面加载时,是您的代码进入该 if/else 语句时,此时您不会滚动,因此它将始终是false. 要捕捉滚动的瞬间并在滚动发生时运行代码,您需要注册一个事件监听器:

scrollComp.onMove ->
  // Scrolling here! Do fancy stuff!
  print scrollComp.isMoving     // 'true'

现在,为了能够在滚动停止 30 秒后触发函数调用,我们必须跟踪时间:

// Define interval as 30 seconds.
// (expressed in milliseconds)
interval = 30*1000
time = Date.now() // set timer to now

scrollComp.onMove ->
  // We update the timer every time
  // scroller moves.
  time = Date.now()

// We need to create an infinite loop
// that will check the time since last 
// move of the scroller, and execute
// a function when the time has surpassed
// some threshold.
setInterval ->
  if (Date.now() - time) > interval
    // It has been 30 seconds since
    // scroller last moved.
, 5000 

最后一个5000数字就是运行时间检查的频率;这将每 5000 毫秒运行一次。

如果您真的想计算帧数,您可以通过计算帧速率并使用一些代数柔术来动态生成该interval变量。

于 2017-07-25T05:08:10.020 回答