简体   繁体   English

使用 Jquery 悬停事件更改 css

[英]Using Jquery hover event to change css

So, I'm trying to use bootstraps "active" class, but when I hover over a different navbar link, I want the "active" to take the look of the other navbar link that are not being hovered over.因此,我正在尝试使用引导程序“活动”类,但是当我将鼠标悬停在不同的导航栏链接上时,我希望“活动”能够查看未悬停在其上的其他导航栏链接。

Here is my relevant HTML:这是我的相关 HTML:

<div id="navbar" class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a id="active" href="#">TOWER DUEL</a></li>
                        <li><a class="otherNavs" href="#thecrew">THE CREW</a></li>
                        <li><a class="otherNavs" href="./presskit/">PRESSKIT</a></li>
                    </ul>
      </div>

And here is my Relavent JQuery这是我的 Relavent JQuery

    $( ".otherNavs" ).hover(function() {
            $("#active" ).css("color", "#68ddff !important");
                $("#active" ).css("color", "#68ddff !important");
                $("#active").css("background", "#0f2436")
                $("#active").css("background", "-webkit-linear-gradient(#0f2436, #14335c)")
                $("#active").css("background", "-o-linear-gradient(#0f2436, #14335c)")
                $("#active").css("background", "-moz-linear-gradient(#0f2436, #14335c)")
                $("#active").css("background", "linear-gradient(#0f2436, #14335c)")
            });

As you may have noticed by now... There is a curveball.正如您现在可能已经注意到的那样......有一个曲线球。 My background is gradient, so I had to use all that browser support bullshit, lol.我的背景是渐变的,所以我不得不使用所有浏览器支持的废话,哈哈。

Im to the point where im like positive this code should work, but its not.我到了我喜欢这个代码应该工作的地步,但它不是。

Also, I have confirmed my jquery library is functional, I'm using it in another parts of my code.另外,我已经确认我的 jquery 库是可用的,我在代码的其他部分使用它。

I do realize I have no support to "Pop it back in place" when I'm done hovering, but that's the easy part once I get the initial code working.我确实意识到当我完成悬停时,我不支持“将其弹回原位”,但是一旦我使初始代码正常工作,这就是简单的部分。

EDIT:编辑:

So I was approached with a much easier method, simply removing the class onhover and adding it back after:所以我找到了一个更简单的方法,只需删除类 onhover 并在之后添加它:

<div id="navbar" class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li id="activePage" class="active"><a href="#">TOWER DUEL</a></li>
                        <li><a class="otherNavs" href="#thecrew">THE CREW</a></li>
                        <li><a class="otherNavs" href="./presskit/">PRESSKIT</a></li>
                    </ul>
      </div>

And my new jquery:还有我的新 jquery:

        $(".otherNavs").hover( function () {
            $("#activePage").removeclass("active");
        }, function () {
            $("#activePage").addclass("active");
        });

However, this code is still unfunctional.但是,此代码仍然无法正常工作。 Any ideas?有任何想法吗?

Thanks谢谢

I think you should use addClass() when the mouse hovers over the element, and use removeClass() when the mouse stops hovering, instead of css() , like this:我认为您应该在鼠标悬停在元素上时使用addClass() ,并在鼠标停止悬停时使用removeClass()而不是css() ,如下所示:

$(".otherNavs").hover( function () {
    $(".active").addclass("myclass");
}, function () {
    $(".active").removeclass("myclass");
});

And then add this to your CSS :然后将其添加到您的CSS

.myclass {
    color: #68ddff !important;
    background: #0f2436;
    background: -webkit-linear-gradient(#0f2436, #14335c);
    background:    -moz-linear-gradient(#0f2436, #14335c);
    background:      -o-linear-gradient(#0f2436, #14335c);
    background:         linear-gradient(#0f2436, #14335c);
}

UPDATE:更新:

For your new markup use this:对于您的新标记,请使用:

$(document).ready(function() {
  $(".otherNavs").hover(function() {
    $("#activePage").removeClass("active");
  }, function() {
    $("#activePage").addClass("active");
  });
});

your code should work if you put your <script> before the closing </body> tag.如果您将<script>放在结束</body>标记之前,您的代码应该可以工作。

Pure CSS:纯 CSS:

 #navbar:hover .active:not(:hover) { color: #68ddff !important; background: "#0f2436"; background: -webkit-linear-gradient(#0f2436, #14335c); background: -o-linear-gradient(#0f2436, #14335c); background: -moz-linear-gradient(#0f2436, #14335c); background: linear-gradient(#0f2436, #14335c);
 <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li class="active"><a id="active" href="#">TOWER DUEL</a></li> <li><a class="otherNavs" href="#thecrew">THE CREW</a></li> <li><a class="otherNavs" href="./presskit/">PRESSKIT</a></li> </ul> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM