Javascript 将引用传递给调用jQuery插件方法的jQuery对象(窗口对象)
我试图将Javascript 将引用传递给调用jQuery插件方法的jQuery对象(窗口对象),javascript,jquery,Javascript,Jquery,我试图将$(窗口)作为参数传递给某个函数,但当我试图从函数内部获取scrollTop()时,它返回0,但如果我试图获取$(窗口)。scrollTop会给出正确的结果 您能告诉我我犯了什么错误吗?如果将元素作为参数传递不是正确的方法,那么它的替代方法是什么 下面是我的代码 //Funtion Declaration $.fn.someFunction = function (element) { var container = $(this), viewTop =
$(窗口)
作为参数传递给某个函数,但当我试图从函数内部获取scrollTop()
时,它返回0,但如果我试图获取$(窗口)。scrollTop
会给出正确的结果
您能告诉我我犯了什么错误吗?如果将元素作为参数传递不是正确的方法,那么它的替代方法是什么
下面是我的代码
//Funtion Declaration
$.fn.someFunction = function (element) {
var container = $(this),
viewTop = element.scrollTop(),
console.log($(window).scrollTop());//returns correct value
console.log(viewTop);//returns 0
}
//Calling the function
$fn.otherFunction = function(){
$(el).someFunction($(this));
}
$(window).otherFunction();
窗户的高度也是如此
// Following is the actual Code. It may be messy to look at but still.
$(window).scroll(function () {
$(window).scrolling();
$(".footer").displayFooter();
});
$.fn.scrolling = function () {
$(".parallax_active").each(function (i, el) {
$(el).parallax($(this));
})
// some more stuff goes here.
};
$.fn.parallax = function (parent) {
var container = $(this),
viewTop = parent.scrollTop(),
viewHeight = parent.height(),
viewHeader = $(".header-container").outerHeight(),
viewBottom = viewTop + viewHeight,
header = viewTop + viewHeader;
console.log($(window).scrollTop());
console.log(viewTop);
console.log(viewHeight);
}
谢谢,
在.each()
方法中,此
引用您正在迭代的元素
如果希望this
绑定到调用.scrolling()
方法的元素,请在之外获取对this
的引用。each()
方法:
$.fn.scrolling = function () {
var self = this;
$(".parallax_active").each(function (i, el) {
$(el).parallax($(self));
})
};
您还可以清楚地传递一个jQuery对象,该对象也包含窗口对象:
$(el).parallax($(window));
所以听起来像是这个
不是指窗口
对象。您需要提供更多关于如何调用此函数的上下文。顺便说一句,我不明白为什么不直接使用窗口
对象如果你有jquery函数,这个参数在这种情况下是不需要的,因为这意味着$(el)。您可以通过此访问内部函数。它实际上是$(el).someFunction($(this));正在从其他函数(如$(window).otherFunction()中调用;这不是您正在使用的真正代码,因为发布的代码包含一些打字错误,所以请提供相关代码和示例以复制问题。在每个
循环中,这
不是指窗口