I’m using this plugin to detect if an element is visible or not on scroll. I am able to add the classes
just fine when I scroll and the element becomes visible. However, I am not able to remove the classes when
the elements become invisible.
I have since used console.log(IsVisible) and it is showing me that the value for IsVisible (I did correct the capitalization) is still evaluating to false. So it doesn’t make sense to me that it is still adding the classes on scroll.
Yes. Assigning IsVisible = true will also return true, which is the reason why the classes are getting added but never removed.
If the code is the same as in your edited OP, then this cannot be the case… either IsVisible is false or it is not. Sorry for the stupid question, but do you maybe have some sort of typo in your actual code?
I’m copying and pasting it again to update the code… I believe that this is the same code that I updated. Yes the fact that it is adding the classes but displaying false is what is confusing to me too.
That is so weird. I see that yours is working. I can see that IsVisible value changing for you in the console. I wonder why it isn’t changing for me. I put up a dev site. Maybe this will offer more insight. I can’t see any difference in the code though. http://connect4web.staging.wpengine.com/
Here’s an updated fiddle. (Horizontal visibility can be implemented analogously, of course.)
BTW, having a look at their code, I find it a bit curious that they’re elaborately dealing with native methods and properties (such as a fallback for .getBoundingClientRect()), when jQuery has already taken care of all of this and they’re writing a jQuery plugin in the first place. Or to phrase it the other way round, the way they’ve written it they could just as well have written it for the Element.prototype w/o any need for jQuery. :-\
This is working pretty good. It looks like you completely abandoned the plugin. The funny thing is that I was hoping that the plugin would be more efficient in the future. I guess it may be once it is updated some.
I was doing something similar to this on this page http://connect4web.staging.wpengine.com/services/ (see the ONLINE MARKETING header) and my code was quite long to have to do for each element I wanted to use it with.
So with $.fn.visible you are creating a new function in jQuery, correct?
Then return $(this) is referring to that new function? or is return $(this) referring to the element (‘$ArticleHome’) here?
var IsVisible = $(‘#ArticleHome’).visible();
Yes, if you’d include both, one would overwrite the other. If you want to keep the other one as well, just rename that method appropriately.
Yes, this is referring to the current jQuery object here. jQuery methods would typically return this, which is why most of them are chainable – the next method is called on the object the previous method returned. If this case, of course, the method is returning a boolean and thus not further chainable with other jQuery methods. If you’re interested in how to write such custom methods, have a look here.