eozzy
eozzy

Reputation: 68650

jQuery if hasclass addclass

/* If the object is completely visible in the window... */
if( bottom_of_window > bottom_of_object ){
    $(this).is(".block-left").addClass('fadeInLeft');
    $(this).is(".block-right").addClass('fadeInRight');
}

I wand to addClass by checking the elements class, but this doesn't seem to work.

Upvotes: 0

Views: 141

Answers (4)

Use .hasClass()

if($(this).hasClass("block-left")){
   $(this).addClass('fadeInLeft');
}

Upvotes: 0

Barmar
Barmar

Reputation: 780724

You have to use if

if( bottom_of_window > bottom_of_object ){
    if ($(this).is(".block-left")) {
        $(this).addClass('fadeInLeft');
    } else if ($(this).is(".block-right"))
        $(this).addClass('fadeInRight');
    }
}

Upvotes: 0

Felix
Felix

Reputation: 38102

You can use hasClass():

if( bottom_of_window > bottom_of_object ){
    if($(this).hasClass("block-left")) {
        $(this).addClass("fadeInLeft");
    } else if($(this).hasClass("block-right")) {
        $(this).addClass("fadeInRight");
    }
}

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

You need to use filter() instead of is() because is() returns a boolean value which will cause an error

$(this).filter(".block-left").addClass('fadeInLeft');
$(this).filter(".block-right").addClass('fadeInRight');

Upvotes: 1

Related Questions