【问题标题】:Countdown script hours countdown Percentage goes Down倒计时脚本小时倒计时百分比下降
【发布时间】:2015-10-24 17:48:21
【问题描述】:

我是编程新手,一直在尝试创建一个图形倒计时脚本,随着时间的推移减少价格折扣。

随着时间倒计时,折扣百分比会减少并调整价格。

因此,它可能从 100 英镑开始,折扣 70%,随着时间的推移,折扣减少,所以在 69 小时时折扣为 69%,30 小时为 30%,等等。

我发现了几个不错的图形化 sn-ps How to create a circular countdown timer using HTML, CSS or JavaScript?

http://jsfiddle.net/lloydhester/4PKEB/204/

<div id="progressBar">
  <div></div>
</div>

<a href="https://stackoverflow.com/q/24530908/1238019" class="solink" target="_blank">Div CSS Transition Direction</a>

function progress(timeleft, timetotal, $element) {
    var progressBarWidth = timeleft * $element.width() / timetotal;
    $element.find('div').animate({ width: progressBarWidth }, 500).html(timeleft + " Time to go");
    if(timeleft > 0) {
        setTimeout(function() {
            progress(timeleft - 1, timetotal, $element);
        }, 1000);
    }
};

progress(180, 180, $('#progressBar'));

#progressBar {
  width: 90%;
  margin: 10px auto;
  height: 22px;
  background-color: #0A5F44;
}

#progressBar div {
  height: 100%;
  text-align: right;
  padding: 0 10px;
  line-height: 22px; /* same as #progressBar height if we want text middle aligned */
  width: 0;
  background-color: #CBEA00;
  box-sizing: border-box;
}

以 % 为单位进行倒计时的脚本 http://jsfiddle.net/gS4Rr/1/

<div id="counter">
    <p><span>100</span>%</p>
</div>


var stop = 6;

function decrease(){
    var percent = Number($('#counter span').text());
    if(percent > stop){
        $('#counter span').text(--percent);
        var t = setTimeout(decrease,10000);
    }
}
setTimeout(decrease,1000);

我可以找到一个示例代码将价格提高一个固定百分比。但不是变化的百分比。

我们将非常感激任何帮助。

【问题讨论】:

  • 请看看我的回答,告诉我你是否想要达到这个目标。

标签: javascript timer countdown percentage discount


【解决方案1】:

我不确定您是否想要实现,但请查看 Fiddle Example。

使用:

((timeleft/timetotal)*100).toFixed(1)

用百分比显示倒计时。

Js:

 function progress(timeleft, timetotal, $element) {
    var progressBarWidth = timeleft * $element.width() / timetotal;

    $element.find('div').animate({ 
        width: progressBarWidth }, 500).html(((timeleft/timetotal)*100).toFixed(1) + "% Time to go");
    if(timeleft > 0) {
        setTimeout(function() {
            progress(timeleft - 1, timetotal, $element);
        }, 1000);
    }
};

progress(180, 180, $('#progressBar'));

希望有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多