我正在尝试在我的反应应用程序上使用jquery.scrollto但似乎无法正确使用它。
我是通过npm安装的
>npm install jquery.scrollto
+ [email protected]
added 1 package in 5.316s
但是当我尝试使用它时,我得到一个typeError
Uncaught TypeError: $(...).scrollTo is not a function
这是应用程序内部的代码:
var $ = require('jquery')
require('jquery.scrollto')
$('#home').on('click',function(){
$(window).scrollTo('#home', 1000);
})
这是我的package.json:
{
"name": "portfolio",
"version": "0.1.0",
"private": true,
"dependencies": {
"jquery": "^3.3.1",
"jquery.scrollto": "^2.1.2",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-scripts": "1.1.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
我错过了什么吗?非常感谢。
经过几次尝试,我发现了如何使它工作。
我要求使用scrollto:require('jquery.scrollto')
然后通过jquery的$()
调用它。显然,当通过npm安装scrollTo
时,它不是要走的路。我做的是以下内容:
var $ = require('jquery');
var scrollTo = require('jquery.scrollto');
// inside react's componentDidMount :
$('#testbutton').on('click',scrollto(0,500))
我不知道scrollto不需要嵌套在jquery中。
此外,我意识到使用字符串引用是无益的,我建议人们阅读这个帖子来访问ReactJS how to scroll to an element以获取更多信息。感谢@davidbucka