How to insert declared CSS with Javascript after scrolling the page

Asked

Viewed 412 times

1

Well, I just wanted to make the javascript function work:

var $document = $(document),
    $element = $('#minhaDiv'),
    className = '.teste-css';

$document.scroll(function() {
  if ($document.scrollTop() >= 5) {
$element.addClass(className);
  } else {
    $element.removeClass(className);
  }
});  

But it doesn’t work at all, and it seems to be right!

I used:

<div id="minhaDiv" class=""><br><br><br>...<br><br><br><br><br><br><h1>Teste2</h1></div>

and in css

<style>
  .teste-css
{
  color: red;
}
</style>

and put on the end:

<!-- jQuery -->
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

But it doesn’t spin at all.

tried as well:

  className = '.teste-css'; //CLASS
  className = 'teste-css';  //TAG
  className = '#teste-css'; //ID

and so on...

scree

  • because your variables are with $ in front of the name?

  • variable declaration in JS does not have? The.o

  • I removed " . " from the class in variable. I tested and worked here.

  • Maybe it’s browser then... (firefox)

  • if you give a console.log( $Document.scrollTop() does it show or not? Inside $Document.scroll(Function() {

  • in php has, js do not know this need.(I may be wrong). I use $ to select tags(elements) html

  • thus? var Document = $(Document), element = $('#myDiv'), classname = 'test-css';

  • @Andrévicente does not show

  • So the problem is that even the scroll is not identifying, it is not the class name. Give some error in the browser console?

  • @Andrévicente Humm... gives error yes : no element found e04b:1:1 Typeerror: Document.getElementById(...) is null overlay.js:7333:5 Typeerror: aRequestOrigin is null policy.js:94:5 Typeerror: Document.getElementById(...) is null overlay.js:7333:5&#xA;TypeError: browserElement is null&#xA; overlay.js:7615:3&#xA;TypeError: aRequestOrigin is null policy.js:94:5&#xA;TypeError: document.getElementById(...) is null&#xA; overlay.js:7333:5&#xA;TypeError: aRequestOrigin is null

  • I believe I’m not finding the elements. I play everything inside the $( document ).ready(function() { //joga seu código jquery aqui });

  • Referenceerror: $ is not defined

  • Did not spin... <script>&#xA; &#xA; &#xA; $( document ).ready(function() { &#xA; &#xA; var $document = $(document),&#xA; element = $('#minhaDiv'),&#xA; className = 'teste-css';&#xA;$document.scroll(function() {&#xA; if ($document.scrollTop() >= 5) {&#xA; // user scrolled 50 pixels or more;&#xA; // do stuff&#xA; $element.addClass(className);&#xA; } else {&#xA; $element.removeClass(className);&#xA; }&#xA;}); &#xA; &#xA; });&#xA; &#xA; &#xA; </script>

  • Makes a mistake ReferenceError: $ is not defined and points to that line saying you’re in error $(document).ready(function() {

  • Check it out: https://jsfiddle.net/pmqnnjpm/

  • It ran right... but then it’s complicated in my ". php"... I’ll test without other page codes...

Show 11 more comments

1 answer

1


Before answering the question, a note about what was said in the comments: Javascript variable statements do not need the $ in the front. As far as I know, this is a way that developers have adopted to make it easier to read code, distinguishing that variable is an element of the DOM. In the same way that they use variables with upper-case name as convention for constants in some languages.

// Você 'bate o olho' e sabe que '$foo' é algum elemento no documento.
$foo.insertAdjacentHTML('afterend', '<p> olá </p>');

In your case, the problem is that you are running a script that depends on jQuery, but when it is run Jquery has not yet been loaded.

Another problem is that you are entering the value of className in function addClass(). The value has a . before the class name and this is not necessary, jQuery will try to add a class with name ..teste-css and it will never work. addClass already knows that a class will be inserted, so it is not necessary the point, only the class name ( teste-css ).

Your code would look like this:

<!doctype html>
<html>

<head>
  <style>
    .teste-css {
      color: red;
    }
  </style>
</head>

<body>
  <p>Desça a página</p>
  <div id="minhaDiv" class="">
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <h1>Teste2</h1>
  </div>
  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
    $(function(){
      
      var $document = $(document),
          $element  = $('#minhaDiv'),
          className = 'teste-css';
      
      $document.scroll(function(){
        if($(this).scrollTop() >= 5)
          $element.addClass(className);
        else
          $element.removeClass(className);
      });
    });
  </script>
</body>

</html>

  • Killed the answer Renan! Wow! I did not know that the sequence of implementation depended on each other... our that thing! rs changed the JQUERY order with the function and ran... O.o You and all the other friends! Thank you very much!! Thank you very much!!

  • 1

    Kara, thanks, cool, it’s already marked, thank you very much!! I’m new here!! thanks!!

Browser other questions tagged

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