How to combine conditions with several selector?

user3334919

Hy, I'm trying to combine these condition:

$(window).scroll(function() {

if ($('#box2').isOnScreen() == true){
        $('#box2').css('opacity', '1')}
else{$('#box2').css('opacity', '0')};
        });

$(window).scroll(function() {

if ($('#box3').isOnScreen() == true){
        $('#box3').css('opacity', '1')}
else{$('#box3').css('opacity', '0')};
        });

I tried something like that:

$(window).scroll(function() {

    if ($('.box').isOnScreen() == true){
            $(this).css('opacity', '1')}
    else{$(this).css('opacity', '0')};
            });

but it don't work

my html look likes this

<div id="box2" class="box"></div>
<div id="box3" class="box"></div>

Thanks

afrin216

You need jquery's each method

$('.box').each(function(){
    if ($(this).isOnScreen() == true){
        $(this).css('opacity', '1')
    } else{
        $(this).css('opacity', '0')};
    }
});

Something like this, should do the trick.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related