在 JQUERY .hover 中转换 CSS { ul li:hover a }
我现在想知道是否有一种方法可以在 jquery .hover 中转换此 css 属性,或者通过 javascript 控制它以动态更改颜色。
CSS:
ul li:hover a {
color: #FFF;
}
有人可以帮忙吗?
编辑:
我的问题是:
我有一个下拉菜单,我希望当我将鼠标悬停在菜单上时,文本颜色会发生变化,当我将鼠标悬停在子菜单上时,两者的悬停状态都会保持不变。
JQuery:
$("ul li").hover(function () {
$(this).stop().animate({ backgroundColor: "white"}, 500);
}, function () {
$(this).stop().animate({ backgroundColor: "black"}, 400);
});
在菜单和子菜单中悬停时设置背景颜色动画。
例如,如果文本是黑色的,我想在悬停时将文本设置为白色。为此,我使用:(子菜单示例,当然,对于菜单更改选择器)
$('ul.submenu li a').hover(function () {
$(this).css({color:'#FFFFFF'});
}, function () {
$(this).css({color:'#00FF00'});
});
所有这些工作正常,但是当我悬停子菜单时,菜单返回到原始状态(因为 mouseleave 在悬停时被激活)。我想要的是,当我悬停子菜单时,菜单中的悬停状态也保持活动状态。
我尝试了很多方法,但都给我带来了问题,唯一有效的是 css,但我也需要动态控制文本颜色。
HTML 结构:
<ul class="menu">
<li><a href="#">text</a></li>
<li><a href="#">text</a>
<ul class="submenu">
<li><a href="#">text</a></li>
<li><a href="#">text</a></li>
<li><a href="#">text</a></li>
</ul>
</li>
<li><a href="#">text</a></li>
</ul>
I want to now if there's a way to transform this css properties in a jquery .hover, or control this by javascript to change the colour dynamically.
CSS:
ul li:hover a {
color: #FFF;
}
Can anyone Help ?
EDIT:
My problem is:
I have a drop down menu and i want that when I hover the menu the text color change and when I hover the submenu the hover state stays for both.
JQuery:
$("ul li").hover(function () {
$(this).stop().animate({ backgroundColor: "white"}, 500);
}, function () {
$(this).stop().animate({ backgroundColor: "black"}, 400);
});
To animate background color on hover in menu and submenu.
For example if the text are black I want to make the text white on hover. For this I use:(Submenu example, for menu change the selector of course)
$('ul.submenu li a').hover(function () {
$(this).css({color:'#FFFFFF'});
}, function () {
$(this).css({color:'#00FF00'});
});
All This works fine, but when I hover the submenu the menu returns to the original state(because the mouseleave is activated on hover out). All I want is that when I hover submenu the hover state in menu stays active as well.
I've tried many things but all give me problems, only thing that works is css, but I need to control the text colours dynamically too.
HTML Structure:
<ul class="menu">
<li><a href="#">text</a></li>
<li><a href="#">text</a>
<ul class="submenu">
<li><a href="#">text</a></li>
<li><a href="#">text</a></li>
<li><a href="#">text</a></li>
</ul>
</li>
<li><a href="#">text</a></li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请告诉我这是否符合您的要求。
我还没有完全正常工作,但看看它是否有帮助:
我的 JSFiddle
这是到目前为止的代码:
和 CSS:
注意:我并不是说这是最好的答案,也不是一个完整的解决方案,但也许这里的某些内容可以帮助某人找到正确的解决方案。
Please let me know if this is along the right track of what you're asking.
I haven't got it working fully, but give this a look and see if it helps:
my JSFiddle
Here's the code so far:
And CSS:
Note: I'm not saying this is the best answer, nor is it a complete solution, but maybe something in here will assist someone in finding the correct solution.
更新:
假设第一个选择器(
ul li a:hover
)是多余的,我们可以大大简化代码:这个更新的代码也应该可以工作(在您ANCHOR 元素的 style 属性内没有额外的 CSS 代码)。
更新:
替代解决方案如下:
使用此 CSS 代码:
我强烈推荐此替代解决方案!
Update:
Assuming that the first selector (
ul li a:hover
) is superfluous, we can simplify the code considerably:This updated code should work also (under the condition that you don't have additional CSS code inside the style attribute of the ANCHOR elements).
Update:
An alternative solution would be this:
with this CSS code:
I highly recommend this alternative solution!
试试这个(现已测试:http://jsfiddle.net/nathan/J7HLV/):
Try this (now tested: http://jsfiddle.net/nathan/J7HLV/):
当然,只需使用悬停绑定即可。
请注意,当您将鼠标移开时,此代码不会重置 CSS 属性。为此,您需要存储原始颜色值。
将
a
元素设置为display: block
可能是值得的,以便它扩展到整个父li
元素。然后您只需将鼠标悬停在其中之一上即可。Sure, simply use the hover binding.
Note that this code won't reset the CSS properties when you mouse out. To do that you would need to store the original color values.
It is probably worth it to set that
a
element todisplay: block
so it expands to the entire parentli
element. Then you only need to hover on one of them.为li标签绑定hover函数。每当 mouseover/mouseout 位于
标记上时,事件就会冒泡到
请参阅 工作演示
如果您可以使用 CSS 实现效果,那么为什么还要使用 javascript 解决方案。
Bind a hover function for the li tag. Whenever mouseover/mouseout is on the
<a>
tag the event will bubble up to the<li>
See a working demo
If you can achieve the effect using CSS then why go for javascript solution.