进度条

此示例展示了如何实现平滑滚动的进度条。

¥This example shows how to implement a smoothly scrolling progress bar.

我们从一个初始状态开始,该按钮发出 POST/start 以开始工作:

¥We start with an initial state with a button that issues a POST to /start to begin the job:

<div hx-target="this" hx-swap="outerHTML">
  <h3>Start Progress</h3>
  <button class="btn primary" hx-post="/start">
            Start Job
  </button>
</div>

然后,此 div 被替换为包含状态和进度条的新 div,该 div 每 600 毫秒重新加载一次:

¥This div is then replaced with a new div containing status and a progress bar that reloads itself every 600ms:

<div hx-trigger="done" hx-get="/job" hx-swap="outerHTML" hx-target="this">
  <h3 role="status" id="pblabel" tabindex="-1" autofocus>Running</h3>

  <div
    hx-get="/job/progress"
    hx-trigger="every 600ms"
    hx-target="this"
    hx-swap="innerHTML">
    <div class="progress" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" aria-labelledby="pblabel">
      <div id="pb" class="progress-bar" style="width:0%">
    </div>
  </div>
</div>

此进度条每 600 毫秒更新一次,其中 “width” 样式属性和 aria-valuenow 属性设置为当前进度值。由于进度条 div 上有一个 id,因此 htmx 将通过将样式属性设置为其新值来平滑地在请求之间过渡。当与 CSS 过渡结合使用时,可以使视觉过渡连续而不是跳跃。

¥This progress bar is updated every 600 milliseconds, with the “width” style attribute and aria-valuenow attributed set to current progress value. Because there is an id on the progress bar div, htmx will smoothly transition between requests by settling the style attribute into its new value. This, when coupled with CSS transitions, makes the visual transition continuous rather than jumpy.

最后,当该过程完成时,服务器将返回 HX-Trigger: done 标头,这将触发 UI 更新到 “完成” 状态,并在 UI 中添加一个重新启动按钮(我们在此示例中使用 class-tools 扩展在按钮上添加淡入效果):

¥Finally, when the process is complete, a server returns HX-Trigger: done header, which triggers an update of the UI to “Complete” state with a restart button added to the UI (we are using the class-tools extension in this example to add fade-in effect on the button):

<div hx-trigger="done" hx-get="/job" hx-swap="outerHTML" hx-target="this">
  <h3 role="status" id="pblabel" tabindex="-1" autofocus>Complete</h3>

  <div
    hx-get="/job/progress"
    hx-trigger="none"
    hx-target="this"
    hx-swap="innerHTML">
      <div class="progress" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="122" aria-labelledby="pblabel">
        <div id="pb" class="progress-bar" style="width:122%">
      </div>
    </div>
  </div>

  <button id="restart-btn" class="btn primary" hx-post="/start" classes="add show:600ms">
    Restart Job
  </button>
</div>

此示例使用从 bootstrap 进度条中抄袭的样式:

¥This example uses styling cribbed from the bootstrap progress bar:

.progress {
    height: 20px;
    margin-bottom: 20px;
    overflow: hidden;
    background-color: #f5f5f5;
    border-radius: 4px;
    box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
}
.progress-bar {
    float: left;
    width: 0%;
    height: 100%;
    font-size: 12px;
    line-height: 20px;
    color: #fff;
    text-align: center;
    background-color: #337ab7;
    -webkit-box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);
    box-shadow: inset 0 -1px 0 rgba(0,0,0,.15);
    -webkit-transition: width .6s ease;
    -o-transition: width .6s ease;
    transition: width .6s ease;
}
Server Requests ↑ Show

🔗Demo