6h8j5 6h8j5 - 1 year ago 71
Javascript Question

$(this) not working within if ($(".element").is()

My expected result for the code below is

.csr-area-3 .csr-video
with added classes
csr-animate csr-zoom-in
, why does
not work in this situation?

if ($(".csr-area-3 .csr-video").is(":in-viewport")) {
$(this).addClass('csr-animate csr-zoom-in');

Answer Source

You need .filter(function), to get the matched element which are in-view port and then add class to then

$(".csr-area-3 .csr-video").filter(function() {
    return $(this).is(":in-viewport");
}).addClass('csr-animate csr-zoom-in');

You could also use

$(".csr-area-3 .csr-video:in-viewport").addClass('csr-animate csr-zoom-in');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download