Javascript,将我的字符串作为参数传递将其变为 0

Javascript, passing my string as an argument turns it to a 0

我正在使用 scrollmagic.io 并正在制作锚导航菜单。我正在关注 this tutorial。卷轴有效!除了滚动回到开头而不是它应该在的页面。

这是我的代码:

// init controller
var controller = new ScrollMagic.Controller();

// animate scroll instead of a jump
controller.scrollTo(function(target) {
  console.log('scroooooll');
  console.log('target = '+target); // THIS IS PRINTING 0
  console.log(typeof(target));
  /* Commenting out what it should do for simplicity. */
});

// scroll action when you click the nav links
$(document).on('click', 'a[href^=#]', function(e) {
  var id = $(this).attr('href'); // get the href of clicked link
  if ($(id).length > 0) { // not empty links
    e.preventDefault(); // prevent normal link action

    // this is the function call
    console.log('click');
    console.log('id = '+id); // IT PRINTS CORRECT HERE
    console.log(typeof(id));
    controller.scrollTo(id); // scroll on click

    // update the URL
    if (window.history && window.history.pushState) {
      history.pushState("", document.title, id);
    }
  }
});

这是我的控制台日志的输出:

click
id = #{the-href-value}
string
scroooooll
target = 0
number

我的 Javascript 很生疏,但这对我来说似乎不合适。当我将它作为参数传递时,为什么它会将我的变量从字符串更改为 0?

来自文档:

"This function will be used for future scroll position modifications. This provides a way for you to change the behaviour of scrolling and adding new behaviour like animation. The function receives the new scroll position as a parameter and a reference to the container element using this. It may also optionally receive an optional additional parameter (see below)"

所以,第一个参数是由控制器传递的。 之后你会得到你的参数。

http://scrollmagic.io/docs/ScrollMagic.Controller.html#scrollTo

尝试打印 console.log(args);

controller.scrollTo(function(scrollPos, targetHrefISent) {
  console.log('scroooooll');
  console.log('target = '+targetHrefISent); // THIS IS PRINTING 0
  console.log(typeof(targetHrefISent));
  /* Commenting out what it should do for simplicity. */
});