2017-02-24 73 views
0

我试图添加一个悬停功能,它将类添加到我的标题。我已经得到了这个代码,但在滚动上。Jquery头添加类上悬停

$(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(); 
     } 
    });  

有没有办法编辑这个现有的代码,所以它相同,但悬停?我试过了我能想到的一切,它不会工作。 在此先感谢。

+1

你试过['.hover()'](https://api.jquery.com/hover/)功能? – Satpal

+0

https://api.jquery.com/hover/ –

+0

尝试'$(“elemnt”)。hover()' – Nadim

回答

0

没有看到你所有的代码,很难告诉你究竟该做什么,但这是使用jQuery的$.hover()的一般想法。在悬停时应用该课程,并在悬停时选择性地移除该课程。

$('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>

+0

我已经试过这个...不工作。 – Geatrix

+0

@Geatrix然后你需要给我们一个工作演示,所以我们可以给你更准确的代码,将为你工作。否则,我们只是在猜测。 –

+0

我不能,因为我使用shopify和即时编辑主题,不活跃,只有我可以预览它。我应该向你发送header.liquid文件吗? – Geatrix