【问题标题】:jQuery : Change var when clickingjQuery:单击时更改 var
【发布时间】:2017-08-16 22:51:47
【问题描述】:

我有一个圆形进度条,并希望在单击 div 时更新它(及其值的跨度)。单击应将输入的值添加到 var。我为此创建了一个小预览。我的问题是点击时 var 似乎没有改变。任何人都可以帮忙吗?谢谢!

var calories = 1000;
var total = 2500;
var percentage = calories / total;
var pluscal = $('#quantity').val();
var span = $('#today-progress span').html(calories + '/' + total);


/* setInterval(() => {
  calories = 0
}, 1000 * 60 * 60 * 24);
var today = new Date().getTime();
localStorage.setItem("today", today); */


$('#confirm-svg').click(function() {
  calories = calories + pluscal;
  $('#today-progress').circleProgress();
});


$('#today-progress').circleProgress({
  value: percentage,
  size: 300.0,
  startAngle: -1.57,
  thickness: 'auto',
  fill: {
    gradient: ['#CE6363', '#B23939'],
  },
  emptyFill: '#D6A0A0',
  animation: {
    duration: 3000,
    easing: 'circleProgressEasing'
  },
  animationStartValue: 0.0,
  reverse: false,
  lineCap: 'round',
});
body {
  font-family: Helvetica, sans-serif;
}

#today-progress {
  z-index: 0;
  position: relative;
  top: 10px;
  text-align: center;
}

#today-progress span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  font-size: 45px;
}

svg {
  width: 30px;
  height: 30px;
  margin: 5px 10px;
  float: right;
  fill: #000;
}

svg:active {
  fill: red;
}

form {
  margin-top: 20px;
  margin-left: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/kottenator/jquery-circle-progress/1.2.2/dist/circle-progress.js"></script>
<div id="today-progress">
  <span></span>
</div>

<form>
  Quantity (between 1 and 1000):
  <input type="number" id="quantity" min="1" max="1000">
</form>

<div id="confirm-svg">
  <svg id="confirm-svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" width="512px" height="512px" viewBox="0 0 305.002 305.002" style="enable-background:new 0 0 305.002 305.002;"
  xml:space="preserve">
    <g>
      <g>
        <path d="M152.502,0.001C68.412,0.001,0,68.412,0,152.501s68.412,152.5,152.502,152.5c84.089,0,152.5-68.411,152.5-152.5    S236.591,0.001,152.502,0.001z M152.502,280.001C82.197,280.001,25,222.806,25,152.501c0-70.304,57.197-127.5,127.502-127.5    c70.304,0,127.5,57.196,127.5,127.5C280.002,222.806,222.806,280.001,152.502,280.001z"
        />
        <path d="M218.473,93.97l-90.546,90.547l-41.398-41.398c-4.882-4.881-12.796-4.881-17.678,0c-4.881,4.882-4.881,12.796,0,17.678    l50.237,50.237c2.441,2.44,5.64,3.661,8.839,3.661c3.199,0,6.398-1.221,8.839-3.661l99.385-99.385    c4.881-4.882,4.881-12.796,0-17.678C231.269,89.089,223.354,89.089,218.473,93.97z"
        />
      </g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
    <g>
    </g>
  </svg>
</div>

(JSFiddle here) 另外,如果有可能我想每 24 小时将这个 var(卡路里)重置为 0,你可以在 js 代码的评论中看到我可怜的尝试。如果你能帮助我,那就太棒了太:)

【问题讨论】:

    标签: jquery progress-bar reset var


    【解决方案1】:

    每次单击提交复选标记按钮时,您都必须更新在顶部声明的变量。

    此外,您应该解析输入框中的值,并且应该调用circleProgress() 并使用所有必要的设置来重新绘制圆圈内容。你现在的方式,没有设置。

    以下内容可以帮助您入门:See JSFiddle here

    $('#confirm-svg').click(function() {
      // Every time you click, you should parse the integer from the input,
      // then add that to the sum of calories.
      calories = calories + parseInt($('#quantity').val(), 10);
      percentage = calories / total; 
    
      // Then you have to update the calories mark with the new value of calories
      $('#today-progress span').html(calories + '/' + total);
      // Then you have to redraw the circle stuff
      drawCircle()
    });
    
    function drawCircle() {
      $('#today-progress').circleProgress({
        value: percentage,
        size: 300.0,
        startAngle: -1.57,
        thickness: 'auto',
        fill: {
          gradient: ['#CE6363', '#B23939'],
        },
        emptyFill: '#D6A0A0',
        animation: {
          duration: 3000,
          easing: 'circleProgressEasing'
        },
        animationStartValue: 0.0,
        reverse: false,
        lineCap: 'round',
      });
    }
    
    // Draw the initial circle
    drawCircle();
    

    【讨论】:

    • 非常感谢!那行得通,我完全忘记了 parseInt 的必要性。我在函数 drawCircle 中添加了跨度更改更新,所以只有这个函数可以调用,但你解决了,谢谢 :)
    • 对不起,我想接受你的回答,但是重置问题仍然没有解决,如果我接受它,没有人会看它,如果你没问题我会接受它
    • @MaëlleJumel 你可以设置一个超时时间让它在 24 小时后重置,但是谁会在你的网站上等待 24 小时来重置它?如果您谈论的是永久存储,那是完全不同的东西,需要后端数据库来维护您的数据。前端只关心视图,应该从其他地方接收数据。它只是以一种漂亮友好的格式呈现数据。
    【解决方案2】:

    我在你的小提琴中添加了...

    setInterval(() => {
        calories = 0;
        $('#today-progress').circleProgress();
    }, 5000);
    

    它每 5 秒用动画重置一次。不考虑 5 秒和 24 小时,这就是你所追求的吗?

    【讨论】:

    • 您好,感谢您的回答!这是我正在寻找的东西,但 var 没有设置回 0?您还知道,使用您的代码,刷新时该值是否会保持不变,这意味着间隔不会再次开始?
    • @MaëlleJumel 我认为您缺少一些 Web 开发的基本概念。前端没有什么是永久的,如果你刷新页面,它会回到它的初始状态。是的,您可以尝试使用本地存储,但这只是应用程序用户的本地存储。要拥有“真正的永久存储”,您需要了解后端和数据库的概念。你在前端看到的只是从其他地方接收到的数据的友好再现。简而言之,前端总是首先从服务器获取一些数据,然后以人类友好的格式显示。
    • 是的,我想让它成为“真正的永久存储”,我需要了解这一点。抱歉,我确实不是专家,我只是通过创建网站草稿(HTML/CSS/JS)来学习主要内容,但从来没有一个具有域和实际数据的实际站点@nbkhope
    【解决方案3】:

    要回答您重置卡路里变量的问题, 假设你想每上午 10 点重置一次, 试试:

    var now = new Date();
        var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 10, 0, 0, 0) - now;
        if (millisTill10 < 0) {
             millisTill10 += 86400000; // it's after 10am, try 10am tomorrow. 10 is my number
        }
        setTimeout(function(){var calories = 0;}),
    

    让我知道它是如何工作的。(暂时不要投反对票)玩得开心编码 ;-)。

    【讨论】:

    • 嘿,谢谢!尽管这与其他答案一样,但只有在您不重新加载页面时才有效?
    • 是的。您究竟打算如何让您的应用程序工作?
    • 您可以创建一个简单的 PHP 脚本来处理时间问题。这将负责刷新页面。如果您需要帮助,请告诉我。
    • 我要研究一下这个 PHP 的东西,谢谢。如果我迷路了,我会告诉你的:)
    • 酷。万事如意
    猜你喜欢
    • 2016-02-06
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多