简体   繁体   English

jQuery在导航链接鼠标悬停时更改背景颜色

[英]Jquery Change background colour on Nav link mouseover

I want to change the color of background when I hover on different links in my nav menu. 当我将鼠标悬停在导航菜单中的不同链接上时,我想更改背景的颜色。 Say, link one.. the background goes to red, nav 2 the background goes to blue, nav 3 the background goes to green etc. But now, when i am on those pages, background to be the same as when the mouse hover over the links. 说,链接一..背景变为红色,导航2变为蓝色,导航3背景变为绿色等。但是现在,当我在这些页面上时,背景与鼠标悬停时的背景相同链接。 So page 1 background will be red, page 2 = blue, page 3 = green etc. 因此,第1页背景将为红色,第2页=蓝色,第3页=绿色等。

Store the background-color info for all the links in cookies and when you visit one of these pages, get all the cookies and compare their values for your current page and set the background of that color. 将所有链接的背景颜色信息存储在cookie中,当您访问这些页面之一时,获取所有cookie并比较它们在当前页面中的值并设置该颜色的背景。

For example when you hover over link1 set a cookie similar to this pair of string "link1color": "red" and so on. 例如,当您将鼠标悬停在link1上时,设置一个类似于该字符串对的cookie“ link1color”:“ red”,依此类推。 When you visit one of these pages get the cookies find the matching cookie for the page and get the background-color and update the background. 当您访问这些页面之一时,获取cookie,找到与页面匹配的cookie,获取background-color并更新背景。

You can do something like this. 你可以做这样的事情。

<a href="#" class="change-bg-color" data-bg-color="#ff0000">Link 1</a>
<a href="#" class="change-bg-color" data-bg-color="#00ff00">Link 2</a>
<a href="#" class="change-bg-color" data-bg-color="#0000ff">Link 3</a>

<script>
    $('a.change-bg-color').hover(function(){
        // Change you color
        $(body).css('background-color', this.getAttribute('data-bg-color') );
    },function(){
        // Back to the original color
        $(body).css('background-color', '#fff' ); // Supposing that #fff is your default bg color
    });
</script>

And if you want the "Link 1", "Link 2" and "Link 3" pages to have a different background color, the best way to do it should be to have a page-specific CSS class. 而且,如果您希望“链接1”,“链接2”和“链接3”页面的背景颜色不同,最好的方法应该是使用特定于页面的CSS类。 It doesn't require javascript. 它不需要javascript。

将您的背景色存储在数组中,并根据当前页面上的需要在js / jquery的帮助下从数组值中相应地更改值,因为如果数组是全局定义的,则您可以在应用程序中的任何位置进行快速访问而访问率低记忆。

Approach : 方法:

  1. Have three different classes in your CSS - say bkBlue, bkRed, bkGreen CSS中有三个不同的类-说bkBlue,bkRed,bkGreen

    .bkBlue { background-color : 'blue' } .bkBlue {background-color:'blue'}

  2. Using on hover event, change the class applied to the link using addClass and removeClass. 使用悬停事件,使用addClass和removeClass更改应用于链接的类。

  3. Store the current class value in a session variable. 将当前类的值存储在会话变量中。

  4. add the class from the session variable to the current page. 将会话变量中的类添加到当前页面。

And one another method is keep different hover classes with different id/class, if nav is not in large numbers and attach that classes with your particular nav hover. 另一种方法是,如果nav数量不多,则使用不同的id / class保留不同的悬停类,并使用您的特定nav悬停附加该类。 I hope you understand because this is the easiest way. 希望您能理解,因为这是最简单的方法。 Gud luck 上帝的运气

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

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