0

i need to add class after 1070 i have the code for that and its work great:

jQuery( document ).ready(function( $ ) {
	$(window).scroll(function() {
	    var scroll = $(window).scrollTop();
      
	    if (scroll >= 1070) {
		$(".header-main").addClass("extrablack");
	    } else {
		$(".header-main").removeClass("extrablack");
	    }
	});
});

now i need to add class extrablack in 1070 but remove in 1580 how can i do that? I need the class stay between 1070 and 1580 of website. thanks

5
  • sorry this doesn't work. Commented Apr 23, 2018 at 18:16
  • if (scroll >= 1070 && scroll < 1580) then add class else remove it (important the else part) Commented Apr 23, 2018 at 18:20
  • @Mohamed-Yousef your condition is an impossibility Commented Apr 23, 2018 at 18:35
  • i need to add class in 1070 and after 1580 remove. this is impossibility? Commented Apr 23, 2018 at 18:37
  • sorry @Vachos my bad .. I don't know how I saw 1070 bigger then 1580 .. I need to change my Ophthalmologist :-) .. sorry again .. Good Luck Commented Apr 23, 2018 at 20:28

2 Answers 2

2

Is this what you were looking for?

jQuery( document ).ready(function( $ ) {
	$(window).scroll(function() {
	    var scroll = $(window).scrollTop();
      
	    if (scroll >= 1070 && scroll < 1580) {
		$(".header-main").addClass("extrablack").text(scroll);
	    } else {
		$(".header-main").removeClass("extrablack").text(scroll);
	    }
	});
});
.header-main{
  position: fixed;
  width: 100%;
  height: 100px;
  background: lightgray;
  color: white;
  text-align: center
}

.header-main.extrablack{
  background: black
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="header-main"></div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

Sign up to request clarification or add additional context in comments.

Comments

0
jQuery( document ).ready(function( $ ) {
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();

        if (scroll >= 1070 && scroll <= 1580) {
          $(".header-main").removeClass("extrablack");
          return;
        }

        if (scroll >= 1070) {
          $(".header-main").addClass("extrablack");
        }   
    });
});

2 Comments

sorry this doesn't work. this add class but dont remove it.
@Vachos try it now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.