0

I've done a lot of research to try find a solution for this problem. I have a project where I am using parallax (magicScroll). I have a hamburger icon (where show/hide the menu), however this icon is black and I need change the color based on image background. So if I have a dark image as background the hamburger icon should be white and if I have a white background the icon should be black.

I've tried a lot of ways to do that, however I am using parallax. So I am having problems to write the function and I need help. My menu icon (.hamburger) is fixed at the header (position:fixed) and I have the sections below. The problem is: How identify the section in viewport (visible) and how add/remove the classe in my menu icon (.hamburger).

I was trying something like that:

 var setMenu = function() {
  if($('#section-3').inViewport()) {
    $('.hamburger').addClass('navwhite')
  } else {
    $('.hamburger').removeClass('navwhite')
  };
};

I was trying something like that to test, but without luck. :( Once my #section-3 hits the top of viewport, because the parallax, my ID never leaves this position, so I need help to learn how to do that.

Thanks in advance!

George
  • 1
  • 1
  • Your `inViewport()` function is wrong for your case, it needs adjustments. Check some jQuery libraries as [this one](https://github.com/zeusdeux/isInViewport). – skobaljic Sep 05 '16 at 13:47

6 Answers6

0

will check if page has been scrolled up to or greater then section "section-3" then "navwhite" class will be added otherwise it will be removed.

$(window).scroll(function() {
    if ($(this).scrollTop() >= $('#section-3').offset().top) {
        $('.hamburger').addClass('navwhite')
    }
    if($(this).scrollTop() < $('#section-3').offset().top) {
        $('.hamburger').removeClass('navwhite')
    }
});
Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
0

The following code proves if an element has completely entered the viewport and sets or removes a class:

var section = $('.section');
var sectionTop = section.offset().top + section.height();

$(window).bind('scroll', function() {
    var windowTop = $(window).scrollTop() + $(window).height();

    if (windowTop > sectionTop) {
          $('.hamburger').addClass('navwhite')
    }
    else {
        $('.hamburger').removeClass('navwhite')
    }
});
Michael Andorfer
  • 1,660
  • 5
  • 25
  • 45
0

Since your problem addresses background and foreground colors as far as I understood, you could also have a look at the following library: BackgroundCheck

Michael Andorfer
  • 1,660
  • 5
  • 25
  • 45
0

Please have a look at this answer: Determine distance from the top of a div to top of window with javascript.

Using Jaspers code, you are able to calculate the distance from a element to to top edge of the viewport.

Then you could compare the size of your background and it's current position on the screen:

// distance defined like in Jaspers answer linked above
if(distance <= 0 && distance >= (0-$('#section-3').outerHeight())){
$('.hamburger').addClass('navwhite');
}else{
$('.hamburger').removeClass('navwhite');
}

I have forked Jaspers JSFiddle to add a class red to the body element, as soon as the yellow square touches to upper document bound and removes the class, as soon as the yellow square stops touching the edge: http://jsfiddle.net/4psxe6uL/

Community
  • 1
  • 1
RicoBrassers
  • 91
  • 1
  • 8
0

This is in pure js. It triggers on scroll and changes the class as necessary.
Assumes body is your container element.

document.body.addEventListener('scroll',function(){
 if (document.body.scrollTop<document.querySelector('#section-3').style.height) {
   document.querySelector('.hamburger').setAttribute('class','navwhite');
 } else if (document.body.scrollTop>=document.querySelector('#section-3').style.height) {
  document.querySelector('.hamburger').removeAttribute('class');
 }
});
Eason
  • 31
  • 3
-1

its simple

jQuery( "classname/id of the div" ).eq(position of the div).addClass(new class name);
  • The question is about the div's position (=location) on the screen, not its index relative to the parent. – JJJ Dec 06 '18 at 07:47