1

I want to select every single element in a document and make them color red when I scroll to them.

$(document).ready(function() {
  $(document).on("scroll", animationDivs);

  function animationDivs(event) {
    var scrollPos = $(document).scrollTop();

    var divs = $("*");
    $(divs).each(function() {
      var currLink = $(this);
      if (currLink.position().top <= scrollPos && currLink.position().top + currLink.height() > scrollPos) {
        currLink.style.color = "red";
      }
    });
  };
});

I used this codes but didn't work.

Louys Patrice Bessette
  • 27,837
  • 5
  • 32
  • 57

4 Answers4

1

using JS:

document.querySelectorAll('*')
        .forEach(el => el.style.color = 'red')

Try it in the console of your browser to see how it works and here's a brief overview of DOM selection with JS vs jQuery.

This is a similar question with a variety of solutions.

saylestyler
  • 141
  • 1
  • 15
0

First add some common css class on each divs and add following jquery.

$('.class-name').each(function() {
   var currLink = $(this);
   if (currLink.position().top <= scrollPos && currLink.position().top + currLink.height() > scrollPos) {
   currLink.style.color = "red";
   }
});
Daniel Smith
  • 1,294
  • 18
  • 42
0

using jq, you could simple get all element withing the html by "*"

var items = $("*").css({background : "red"})

console.log(items)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div></div>

<p></p>
Alen.Toma
  • 4,792
  • 2
  • 11
  • 24
0

currLink is a jQuery object in your code. So use a jQuery method on it.

That would be the .css() method in your case.

And I suggest you use an else part to your condition so the elements does not turn red after the first single wheel spin... Since <body> is also collected in $("*").

$(document).ready(function() {
  $(document).on("scroll", animationDivs);

  function animationDivs(event) {
    var scrollPos = $(document).scrollTop();

    var divs = $("*");
    $(divs).each(function() {
      var currLink = $(this);
      if (currLink.position().top <= scrollPos && currLink.position().top + currLink.height() > scrollPos) {
        currLink.css({"color":"red"});
      }else{
        currLink.css({"color":"initial"});
      }
    });
  };
});
.spacer{
  height:500px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<br>
<span>Scroll me.</span>
<div class="spacer"></div>
<div>div
  <p>paragraph</p>
  <a>anchor</a>
  <span>span</span>
</div>
<div class="spacer"></div>

By the way... Using an .each() loop on the $("*") collection on every scroll event is the worst jQuery usage I suppose I will ever see. I can assure you that you'll scratch your head pretty soon with a real web page with real content.

Every elements, including <br> and <script> and <link> etc. are collected using $("*") that way... And are compared in the loop. You should only use it when absolutely necessary and within at least a container to lower the amount of collected elements.... Like $(".some-class *").

Louys Patrice Bessette
  • 27,837
  • 5
  • 32
  • 57