首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >javascript动态更改背景

javascript动态更改背景
EN

Stack Overflow用户
提问于 2016-11-03 00:32:03
回答 2查看 111关注 0票数 1

我把这个进度条修好了。我唯一有问题的是在选中许多复选框时更改颜色.

代码语言:javascript
运行
复制
0-3 background = red
4-7 = yellow
8-10 = Green

我的CSS

代码语言:javascript
运行
复制
.progress-bar-wrapper .progress-bar-filling {
height: 100%;
color: #fff;
text-align: right;
width: 0;
background-color: #39b54a;
border-radius:20px;
}

.progress-bar-wrapper .progress-bar-percent {
font-weight: 400;
font-family: sans-serif;
color: #626162;
padding-top: 6px;
display: block;
}

我的HTML / Javascript

代码语言:javascript
运行
复制
<div id="yyy">
<input type="checkbox" class="check_id" onclick="xxx()" checked>
<input type="checkbox" class="check_id" onclick="xxx()">
<input type="checkbox" class="check_id" onclick="xxx()">
<input type="checkbox" class="check_id" onclick="xxx()" checked>
<input type="checkbox" class="check_id" onclick="xxx()">
<input type="checkbox" class="check_id" onclick="xxx()" checked>
<input type="checkbox" class="check_id" onclick="xxx()">
<input type="checkbox" class="check_id" onclick="xxx()">
<input type="checkbox" class="check_id" onclick="xxx()" checked>
<input type="checkbox" class="check_id" onclick="xxx()">
<div>
<script>

window.onload = xxx;


function xxx()
{
var zzz = $(".check_id:checked").length;
document.getElementById("insert").innerHTML = zzz


$(document).ready(function(){
function progress(percent, $element) {
var progressBarWidth = percent * $element.width() / 100;
$element.find('.progress-bar-filling').animate({ width: progressBarWidth }, 300)

jFiddle在这里,提琴曲,谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-03 00:42:15

你可以直接查一下没有。通过$(".check_id:checked").length选中复选框,并仅更改background-color css属性。

代码语言:javascript
运行
复制
<div id="yyy">
<input type="checkbox" class="check_id" onclick="xxx()" checked>
<input type="checkbox" class="check_id" onclick="xxx()">
<input type="checkbox" class="check_id" onclick="xxx()">
<input type="checkbox" class="check_id" onclick="xxx()" checked>
<input type="checkbox" class="check_id" onclick="xxx()">
<input type="checkbox" class="check_id" onclick="xxx()" checked>
<input type="checkbox" class="check_id" onclick="xxx()">
<input type="checkbox" class="check_id" onclick="xxx()">
<input type="checkbox" class="check_id" onclick="xxx()" checked>
<input type="checkbox" class="check_id" onclick="xxx()">
<div>
<script>

window.onload = xxx;


function xxx()
{
  var zzz = $(".check_id:checked").length;
  document.getElementById("insert").innerHTML = zzz


    $(document).ready(function(){
    function progress(percent, $element) {
      var progressBarWidth = percent * $element.width() / 100;
      $element.find('.progress-bar-filling').animate({ width: progressBarWidth }, 300)
      //.html(percent + "%&nbsp;");

      $('.progress-bar-percent').html( percent + "%&nbsp;" );

      // calculate color
      if($(".check_id:checked").length <= 3)
      {
         $("#progress-bar-filling").css("background-color","red");
      }
      else if($(".check_id:checked").length >= 4 && $(".check_id:checked").length <= 7 )
      {
          $("#progress-bar-filling").css("background-color","yellow");
      }      
      else if($(".check_id:checked").length >= 8 &&    $(".check_id:checked").length <= 10 )
      {
          $("#progress-bar-filling").css("background-color","green");
      }
  }

  var complete = zzz+'0';
  progress(complete, $('#progress-bar'));
})
}




</script>
<p id="insert"></p>

<div class="progress-bar-wrapper">
  <div id="progress-bar">
    <div id="progress-bar-filling" class="progress-bar-filling"></div>
  </div>
     <div class="progress-bar-percent"></div>
 </div>

示例:https://jsfiddle.net/tLq07L77/1/

票数 0
EN

Stack Overflow用户

发布于 2016-11-03 00:42:10

由于您已经知道了:checked复选框的数量,您可以添加3个新类(在css中)来设置进度栏的背景色:

代码语言:javascript
运行
复制
.progress-bar-wrapper .progress-bar-filling.fill0 {
  background-color: red;
}
.progress-bar-wrapper .progress-bar-filling.fill1 {
  background-color: yellow;
}
.progress-bar-wrapper .progress-bar-filling.fill2 {
  background-color: #39b54a;
}

javascript代码中--首先--删除所有fill0,fill1,fill2类,然后--根据您的数字--只添加相关的类:

代码语言:javascript
运行
复制
$('.progress-bar-filling').removeClass('fill0 fill1 fill2');
if (zzz <= 3) {
  $('.progress-bar-filling').addClass('fill0')
} else if (zzz <= 7) {
  $('.progress-bar-filling').addClass('fill1')
} else {
  $('.progress-bar-filling').addClass('fill2')
}

下面是一个有用的例子:

https://jsfiddle.net/g4Lhvawq/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40391979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档