简体   繁体   English

如何将活动CSS块导航元素(跨度)设置为与使用Javascript和“活动”类不同的颜色?

[英]How to set active CSS block navigation element (span) to a different color to using Javascript and an “active” class?

This is giving me a major headache. 这让我头疼不已。 Here's my code: 这是我的代码:

This section of the page is laid out using a single .php file and some CSS + php to create 3 'panels' that display in a block element and navigate around as though they are 3 different pages, when really they're just different chunks of the page being moved. 页面的此部分使用单个.php文件和一些CSS + php进行布局,以创建显示在块元素中的3个“面板”,并像3个不同的页面一样浏览,而实际上它们只是不同的块页面的移动。 The navigation is 3 simple a href links with a span tag thrown in to contain the text. 导航是3个简单的href链接,其中包含一个span标记以包含文本。 The problem is that I need to change the color of that span text when that particular "tab" is active. 问题是,当该特定的“选项卡”处于活动状态时,我需要更改该跨度文本的颜色。 Note, I am not talking about the css function a:active or anything like that. 注意,我不是在说css函数a:active或类似的东西。 I need the text to alternate between 3 separate colors. 我需要文本在3种不同的颜色之间交替。 Color 1 for the no activity state: "product-reservation-btn-1" is this color unless > Color 2 is the :hover state, which is currently working, but finally Color 3 is the active state where "$css_step_2_tab" is the currently selected tab, the span text becomes Color 3 until another tab is selected. 颜色1表示无活动状态:除非>颜色2是:hover状态(当前正在工作),否则颜色为“ product-reservation-btn-1”是该颜色,但最终颜色3为活动状态,其中“ $ css_step_2_tab”为当前选择的选项卡,跨度文本将变为颜色3,直到选择了另一个选项卡。 Any ideas? 有任何想法吗? This is driving me nutty! 这让我发疯!

CSS: CSS:

#product-reservation-steps a span{
text-align:center;
color:#ffffff;
font-family: 'latoregular','arial','serif';
font-size:1.8em;
line-height:48px;
text-decoration:none;
color: #b6b7b7;
}

#product-reservation-steps a span:hover {
color: #f19c23;
}

#product-reservation-step-1-btn{
display:block;
position:absolute;
top:0;
left:5;
z-index:1;
width:300px;
height:48px;
text-align:center;
text-decoration:none;   
}

#product-reservation-step-1-btn.active{ 
font-style: oblique;    
}

#product-reservation-step-1-btn:hover{  
text-decoration:none;   
}

#product-reservation-step-2-btn{
display:block;  
position:absolute;
top:0;
left: 320px;
z-index: 2;
width: 278px;
height:48px;
text-align:center;
text-decoration:none;       
}

#product-reservation-step-2-btn.active{
font-style: oblique;    
}

#product-reservation-step-2-btn:hover{  
text-decoration:none;   
}

#product-reservation-step-3-btn{
display:block;  
position:absolute;
top:0;
left:573px;     
z-index:1;  
width:300px;
height:48px;
text-align:center;  
text-decoration:none;   
}

#product-reservation-step-3-btn.active{ 
font-style: oblique;    
}

#product-reservation-step-3-btn:hover{  
text-decoration:none;   
}

HTML: HTML:

<!-- begin steps -->
<?php
// default step to open on load
$css_step_1_tab = '';
$css_step_2_tab = '';
$css_step_3_tab = '';

$css_step_1_panel = 'style="display:none;"';
$css_step_2_panel = 'style="display:none;"';
$css_step_3_panel = 'style="display:none;"';

switch($_GET["step"]){
case 3:
$css_step_3_tab = 'class="active"';
$css_step_3_panel = 'style="display:block;"';
break;
case 2:
$css_step_2_tab = 'class="active"';
$css_step_2_panel = 'style="display:block;"';
break;
case 1:
default:
$css_step_1_tab = 'class="active"';
$css_step_1_panel = 'style="display:block;"';
break;
}
?>

<div id="product-reservation-steps">

<a href="javascript:picturebooth_step(1);" id="product-reservation-step-1-btn" <?php echo $css_step_1_tab; ?>><span>Customize your booth</span></a>
<div id="arrow-left"><img src="<?php echo get_template_directory_uri(); ?>/images/arrow-right.png" /></div>
<a href="javascript:picturebooth_step(2);" id="product-reservation-step-2-btn" <?php echo $css_step_2_tab; ?>><span>Billing & Shipping</span></a>
<div id="arrow-right"><img src="<?php echo get_template_directory_uri(); ?>/images/arrow-right.png" /></div>
<a href="javascript:picturebooth_step(3);" id="product-reservation-step-3-btn" <?php echo $css_step_3_tab; ?>><span>Confirmation</span></a>
<div style="clear:both;"></div>

</div>
<!-- end steps -->

Javascript: This is all I can give you without giving away the company name and breaking my nda. Javascript:这就是我所能给您的,而无需透露公司名称和破坏我的保密协议。 This chunk adds the 'active' class to the id "product-reservation-step-btn" depending on which button is active. 该块根据哪个按钮处于活动状态将“活动”类添加到id“ product-reservation-step-btn”中。 However, passing a simple css color: #8bcc3b; 但是,传递一个简单的CSS颜色:#8bcc3b; parameter to the element to change the text color. 元素的参数以更改文本颜色。 Nor does creating a #product-reservation-steps a span.active CSS chunk and passing it the color I want for the active state. 创建#product-reservation-step也不会跨过span.active CSS块并将其传递给我想要的活动状态颜色。

// reset tabs and divs
for(i=1 ; i<=total_steps ; i++){
$('#product-reservation-step-'+i).hide();

if($('#product-reservation-step-'+i+'-btn').hasClass("active")){
$('#product-reservation-step-'+i+'-btn').removeClass("active");
}
}
$('#product-reservation-step-'+step).fadeIn("fast");
$('#product-reservation-step-'+step+'-btn').addClass("active");

Any help, advice, or suggestions are deeply appreciated! 任何帮助,建议或建议,深表感谢!

I have been messing around for ages to help you, so here is my fiddle , the one i worked on before this (heavily edited), was created to improve your UI and compact your html/css. 我一直在忙着为您提供帮助,所以这里有我的小提琴 ,我在此之前(经过大量编辑)曾致力于改善您的UI并压缩html / css。

This is the new CSS, with some cool "prefix" selectors: 这是新的CSS,带有一些很酷的“前缀”选择器:

#product-reservation-steps a{
text-align:center;
font-family: 'latoregular','arial','serif';
font-size:1.8em;
line-height:48px;
text-decoration:none;
color: #b6b7b7;
}

#product-reservation-steps a:hover {
    color: #f19c23;
}

[id^="product-reservation-step-"] {
    /* could all this maybe go into '#product-reservation-steps a' ? */ 
    display:block;  
    position:absolute;
    top:0;
}


[id^="product-reservation-step-"].active{  
/* this is how you select prefixes: */
/* element[attribute^="first part of the attribute value"] */
    font-style: oblique;
    color:red;
}

[id^="product-reservation-step-"]:hover{  
    text-decoration:none;   
}

#product-reservation-step-1-btn{
    left:5;
    z-index:1;
    width:300px;  
}

#product-reservation-step-2-btn{
    left: 320px;
    z-index: 2;
    width: 278px;   
}

#product-reservation-step-3-btn{
    left:573px;     
    z-index:1;  
    width:300px; 
}

I edited the javascript, it may or may not help... 我编辑了JavaScript,它可能会或可能不会帮助...

// reset tabs and divs
var prodres = '#product-reservation-step-';
var btn = '-btn';
for (i = 1; i <= total_steps; i++)
{
//$("a[class|='product-reservation-step-']").click(function()
//{
$(prodres + i + btn).hide();
if ($(prodres + i).hasClass("active"))
{
    $(prodres + i + btn).removeClass("active");
}
else
{
    $(prodres + step).fadeIn("fast");
    $(prodres + step + btn).addClass("active");
    //}
}
}

Try validating your script on the dirtymarkup site 尝试在dirtymarkup站点上验证脚本

this: a[class|='product-reservation-step-'] is how you select objects by their prefix, thought it might help. 这:a [class | ='product-reservation-step-']是您通过对象前缀选择对象的方式,认为这样做可能会有所帮助。

Also turned the refrences into variables, which should make things easier. 还可以将引用转换为变量,这应该使事情变得容易。

Also, have you tried using :focus ? 另外,您是否尝试过使用:focus? This wont stay once the mouse is clicked somewhere else or the tab key is pressed though... 一旦单击鼠标在其他地方或按下Tab键,此操作就不会保留。

This is from W3Schools: 这是来自W3Schools:

input:focus
{
background-color:yellow;
}

<p>Click inside the text fields to see a yellow background:</p>

<form>
First name: <input type="text" name="firstname" /><br>
Last name: <input type="text" name="lastname" />
</form>

Although this may not help, I made something similar this a while ago: 尽管这可能无济于事,但前一阵子我做了类似的事情:

http://leeli.co.uk/bengoddard/demos/ovalistic/ http://leeli.co.uk/bengoddard/demos/ovalistic/

This is from http://jqueryui.com/tabs/ 这是从http://jqueryui.com/tabs/

    <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>
</head>
<body>
<div id="tabs">
<ul>
<li><a href="#tabs-1">Nunc tincidunt</a></li>
<li><a href="#tabs-2">Proin dolor</a></li>
<li><a href="#tabs-3">Aenean lacinia</a></li>
</ul>
<div id="tabs-1">
<p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
</div>
<div id="tabs-2">
<p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
</div>
<div id="tabs-3">
<p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
<p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
</div>
</div>
</body>
</html>

...May be your easiest solution... ...可能是您最简单的解决方案...

Back again! 再次回来! Try this... alot simpler than I thought it'd be! 试试这个...比我想象的要简单得多!

jQuery: jQuery的:

$('#menu li a').click(function(){
$('#menu li a').removeClass('selected'); // remove selected from any other item first
$(this).addClass('selected'); //add selected to the one just clicked.
});

CSS: CSS:

a {
    font-size: 24px;
    color: blue;
    font-family: 'Open Sans';
}

a:hover {
    font-weight: bolder;
    color: blue;
}

a:link {
    color: red;
}

a:visited {
    color: grey;
}

a:active {
    color: #90EE90;
    transition: color .03s .0s linear;
}

.selected {
    border: solid 1px #000;
    color: orange;
}

.selected span {
    color: #FF0;
}

http://jsfiddle.net/benji/9Qxdz/25/ http://jsfiddle.net/benji/xcbGh/8/ http://jsfiddle.net/benji/9Qxdz/25/ http://jsfiddle.net/benji/xcbGh/8/

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

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