0

I´m trying to add a hover function which adds a class to my header. I already got this code which does the same but on scroll.

  $(document).ready(function(){
  jQuery(window).trigger('resize').trigger('scroll');

    $(window).scroll(function() {
      var scroll = $(window).scrollTop();
      if (scroll >= 1 ) {
        header.removeClass('scroll').addClass("cover");
        $('#phantom').show();
        $('#phantom').css('height', Heightcalculate+'px');
             } else {
        header.removeClass("cover").addClass('scroll');
        $('#phantom').hide();
      }
    });     

Is there a way to edit this existing code so it does the same but on hover? i tried everything i could think of and it wouldn´t work. Thanks in advance.

3
  • 1
    Have you tried .hover() function? Commented Feb 24, 2017 at 20:33
  • api.jquery.com/hover Commented Feb 24, 2017 at 20:34
  • Try $("elemnt").hover() Commented Feb 24, 2017 at 20:35

1 Answer 1

0

Without seeing all of your code, it's hard to tell you exactly what to do, but this is the general idea using jQuery's $.hover(). Apply the class on hover, and optionally remove the class when you hover off.

$('header').hover(function() {
  $(this).addClass('class');
}, function() {
  $(this).removeClass('class');
});
.class {
  background: black;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>header</header>

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

3 Comments

i already tried this... doesn´t work.
@Geatrix then you need to give us a working demo so we can give you more accurate code that will work for you. Otherwise we're just taking guesses.
I can´t, because i´m using shopify and im editing a theme that isn´t active and only i can preview it. should i send you the header.liquid file?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.