[英]jQuery Waypoints add class when element is scrolled into view
I'm using the Waypoints plugin to check if an element is scrolled into view. 我正在使用Waypoints插件检查元素是否滚动到视图中。 I have multiple divs with class
item
as the user scrolls down the page, I want to add a class "viewed" to each. 当用户向下滚动页面时,我有多个带有类
item
div,我想为每个类添加一个“已查看”的类。
$(".item").waypoint(function(){
$(this).addClass("viewed");
console.log("yey");
});
The console.log works, but the .addClass
doesn't. console.log有效,但
.addClass
无效。 Does the plugin not support $(this)
? 插件不支持
$(this)
吗?
I finally got it working. 我终于让它工作了。
$(".item").waypoint(function(){
$(this[0,'element']).addClass("viewed");
});
The this
wasn't pointed at the element, so I needed to target it. this
不是针对元素的,所以我需要针对它。
You have to be careful when calling these callback functions, and what this really means. 在调用这些回调函数时,您必须小心, 这实际上意味着什么。 In this instance, its probably referring to your function.
在这种情况下,它可能是指您的功能。
Doesn't the event trigger pass the target in the function as an argument? 事件触发器不将函数中的目标作为参数传递吗? Try using that.
尝试使用它。 If you want to know what your nested this really is, console.log it.
如果你想知道你的嵌套这真的是,它CONSOLE.LOG。
$(".item").waypoint(function(thing){
$(thing).addClass("viewed");
console.log("yey");
});
The checked answer caused loads of errors in newer version of this plugin. 检查的答案在此插件的较新版本中导致大量错误。
This is what works for me: 这对我有效:
$(".item").waypoint(function() {
$(this.element).addClass("viewed");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.