6h8j5 6h8j5 - 1 month ago 8
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
$(this)
not work in this situation?

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

Answer

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');
Comments