简体   繁体   English

使用鼠标悬停和鼠标移出更改背景颜色

[英]Changing the background colour with mouseover and mouseout

I am messing around with this scroll menu and I want each li to change to different colours instead of the same one. 我搞砸了这个滚动菜单 ,我希望每个li更改为不同的颜色,而不是相同的颜色。

var colorOver = '#31b8da';
var colorOut = '#1f1f1f'; 

But this changes the colour of all of them. 但这改变了所有颜色。

The html looks like this: html看起来像这样:

<div id="sidebar">
    <ul id="menu">
        <li id="first"><a href="#">blog <span> / 2012</span></a></li>
        <li id="second"><a href="#">me <span> / 2012</span></a></li>
        <li id="third"><a href="#">etc <span> / 2012</span></a></li>
        <li id="fourth"><a href="#">etc <span> / 2012</span></a></li>
        </ul>
    </div>

I assume you just tell it an id ... 我想你只是告诉它一个id ...

Hopefully I've given enough info. 希望我已经提供了足够的信息。

Thanks for any help. 谢谢你的帮助。

link to the demo and download 链接到演示并下载

you could modify your css to set the colorOver and colorOut classes for each li like: 您可以修改CSS来为每个li设置colorOver和colorOut类,例如:

.first.colorOver { background-color: #31b8da; } 
.first.colorOut { background-color: #1f1f1f; }

and use Francois Wahl's toggleClass option: 并使用Francois Wahl的toggleClass选项:

 $("#sidebar ul#menu li").on("hover", function(){
     $(this).toggleClass("colorOver", "colorOut");
 });

then you can set colors for each li easily. 那么您可以轻松地为每个li设置颜色。

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

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