【发布时间】:2015-05-24 01:23:32
【问题描述】:
我的“关于”页面上有一个小型“交互式幻灯片”,用户可以在其中单击 5 个按钮中的 1 个,然后查看生成的“幻灯片”。每个“幻灯片”都是一个<section> 元素,里面有不同的内容。当页面加载时,第一个按钮(“关于我”)应该是红色的。以下是其他要求:
- 当用户点击任何按钮时,它应该变为红色而所有其他按钮应该变为蓝色。
- 当用户“离开”按钮时,这个按钮必须保持红色
- 当用户将鼠标悬停在任何按钮上时,它应该变为红色,然后在用户离开时变回蓝色
- 除非此按钮是当前按钮,否则它必须保持红色
- 必须始终有 1 个红色按钮,因为这表示上一张幻灯片。
- 一次只能有 2 个按钮为红色(一个来自上一次点击,一个来自当前悬停)。
我的.click() 事件运行良好,但我似乎无法让.hover() 效果同时运行。当我运行这两个效果时,hover 效果会覆盖click 效果。一旦我的鼠标离开按钮点击它,背景就会变为dodgerblue,即使它应该保持indianred(我认为这是因为悬停的'mouseoff'部分事件)。
我看过类似的问题,但找不到能回答我当前问题的问题。
有什么建议吗?
var aryButtons = $('.button');
$(aryButtons[0]).css('background-color', 'indianred');
// IMPORTANT - If you comment this section out, the hover effect disappears, but the click function works. However, I need to have both working at once, so that when a button is clicked, it will stay indian red even when the user leaves the element. In addition, they need to be able to hover over other elements again.
$(aryButtons).hover(function () {
$(this).css('background-color', 'indianred');
}, function () {
$(this).css('background-color', 'dodgerblue');
});
$(aryButtons).click(function () {
$.each(aryButtons, function () {
$(this).css('background-color', 'dodgerblue');
});
$(this).css('background-color', 'indianred');
});
<section class="button"></section>
<section class="button"></section>
<section class="button"></section>
.button {
display: inline-block;
height: 50px;
width: 50px;
margin-right: 10px;
background-color: dodgerblue;
border: 1px solid black;
}
这是一个 jsFiddle:https://jsfiddle.net/UnaviaMedia/b3ozk00p/8/
【问题讨论】:
-
仅供参考,您应该始终在问题中包含相关代码,以防外部网站出现故障。
-
好的,有道理。感谢您的信息。以后我会记住的。
标签: jquery html css hover click