if(scrollTop >= 300){
fn();
}
修改为
if(scrollTop >= 300){
fn.call(_this);
}
@杀心人 正解哦,因为传入的函数fn定义时的上下文是window,所以fn在非严格模式下执行时this==window。
要改变this引用,最直接明了的方法就是:
_this.fn=fn;
...if(scrollTop==300){
_this.fn();//fn函数现在的上下文就是_this了
然后发现这个还是太繁琐了,于是就有了call(context,args1,args,...)和apply(context,[args1,args2,...])方法:
...if(scrollTop==300){
fn.call(_this);//fn函数现在的上下文就是_this了
//或者
fn.apply(_this);//这个和call的差异就是这个可以把函数得参数用数组形式传进去
/*
具体可已参考http://www.w3school.com.cn/js/pro_js_inheritance_implementing.asp
*/
在插件中
fn(_this); //将$(this) 传给fn() :
html中
....................
.........rolling(function(img) {
img.css ................
},100);
......................