【问题标题】:How to change the color of the timeline circle when it is an active stage?处于活动阶段时如何更改时间线圆圈的颜色?
【发布时间】:2021-10-31 10:58:36
【问题描述】:

我创建了一个垂直时间线。现在我必须在活动阶段设置圆圈的背景颜色。您可以检查下图第一个带有文本的圆圈是活动阶段,背景颜色是红色。

例子:

我有四个称为form1,form2,form3,form4 的表单。当页面重新加载时,第一个圆圈总是在红色背景中。如果用户在form1 上,则带有文本的圆圈的背景颜色为红色。单击按钮 1 后,第二个圆圈为红色背景,第一个圆圈为绿色。如果用户点击form2,则第二个圆圈为绿色,第三个圆圈为红色。如果用户点击了第三个按钮3,则背景的第三个圆圈为绿色,四个圆圈为红色。

我尝试了一些代码,只有第一个圆圈可以正常工作,如果我从 form1 点击 Button1,那么所有圆圈都显示为红色。

我的脚本有问题。请检查一下。并且还要在点击事件中检查我的按钮名称,因为我为每个按钮设置了相同的名称。

我在这里更新我的代码。现在的问题是如果该字段是空白的,即使我收到验证错误消息,它也会显示绿色圆圈。https://jsfiddle.net/Narendra2015/g2j1rtzn/

你能帮我解决这个问题吗?

$(document).ready(function(){
  $('.button-clicked').click(function(){

    $('.info-timeline ul li span').removeClass("timeline-circle-active");
    $('.info-timeline ul li a').removeClass("timeline-text-active");

    $('.info-timeline ul li span').addClass("timeline-circle-active");
    $('.info-timeline ul li a').addClass("timeline-text-active");
});
});

$(document).ready(function() {
    $("form[name='form1']").validate({
        rules: {
            fname: {
                required: true,
                 minlength:3,
                maxlength:50
            }
        },
         submitHandler: function() {
         //form.submit();
          $.ajax({
            type: 'post',
            url: 'process.php',
            data: $("form[name='form1']").serialize(),
           success: function (data) {
           //alert(data);
           $('#first').hide();
           $('#second').show();
            }
          });
        }
    })
});

$(document).ready(function() {
    $("form[name='form2']").validate({
        rules: {
            mname: {
                required: true,
                 minlength:3,
                maxlength:50
            }
        },
         submitHandler: function() {
         //form.submit();
          $.ajax({
            type: 'post',
            url: 'process.php',
            data: $("form[name='form2']").serialize(),
           success: function (data) {
           //alert(data);
           $('#second').hide();
           $('#third').show();
            }
          });
        }
    })
});

$(document).ready(function() {
    $("form[name='form3']").validate({
        rules: {
            age: {
                required: true,
                 minlength:3,
                maxlength:50
            }
        },
         submitHandler: function() {
         //form.submit();
          $.ajax({
            type: 'post',
            url: 'process.php',
            data: $("form[name='form4']").serialize(),
           success: function (data) {
           //alert(data);
           $('#third').hide();
           $('#four').show();
            }
          });
        }
    })
});
.info-timeline ul{list-style: none;margin: 0;padding: 0;}
.info-timeline ul li{margin:0 10px;}
.info-timeline ul li span{
  position: relative;
  border: 2px solid #000;
  border-radius: 100%; 
  width: 45px;
  line-height: 40px;
  text-align: center;
  margin-top: 30px;
  color: #000;
  z-index: 2;
  display: inline-block;
}

.info-timeline ul li span.timeline-circle-active{
  background-color: #ff0000;
  color: #000;
  border: 1px solid #ffff00 !important;
}
.info-timeline ul li a.timeline-text-active{
	color: #ff0000 !important;
}

.info-timeline ul li:not(:first-of-type) span:before {
  position: absolute; 
  border: 1px solid #000;
  width: 0; 
  height: 30px; 
  display: block;
  content: ''; 
  left: 50%; 
  z-index: 1; 
  top: -32px; 
  margin-left: -1px;
  }

.info-timeline ul li:first-child {margin-top: 0;}
.info-timeline ul li:first-child:before {display: none;}
.info-timeline ul li a{color: #000;margin: 10px;}

#second, #third, #four{
	display: none;

}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>

<div class="info-timeline">
		<ul>
			<li><span class="timeline-circle-active">1</span><a href="#" class="timeline-text-active">Button1</a></li>
			<li><span>2</span><a href="#">Button2</a></li>
			<li><span>3</span><a href="#">Button3</a></li>
			<li><span>4</span><a href="#">Button4</a></li>
		</ul>
	</div><!--info-timeline-->

<div id="first">
	<form method="post" action="" name="form1">
		<input type="text" name="fname" placeholder="first name">
		<button type="submit" class="button-clicked">Button1</button>
	</form>
</div>

<div id="second">
	<form method="post" action="" name="form2">
		<input type="text" name="mname" placeholder="middle name">
		<button type="submit" class="button-clicked">Button2</button>
	</form>
</div>

<div id="third">
	<form method="post" action="" name="form3">
		<input type="text" name="lname" placeholder="last name">
		<button type="submit" class="button-clicked">Button3</button>
	</form>
</div>

<div id="four">
	<form method="post" action="" name="form4">
		<input type="text" name="age" placeholder="age">
		<button type="submit" class="button-clicked">Submit</button>
	</form>
</div>

【问题讨论】:

  • 这里:$('.info-timeline ul li span').addClass("timeline-circle-active");you 添加 timeline-circle-active 类到 .info-timeline 中 li 中的每个跨度。所以,每个圆圈都会变成红色。
  • 是的,而不是当它处于活动状态时如何改变圆的颜色
  • 您可以在选定的圈子中使用名为“selected”的类。使用next() 选择下一个或索引您的项目以调用它们。

标签: javascript jquery html css


【解决方案1】:

一个工作示例:

(因为投稿的关系,怕是你自己试试,不过对我有用)

注意:

  • 我在 li 中添加了一个 ID 属性(circle-1、circle-2 等)
  • 表单使用 GET 方法,而不是 POST(必须在 URL 中 next_index

简介:

当您提交表单时,属性next_index(圆圈)会随表单一起发送。由于这个属性,我们知道必须选择哪个 LI。

不过,应该存在更智能的解决方案(例如 sessionStorage)。但这一款适合需要。

  $(document).ready(function(){
    
    // The next circle index  (1-start))
    let curr_index = getQueryParam('next_index') ;
    if (curr_index == 'next_index'){ curr_index = 1 }

    /* Here the condition on validation
       
      if (validation is not ok due to x reasons) 
      {
        curr_index -- ; // => stay at current step
      }

    */ 
      
    $('li#circle-'+curr_index).find('span').addClass("timeline-circle-active");
    $('li#circle-'+curr_index).find('a').addClass("timeline-text-active");
  });

  //To get a param in the querystring
  function getQueryParam(param) {
      location.search.substr(1)
          .split("&")
          .some(function(item) { // returns first occurence and stops
              return item.split("=")[0] == param && (param = item.split("=")[1])
          })
      return param
  }
     
      .info-timeline ul{list-style: none;margin: 0;padding: 0;}
      .info-timeline ul li{margin:0 10px;}
      .info-timeline ul li span {
        position: relative;
        border: 2px solid #000;
        border-radius: 100%; 
        width: 45px;
        line-height: 40px;
        text-align: center;
        margin-top: 30px;
        color: #000;
        z-index: 2;
        display: inline-block;
      }

      .info-timeline ul li span.timeline-circle-active{
        background-color: #ff0000;
        color: #000;
        border: 1px solid #ffff00 !important;
      }
      .info-timeline ul li a.timeline-text-active{
      	color: #ff0000 !important;
      }

      .info-timeline ul li:not(:first-of-type) span:before {
        position: absolute; 
        border: 1px solid #000;
        width: 0; 
        height: 30px; 
        display: block;
        content: ''; 
        left: 50%; 
        z-index: 1; 
        top: -32px; 
        margin-left: -1px;
        }

      .info-timeline ul li:first-child {margin-top: 0;}
      .info-timeline ul li:first-child:before {display: none;}
      .info-timeline ul li a{color: #000;margin: 10px;}

      #second, #third, #four{
      	display: none;

      }
      
   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="info-timeline">
		<ul>
			<li id="circle-1"><span>1</span><a href="#">Button1</a></li>
			<li id="circle-2"><span>2</span><a href="#">Button2</a></li>
			<li id="circle-3"><span>3</span><a href="#">Button3</a></li>
			<li id="circle-4"><span>4</span><a href="#">Button4</a></li>
		</ul>
	</div><!--info-timeline-->

<div id="first">
	<form method="get" action="" name="form1">
		<input type="text" name="fname" placeholder="first name">
    <input type="hidden" name="next_index" value="2" />
		<button type="submit" class="button-clicked">Button1</button>
	</form>
</div>

<div id="second">
	<form method="get" action="" name="form2">
		<input type="text" name="mname" placeholder="middle name">
    <input type="hidden" name="next_index" value="3" />
		<button type="submit" class="button-clicked">Button2</button>
	</form>
</div>

<div id="third">
	<form method="get" action="" name="form3">
		<input type="text" name="lname" placeholder="last name">
    <input type="hidden" name="next_index" value="4" />
		<button type="submit" class="button-clicked">Button3</button>
	</form>
</div>

<div id="fourth">
	<form method="get" action="" name="form4">
		<input type="text" name="age" placeholder="age">
		<button type="submit" class="button-clicked">Submit</button>
	</form>
</div>

【讨论】:

  • 我知道,您可以通过 sn-p 提交表单。但表格是你的问题。正如我所说,您自己尝试我的代码(复制 sn-p 以回答,然后将其复制粘贴到您的编辑器中)。注意:使用我的 HTML、CSS 和 JS 代码,不要使用你的。
  • 我在下面的链接中尝试了一些建议,Mihai T。请查看下面的链接jsfiddle.net/Narendra2015/g2j1rtzn
  • 现在的问题是当我点击按钮而不填写字段时,它显示验证,但它也会从第一个圆圈跳到第二个圆圈。我需要当用户填写该字段而不是从第一个圆圈跳到第二个圆圈
  • 为什么是占位符?我不是在谈论占位符。
  • 我在 if 条件中添加了我的 jquery 验证代码
【解决方案2】:

我简化了你的代码(删除了不是问题的代码)。

HTML

我添加了按钮和跨度之间的关系。使用data-target 跨度ID。

JQ

操作评论后编辑

首先,你得到目标span id(当点击button2时,id为#button2的span会被选中)

其次,获取当前选中的一个之前的span(如果有的话) .点击 button2 时的示例,prevSelected 的值为 #button1

然后在跨度和链接中添加和删除类

查看下面的编辑代码

CSS

greenSpan 类添加样式

观察

  1. 无需添加多个$(document).ready(function(){。将所有代码嵌套在 1 个函数中
  2. 您可以将该活动类提供给包含spanali,而不是将活动类提供给span 和活动类,然后只在css 中设置样式,例如li.active &gt; span {/*timeline-circle-active css*/} 和@987654329 @

参见下面的 sn-p。

$(document).ready(function() {

  $('.button-clicked').click(function() {

    var TargetSpan = "#" + $(this).attr("data-target"),
        prevSelected = $(TargetSpan).parents("li").prev("li").find("span")
      
    prevSelected.addClass("greenSpan").removeClass("timeline-circle-active")
    prevSelected.next("a").addClass("greenLink").removeClass("timeline-text-active")
    $(TargetSpan).addClass("timeline-circle-active").removeClass("greenSpan")
    $(TargetSpan).next("a").addClass("timeline-text-active")


  });
});
.info-timeline ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.info-timeline ul li {
  margin: 0 10px;
}

.info-timeline ul li span {
  position: relative;
  border: 2px solid #000;
  border-radius: 100%;
  width: 45px;
  line-height: 40px;
  text-align: center;
  margin-top: 30px;
  color: #000;
  z-index: 2;
  display: inline-block;
}

.info-timeline ul li span.timeline-circle-active {
  background-color: #ff0000;
  color: #000;
  border: 1px solid #ffff00 !important;
}

.info-timeline ul li a.timeline-text-active {
  color: #ff0000 !important;
}

.info-timeline ul li a.greenLink {
  color: green
}

.info-timeline ul li:not(:first-of-type) span:before {
  position: absolute;
  border: 1px solid #000;
  width: 0;
  height: 30px;
  display: block;
  content: '';
  left: 50%;
  z-index: 1;
  top: -32px;
  margin-left: -1px;
}

.info-timeline ul li:first-child {
  margin-top: 0;
}

.info-timeline ul li:first-child:before {
  display: none;
}

.info-timeline ul li a {
  color: #000;
  margin: 10px;
}

.info-timeline ul li span.greenSpan {
  background: green
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="info-timeline">
  <ul>
    <li><span id="button1" class=" timeline-circle-active">1</span><a href="#" class="timeline-text-active">Button1</a></li>
    <li><span id="button2">2</span><a href="#">Button2</a></li>
    <li><span id="button3">3</span><a href="#">Button3</a></li>
    <li><span id="button4">4</span><a href="#">Button4</a></li>
  </ul>
</div>
<!--info-timeline-->

<button type="submit" class="button-clicked" data-target="button1">Button1</button>
<button type="submit" class="button-clicked" data-target="button2">Button2</button>
<button type="submit" class="button-clicked" data-target="button3">Button3</button>
<button type="submit" class="button-clicked" data-target="button4">Button4</button>

【讨论】:

  • 是的,用户必须填写订单(1字段,2字段,3字段等)。
  • @NarendraVerma 查看编辑后的代码和解释。请记住,这仅在用户填写订单时才有效
  • 是的,当然给我一些时间来理解逻辑。因为我必须申请我的表格
  • 不幸的是,此解决方案不适用于提交的表单@MihaiT。见我的。
  • 您从问题中删除了复杂的部分。将您的提交按钮放在表单中,提交表单,它不起作用。如果您看一下我的解决方案,我相信您会理解。
猜你喜欢
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-27
  • 2016-04-15
相关资源
最近更新 更多