Selecting class with jquery

Asked

Viewed 30 times

0

What’s the difference in selecting the class of these two ways in JQUERY?

One has access to parameters, other not, how it works ?

var valor = $(".classe");
var valorAnime = 'classe';

Take a look at this code :

 (function(){
 let $target =$(".projeto"),
    animeStart = 'projeto-anime',
    offset = $(window).height() * 3/4;

    function animeScroll (){
        let documentTop = $(document).scrollTop();

        $target.each(function(){
            let itemTop = $(this).offset().top;

            if( documentTop > itemTop - offset) {
                $(this).addClass(animeStart);
            } else {
                $(this).removeClass(animeStart);
            }
        }) 
    }

 animeScroll();

 $(document).scroll(function(){
     animeScroll();

 });
 }()); 

1 answer

0


The way I see it, when you do

var valor = $(".classe");

you are searching on the page for the object whose class corresponds to classe, when Voce uses this code.

var valorAnime = 'classe';

you are assigning to the variable valorAnime a string whose content is class

EDITED:

well that second code of yours does the following thing;

let $target =$(".projeto"),

holds what has class project

inside the loop he’s making on the objects of $target, after checking on the if he uses the $(this) that nothing else is the object being iterated, and adds to it a class with the command addClass where the class name is anime project

If I’m not mistaken it would work the same way if I did:

            if( documentTop > itemTop - offset) {
                $(this).addClass('projeto-anime');
            } else {
                $(this).removeClass('projeto-anime');
            }

But for modularization reasons, it is better to use it the way you presented it first. I hope I was clear

  • Take a look at the code I added. I understood his explanation, but the way this code is he has access to the project-anime class that is in css and the function is executed without errors, if I select the class the same way I selected the first, error. I can not understand precisely this second form that was used to select the class.

  • I edited my answer to fit the question, I hope it was clear and you understood what is happening

  • yes, yes. thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.