jQuery don't hover animation when div is on top of page

What I want to accomplish: If you hover over the div that says hey, I want a background transition but when that div transitions (when it reaches the red part), I don't want that hover effect on the div.

If you hover over my ".ok" div (the one that says hey), it has a background and color transition.

However, when it gets to the red part, I don't want the hover part to happen on the ".ok" div.

Issues to note: I made an if else statement but it doesn't seem to work.
So I put it in my $(window).scroll function but it only works if you actually scroll. On page load, it doesn't work.

fiddl: https://jsfiddle.net/jzhang172/j2yrumyg/3/

$(function(){

var cheese= $('.ok').offset().top; //Define top of 'hey'

//
//Animates when it reaches red part of page
//
$(window).scroll(function() {
    if ( $(window).scrollTop() >= cheese  ) {
  
        $('.ok').addClass('top');

    }
    else{
           $('.ok').removeClass('top');
        
    }
});

//
//This part doesn't rely on scroll event and is in charge of changing hover on ".ok" div.
//
    if ( $(window).scrollTop() >= cheese  ) {
  

    }
else if ( $(window).scrollTop() <= cheese  ){
      $('.ok').hover(function(){
              $(this).addClass('proj-hover');
              },function(){
                $(this).removeClass('proj-hover');
              });
}

//
//Animate on click
//
$('.ok').click(function(){
    if ( $(window).scrollTop() >= cheese  ) {

}
else{
    $("body, html").animate({ 
            scrollTop: $('.other').offset().top 
        }, 600);
     
}
});








});
*{
  box-sizing:border-box;
}
body,html{
  padding:0;
  margin:0;
}
.div{
  height:100vh;
  width:100%;
  background:#6464FF;
}
.other{
  height:1000px;
  width:100%;
  background:#FF6161;
}
.ok{
  position:absolute;
  bottom:0;
  left:50%;
  margin-left:-100px;
  width:200px;
  height:50px;
  line-height:50px;
  background:black;
  color:white;
  text-align:center;
  transition:1s;
}

.top{
  position:fixed;
  top:0;
  left:0;
  transition:.7s;
      margin-left: 0px;
      width:100%;
}
.proj-hover{
  background:white;
  color:black;
}
.blue{
  background:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">
  
  <div class="ok">Hey</div>
</div>
<div class="other"></div>

Answers 1

  • You need to check whether it is having top class or not. If yes then remove proj-hover class.

    Fiddle

    Your code will look like this :

    $(function(){
    
        var cheese= $('.ok').offset().top; //Define top of 'hey'
    
        //
        //Animates when it reaches red part of page
        //
        $(window).scroll(function() {
            if ( $(window).scrollTop() >= cheese  ) {
                $('.ok').addClass('top');
            } else{
                $('.ok').removeClass('top');
            }
        });
    
        //
        //This part doesn't rely on scroll event and is in charge of changing hover on ".ok" div.
        //
    
        $('.ok').hover(function(){
            if(!$(this).hasClass("top"))        **//here is change**
                $(this).addClass('proj-hover');
        },function(){
            $(this).removeClass('proj-hover');
        });
        //
        //Animate on click
        //
        $('.ok').click(function(){
            if ( $(window).scrollTop() >= cheese  ) {
        }
        else{
            $("body, html").animate({ 
                scrollTop: $('.other').offset().top 
            }, 600); 
        }
        });
    });
    

    Hope this helps!


Related Articles