jQuery 手风琴 - 活动部分的不同图像
我正在使用 Ryan Stemkoski 的“Stupid Simple Jquery Accordion Menu”,可在此处找到:
stemkoski.com/stupid-simple-jquery-accordion-menu/
这是 javascript
$(document).ready(function() {
//ACCORDION BUTTON ACTION (ON CLICK DO THE FOLLOWING)
$('.accordionButton').click(function() {
//REMOVE THE ON CLASS FROM ALL BUTTONS
$('.accordionButton').removeClass('on');
//NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
$('.accordionContent').slideUp('normal');
//IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
if($(this).next().is(':hidden') == true) {
//ADD THE ON CLASS TO THE BUTTON
$(this).addClass('on');
//OPEN THE SLIDE
$(this).next().slideDown('normal');
}
});
/*** REMOVE IF MOUSEOVER IS NOT REQUIRED ***/
//ADDS THE .OVER CLASS FROM THE STYLESHEET ON MOUSEOVER
$('.accordionButton').mouseover(function() {
$(this).addClass('over');
//ON MOUSEOUT REMOVE THE OVER CLASS
}).mouseout(function() {
$(this).removeClass('over');
});
/*** END REMOVE IF MOUSEOVER IS NOT REQUIRED ***/
/********************************************************************************************************************
CLOSES ALL S ON PAGE LOAD
********************************************************************************************************************/
$('.accordionContent').hide();
});
和 CSS
#wrapper {
width: 800px;
margin-left: auto;
margin-right: auto;
}
.accordionButton {
width: 800px;
float: left;
_float: none; /* Float works in all browsers but IE6 */
background: #003366;
border-bottom: 1px solid #FFFFFF;
cursor: pointer;
}
.accordionContent {
width: 800px;
float: left;
_float: none; /* Float works in all browsers but IE6 */
background: #95B1CE;
}
/***********************************************************************************************************************
EXTRA STYLES ADDED FOR MOUSEOVER / ACTIVE EVENTS
************************************************************************************************************************/
.on {
background: #990000;
}
.over {
background: #CCCCCC;
}
有一个“on”类,它允许样式当 AccordionButton 类处于活动状态时,我希望能够让每个活动的 AccordionButton 类具有不同的图像。
例如,在上述网站中,单词“WORK”应该是选择工作部分时会出现较深的灰色图像,因此在选择工作部分时应该进行协作等。
我不知道如何做到这一点,任何帮助将不胜感激。
谢谢, 安德鲁
I'm using Ryan Stemkoski's "Stupid Simple Jquery Accordion Menu" which is available here:
stemkoski.com/stupid-simple-jquery-accordion-menu/
Here is the javascript
$(document).ready(function() {
//ACCORDION BUTTON ACTION (ON CLICK DO THE FOLLOWING)
$('.accordionButton').click(function() {
//REMOVE THE ON CLASS FROM ALL BUTTONS
$('.accordionButton').removeClass('on');
//NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
$('.accordionContent').slideUp('normal');
//IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
if($(this).next().is(':hidden') == true) {
//ADD THE ON CLASS TO THE BUTTON
$(this).addClass('on');
//OPEN THE SLIDE
$(this).next().slideDown('normal');
}
});
/*** REMOVE IF MOUSEOVER IS NOT REQUIRED ***/
//ADDS THE .OVER CLASS FROM THE STYLESHEET ON MOUSEOVER
$('.accordionButton').mouseover(function() {
$(this).addClass('over');
//ON MOUSEOUT REMOVE THE OVER CLASS
}).mouseout(function() {
$(this).removeClass('over');
});
/*** END REMOVE IF MOUSEOVER IS NOT REQUIRED ***/
/********************************************************************************************************************
CLOSES ALL S ON PAGE LOAD
********************************************************************************************************************/
$('.accordionContent').hide();
});
and the CSS
#wrapper {
width: 800px;
margin-left: auto;
margin-right: auto;
}
.accordionButton {
width: 800px;
float: left;
_float: none; /* Float works in all browsers but IE6 */
background: #003366;
border-bottom: 1px solid #FFFFFF;
cursor: pointer;
}
.accordionContent {
width: 800px;
float: left;
_float: none; /* Float works in all browsers but IE6 */
background: #95B1CE;
}
/***********************************************************************************************************************
EXTRA STYLES ADDED FOR MOUSEOVER / ACTIVE EVENTS
************************************************************************************************************************/
.on {
background: #990000;
}
.over {
background: #CCCCCC;
}
There is an "on" class which allows the style of the accordionButton class when it is active but I would like to be able to have each active accordionButton class have a different image.
For example, in the above site the word "WORK" should be a darker grey image when the work section is selected, so should COLLAB when it is selected etc.
I can't figure out how to do this, any help would be greatly appreciated.
Thanks,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重述问题
好吧,这需要一些努力,但我想我理解你的问题。以下是事实(如果我错了,请纠正我):
.accordionButton img
)。div
(.accordionButton
) 时,您希望这些图像变得更暗。div
(使其成为.accordionButton.on
)。background
更改背景颜色。img
标签进行硬编码,而不是使用CSS作为background-image
。简而言之,这个问题与 Accordion 插件无关,而与
.accordionButton
的 CSS 和 HTML 有关(当它正常且处于“打开”状态时)。第一步
您需要熟悉的第一件事是 3-D CSS 盒模型 (原始和交互式可视化)。
这样做的主要要点是,实际上您无法通过 CSS
background
属性影响图像的背景颜色。正如您所声明的那样,浏览器将尝试在图像后面绘制画布(div
),并且由于您的图像不透明,因此图像的背景颜色将保持不变。解决方案
最简单的答案(可能也是您正在寻找的答案)就是使用当前图像,但去掉背景颜色。这将允许 CSS 按照您想要的方式绘制背景。请记住,图像中的部分(在本例中为字母)无法仅通过 CSS 进行更改。
如果这对您不起作用,这里还有一些其他选项:
background-image
CSS 规则。img
标记中的src
属性,或类似的技术。Restating the problem
Okay, it took some doing, but I think I understand your problem. Here are the facts (correct me if I'm wrong):
.accordionButton img
).div
(.accordionButton
).div
(making it.accordionButton.on
).background
.img
tag instead of with CSS as abackground-image
.The problem, in a nutshell, doesn't have to do with the accordion plugin, but with your CSS and HTML for
.accordionButton
when it's normal and when it's 'on'.First step
The first thing you need to become familiar with is the 3-D CSS box model (original and interactive visualizations).
The main takeaway from this is that you cannot, actually, affect the background color of the image by means of the CSS
background
property. As you've declared it, the browser will try to paint the canvas (div
) behind the image, and since your image is not transparent, the background color of the image will stay the same.Solutions
The simplest answer, and probably the one you're looking for, is just to use your current image(s), but take out the background color. This will allow CSS to paint the background like you want. Just remember that the part that's in the image (in this case, the letters) won't be able to be changed by CSS alone.
Here are some other options, if that won't work for you:
background-image
CSS rule for when the header is 'on'.src
attribute in yourimg
tag, or a similar technique.