Javascript,将我的字符串作为参数传递,将其转换为0
我正在使用scrollmagic.io并制作一个锚导航菜单。我跟在后面。这卷轴行得通!除了它是滚动回到开始,而不是它应该在的页面 这是我的密码:Javascript,将我的字符串作为参数传递,将其转换为0,javascript,scrollmagic,Javascript,Scrollmagic,我正在使用scrollmagic.io并制作一个锚导航菜单。我跟在后面。这卷轴行得通!除了它是滚动回到开始,而不是它应该在的页面 这是我的密码: // init controller var controller = new ScrollMagic.Controller(); // animate scroll instead of a jump controller.scrollTo(function(target) { console.log('scroooooll'); cons
// 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?来自文档:
此功能将用于将来的滚动位置修改。
这为您提供了一种更改滚动和
添加新的行为,如动画。函数接收新的
滚动位置作为参数和对容器的引用
元素使用这个。它还可以选择性地接收可选的
其他参数见下文
因此,第一个参数由控制器传递。
之后,您将获得您的参数
尝试打印console.logargs
你试过在浏览器中调试它吗?我正在使用我的浏览器。输出是我单击链接时控制台日志打印的内容。我正在尝试找出0的来源。我说的是使用调试器,而不仅仅是console.log。例如,第一次调用controller.scroll时,您正在向它传递一个函数,第二次只传递一个id。我不知道哪一个是正确的,但我想这就是产生差异的原因。按照我的理解,它应该是目标元素的滚动位置。所以它应该是一个数字。0对我来说意味着你给它的id与页面上的元素不匹配,或者插件由于任何原因无法获得所述元素的y位置。这有点帮助。我传递了一个任意数作为我的第一个参数,它现在可以工作了!谢谢
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. */
});