【问题标题】:Fill css progressbar value/width when reaching a set time/date达到设定时间/日期时填充css进度条值/宽度
【发布时间】:2016-11-23 19:52:24
【问题描述】:

我正在尝试制作一个宽度由日期和时间定义的 CSS 进度条。

目的是在达到特定的预定义日期和时间集时创建进度条。

例如:当日期为 2014 年 12 月 25 日时,进度条达到 100%。 我认为计算方法应该是:[(date&time_defined) - (date&time_today)] =宽度百分比的值,所以当[(date&time_defined)==(date&time_today)]宽度百分比应该是100%。

这是我使用的进度条 css 代码

     <html>
   <body>
       <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
     <script>
     $(function() {
            $(".meter > span").each(function() {
                $(this)
                    .data("origWidth", $(this).width())
                    .width(0)
                    .animate({
                        width: $(this).data("origWidth")
                    }, 1200);
            });
        });
    </script>

    <style>
        .meter { 
            height: 20px;  /* Can be anything */
            position: relative;
            margin: 60px 0 20px 0; /* Just for demo spacing */
            background: #555;
            -moz-border-radius: 25px;
            -webkit-border-radius: 25px;
            border-radius: 25px;
            padding: 10px;
            -webkit-box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3);
            -moz-box-shadow   : inset 0 -1px 1px rgba(255,255,255,0.3);
            box-shadow        : inset 0 -1px 1px rgba(255,255,255,0.3);
        }
        .meter > span {
            display: block;
            height: 100%;
               -webkit-border-top-right-radius: 8px;
            -webkit-border-bottom-right-radius: 8px;
                   -moz-border-radius-topright: 8px;
                -moz-border-radius-bottomright: 8px;
                       border-top-right-radius: 8px;
                    border-bottom-right-radius: 8px;
                -webkit-border-top-left-radius: 20px;
             -webkit-border-bottom-left-radius: 20px;
                    -moz-border-radius-topleft: 20px;
                 -moz-border-radius-bottomleft: 20px;
                        border-top-left-radius: 20px;
                     border-bottom-left-radius: 20px;
            background-color: rgb(43,194,83);
            background-image: -webkit-gradient(
              linear,
              left bottom,
              left top,
              color-stop(0, rgb(43,194,83)),
              color-stop(1, rgb(84,240,84))
             );
            background-image: -moz-linear-gradient(
              center bottom,
              rgb(43,194,83) 37%,
              rgb(84,240,84) 69%
             );
            -webkit-box-shadow: 
              inset 0 2px 9px  rgba(255,255,255,0.3),
              inset 0 -2px 6px rgba(0,0,0,0.4);
            -moz-box-shadow: 
              inset 0 2px 9px  rgba(255,255,255,0.3),
              inset 0 -2px 6px rgba(0,0,0,0.4);
            box-shadow: 
              inset 0 2px 9px  rgba(255,255,255,0.3),
              inset 0 -2px 6px rgba(0,0,0,0.4);
            position: relative;
            overflow: hidden;
        }
        .meter > span:after, .animate > span > span {
            content: "";
            position: absolute;
            top: 0; left: 0; bottom: 0; right: 0;
            background-image: 
               -webkit-gradient(linear, 0 0, 100% 100%, 
                  color-stop(.25, rgba(255, 255, 255, .2)), 
                  color-stop(.25, transparent), color-stop(.5, transparent), 
                  color-stop(.5, rgba(255, 255, 255, .2)), 
                  color-stop(.75, rgba(255, 255, 255, .2)), 
                  color-stop(.75, transparent), to(transparent)
               );
            background-image: 
                -moz-linear-gradient(
                  -45deg, 
                  rgba(255, 255, 255, .2) 25%, 
                  transparent 25%, 
                  transparent 50%, 
                  rgba(255, 255, 255, .2) 50%, 
                  rgba(255, 255, 255, .2) 75%, 
                  transparent 75%, 
                  transparent
               );
            z-index: 1;
            -webkit-background-size: 50px 50px;
            -moz-background-size: 50px 50px;
            -webkit-animation: move 2s linear infinite;
               -webkit-border-top-right-radius: 8px;
            -webkit-border-bottom-right-radius: 8px;
                   -moz-border-radius-topright: 8px;
                -moz-border-radius-bottomright: 8px;
                       border-top-right-radius: 8px;
                    border-bottom-right-radius: 8px;
                -webkit-border-top-left-radius: 20px;
             -webkit-border-bottom-left-radius: 20px;
                    -moz-border-radius-topleft: 20px;
                 -moz-border-radius-bottomleft: 20px;
                        border-top-left-radius: 20px;
                     border-bottom-left-radius: 20px;
            overflow: hidden;
        }

        .animate > span:after {
            display: none;
        }

        @-webkit-keyframes move {
            0% {
               background-position: 0 0;
            }
            100% {
               background-position: 50px 50px;
            }
        }

        .orange > span {
            background-color: #f1a165;
            background-image: -moz-linear-gradient(top, #f1a165, #f36d0a);
            background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #f1a165),color-stop(1, #f36d0a));
            background-image: -webkit-linear-gradient(#f1a165, #f36d0a); 
        }

        .red > span {
            background-color: #f0a3a3;
            background-image: -moz-linear-gradient(top, #f0a3a3, #f42323);
            background-image: -webkit-gradient(linear,left top,left bottom,color-stop(0, #f0a3a3),color-stop(1, #f42323));
            background-image: -webkit-linear-gradient(#f0a3a3, #f42323);
        }

        .nostripes > span > span, .nostripes > span:after {
            -webkit-animation: none;
            background-image: none;
        }
    </style>


    <div class="meter">
            <span style="width: PERCENTAGE%"></span>
        </div>
  </body>
  </html>  

如果可能的话,请向我展示在上述代码中使用所需的代码,因为我对 CSS 和 javascript 或其他任何东西都不太熟悉。我只是想开始:)

编辑:我只需要修改上面的代码,这样当我将日期 $ 时间(例如 2014 年 12 月 25 日 14:08)插入提供的任何位置时,它就可以工作了.. 而不是固定宽度

【问题讨论】:

  • 您的问题是什么?您是否要求我们为您提供代码?还是您在上面发布的代码有问题需要修复?
  • 实际上是一种修改。当您插入宽度百分比时,上面的代码可以完美运行。我想要的是插入一个百分比,我会插入一个带时间的日期,以便它通过图形进度条显示剩余的剩余时间。
  • 好问题。你会有多个进度条吗?您在 JQuery 中的选择器似乎表明您可能会这样做。我现在正在做一个小提琴,因此我的问题。

标签: javascript jquery css html


【解决方案1】:

您需要一种有效的方法将日期转换为数字(最简单的方法是使用毫秒)。您还需要一个用于计算的参考点(让我们使用年初)。

一旦你有了这些东西,公式就很简单了。这是伪代码:

(difference of current from start date / difference of final date from start date) * 100

在 JS 中:

var targetDate = new Date("12/25/2014");
var beginDate = new Date("01/01/2014");
var totalTime = (targetDate - beginDate);
var dateProgress = new Date(data.Date) - beginDate;
var completionPercentage = (Math.round((dateProgress / totalTime) * 100));

然后就变成了代码结构的问题。我喜欢在 JQuery 中使用自定义事件来保持代码 DRY,所以下面是我解决它的方法:

var targetDate = new Date("12/25/2014");
var beginDate = new Date("01/01/2014");
var totalTime = (targetDate - beginDate);
$(function () {

    //Create a custom event on the span to handle incoming data for animation.
    $(".meter > span").bind("progress-event", function (e, data) {
        $(this)
            .width($(this).prop("width"))
            .data("origWidth", data.Complete)
            .animate({
            width: $(this).data("origWidth") + "%"
        }, 1200);
        $(this).prop("title",($(this).data("origWidth") + "%"));
    });
    //Initial animation on page load.
    $(".meter > span").each(function () {
        $(document).trigger("date-changed", {
            Date: new Date() // today's date
        });
    });

    //Apply the datepicker with an event handler for a selected date
    $("#date-input").datepicker({
        onSelect: function (selectedDate, obj) {
            $(document).trigger("date-changed", {
                Date: selectedDate
            });
        } // end onSelect function
    });
});

//Custom event to handle a date being picked from the datepicker
$(document).bind("date-changed", function (e, data) {
    var dateProgress = new Date(data.Date) - beginDate;
    var completionPercentage = (Math.round((dateProgress / totalTime) * 100));
    if(completionPercentage > 100) {  //Make sure we don't go past 100
        completionPercentage = 100;
    } // end if
    $(".meter > span").trigger("progress-event", {
        Date: data.Date,
        Complete: completionPercentage
    });
});

在这里,我创建了两个自定义事件:date-changedprogress-eventdate-changed 事件我用于演示目的,当从文本框中的日期选择器中选择日期时触发。这会在执行一些计算后将“选定”日期提供给progress-eventprogress-event 的职责只是填充进度条的数据和动画。

重要的部分是在$(document).trigger(...... 部分,我在其中给出了我触发的事件的名称,并且我构建了一个 JavaScript 对象,其中包含每个方法所需的数据(在您的情况下,是一个日期对象)。

这是一个 JsFiddle: http://jsfiddle.net/xDaevax/pzgxuoj9/

请注意,我使用的是 JQuery UI,以及比您更新版本的 JQuery。 JQuery UI 不是必需的(除非您想使用它来选择日期),并且此代码应该与您的 JQuery 版本一起使用(您可能必须在我使用 prop 方法的地方替换为对 attr 的调用)。另外需要注意的是,我将此代码放在 HTML 的 HEAD 部分,而不是正文中。

【讨论】:

  • 感谢您的努力! :) 但我真正想要的是将目标日期与时间也作为预定义而不是作为输入框。我要使用的地方不是供用户选择的。主要是用作网站建设进度条,预设时间完成。希望你明白我的意思:)
  • @mpuria 是的,您不需要日期选择器,只需使用今天的日期并在页面加载时触发事件。
  • 好的。时间也怎么样,例如我希望它设置为在 2014 年 12 月 24 日 23:59 完成。我的意思是:)
  • @mpuria JavaScript 日期对象的构造函数允许您传递许多不同格式的日期,因此您可以说:targetDate = new Date("12/24/2014 23:59:59");targetDate = new Date("2014", "12", "24", "23", "59", "59", "00");,而不是像上面那样指定 targetDate。有关更多示例,请参见此处:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 这是我所做的,但没有工作http://jsfiddle.net/028zca8L/
【解决方案2】:

你可以试试这个吗?

 <script>
 $(function(){
   setTimeout(progressBar, 100);
 });
 function progressBar(){
    var oldValue=$('.progressbar').val();
    $('.progressbar').val(oldValue+1);
    setTimeout(progressBar, 100);
  }
</script>
<progress class="progressbar" value="10" max="100"></progress>

【讨论】:

  • 我应该把它放在哪里??抱歉,我刚接触 css 和 java :)
  • 只需复制此代码并将其粘贴到您的 html 文件中并运行它。我保持的每分钟进度条都会增加。你想增加时间,你可以。
【解决方案3】:

** 以秒为单位的进度条填充 **

function move() {
  var elem = document.getElementById("myBar");
  var width = 1;
  var id = setInterval(frame, 100); // set time ex: 100 as 10s 
  function frame() {
    if (width >= 100) {
      clearInterval(id);
    } else {
      width++;
      elem.style.width = width + '%';
      document.getElementById("label").innerHTML = width * 1  + '%';
    }
  }
return false;
}
<style>

#label {
  text-align: center;
  line-height: 22px;
  color: white;
}
.meter {
    height: 20px;
    position: relative;
    margin: 60px 0 20px 0;
    background: #555;
    -moz-border-radius: 25px;
    -webkit-border-radius: 25px;
    border-radius: 25px;
    padding: 10px;
    -webkit-box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3);
    -moz-box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3);
    box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3);
}
.meter > span {
    display: block;
    width:1%;
    height: 100%;
    -webkit-border-top-right-radius: 8px;
    -webkit-border-bottom-right-radius: 8px;
    -moz-border-radius-topright: 8px;
    -moz-border-radius-bottomright: 8px;
    border-top-right-radius: 8px;
    border-bottom-right-radius: 8px;
    -webkit-border-top-left-radius: 20px;
    -webkit-border-bottom-left-radius: 20px;
    -moz-border-radius-topleft: 20px;
    -moz-border-radius-bottomleft: 20px;
    border-top-left-radius: 20px;
    border-bottom-left-radius: 20px;
    background-color: rgb(43,194,83);
    background-image: -webkit-gradient( linear, left bottom, left top, color-stop(0, rgb(43,194,83)), color-stop(1, rgb(84,240,84)) );
    background-image: -moz-linear-gradient( center bottom, rgb(43,194,83) 37%, rgb(84,240,84) 69% );
    -webkit-box-shadow: inset 0 2px 9px rgba(255,255,255,0.3), inset 0 -2px 6px rgba(0,0,0,0.4);
    -moz-box-shadow: inset 0 2px 9px rgba(255,255,255,0.3), inset 0 -2px 6px rgba(0,0,0,0.4);
    box-shadow: inset 0 2px 9px rgba(255,255,255,0.3), inset 0 -2px 6px rgba(0,0,0,0.4);
    position: relative;
    overflow: hidden;
}
.meter > span:after, .animate > span > span {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-image: -webkit-gradient(linear, 0 0, 100% 100%, color-stop(.25, rgba(255, 255, 255, .2)), color-stop(.25, transparent), color-stop(.5, transparent), color-stop(.5, rgba(255, 255, 255, .2)), color-stop(.75, rgba(255, 255, 255, .2)), color-stop(.75, transparent), to(transparent) );
    background-image: -moz-linear-gradient( -45deg, rgba(255, 255, 255, .2) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, .2) 50%, rgba(255, 255, 255, .2) 75%, transparent 75%, transparent );
    z-index: 1;
    -webkit-background-size: 50px 50px;
    -moz-background-size: 50px 50px;
    background-size: 50px 50px;
    -webkit-animation: move 1s linear infinite;
    -moz-animation: move 2s linear infinite;
    -webkit-border-top-right-radius: 8px;
    -webkit-border-bottom-right-radius: 8px;
    -moz-border-radius-topright: 8px;
    -moz-border-radius-bottomright: 8px;
    border-top-right-radius: 8px;
    border-bottom-right-radius: 8px;
    -webkit-border-top-left-radius: 20px;
    -webkit-border-bottom-left-radius: 20px;
    -moz-border-radius-topleft: 20px;
    -moz-border-radius-bottomleft: 20px;
    border-top-left-radius: 20px;
    border-bottom-left-radius: 20px;
    overflow: hidden;
}
</style>
<body onload="move()">



<div id="myProgress" class="meter">
  <span id="myBar">
    <center><div id="label">1%</div></center>
  </span>
</div>

<br>




</body>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-06
    • 2020-03-24
    • 2013-09-28
    • 2011-07-16
    • 1970-01-01
    • 2014-04-21
    • 2014-05-05
    • 2012-01-12
    相关资源
    最近更新 更多