Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>	

<!-- 向左遞減 -->
<div class="progessbar">
  <div class="text"></div>
  <div class="bar" style="left: 0;"></div>
</div>

<br>

<!-- 向右遞減 -->
<div class="progessbar">
  <div class="text"></div>
  <div class="bar" style="right: 0;"></div>
</div>

<br>

<!-- 向右遞增 -->
<div class="progessbar">
  <div class="text"></div>
  <div class="bar_2" style="left: 0;"></div>
</div>

<br>

<!-- 向左遞增 -->
<div class="progessbar">
  <div class="text"></div>
  <div class="bar_2" style="right: 0;"></div>
</div>

<div class="msg"></div>

              
            
!

CSS

              
                .progessbar {
  position: relative;
  text-align: center;
  border: 1px dashed blue;
  height: 16px;
  width: 250px;
}

.progessbar .text {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  line-height: 16px;
  z-index: 10;
}

.progessbar .bar {
  position: absolute;
  /* 為了便於同時展示多個進度條,right跟left直接寫在html style裡。 */
  /* 實務上可以將style移除,改用下面的css來達成right或left的設定。 */
  /*right: 0;*/
  /*left: 0;*/
  top: 0;
  height: 16px;
  background-color: green;
}

.progessbar .bar_2 {
  position: absolute;
  /* 為了便於同時展示多個進度條,right跟left直接寫在html style裡。 */
  /* 實務上可以將style移除,改用下面的css來達成right或left的設定。 */
  /*right: 0;*/
  /*left: 0;*/
  top: 0;
  height: 16px;
  background-color: green;
}
              
            
!

JS

              
                // 起始時間(計時器的啟動時間)。
const startTime = new Date().getTime();

// 目標時間(要倒數幾秒)。
const targetSeconds = 120;

// timer.
var timer = function (startTime) {
  // 當前時間。
  var currentTime = new Date().getTime();
  
  // 當前時間 - 起始時間 = 經過時間。(因為不需要毫秒,所以將結果除以1000。)
  var diffSec = Math.round((currentTime - startTime) / 1000);
  
  // 目標時間 - 經過時間 = 剩餘時間。
  var remainingTime = targetSeconds - diffSec;
  
  // update progess.  
  update(remainingTime);   
  
  if (remainingTime == 0) {
    // stop the timer.
    clearInterval(timerId);
    
    // do anything you want to.
    $(".msg").text("time up!");
  } 
}

// start the timer.
var timerId = setInterval( function () { timer(startTime); }, 1000);

// update progess with the timer.
function update (seconds) {
  barRenderer(seconds);
  textRenderer(seconds);
}

// refresh the bars.
function barRenderer (seconds) {
  // 遞減計算。
  var percent = (seconds / targetSeconds) * 100;
  $(".bar").css("width", percent + "%");
  
  // 遞增計算。
  var percent_2 = (1 - (seconds / targetSeconds)) * 100;
  $(".bar_2").css("width", percent_2 + "%");
}

// refresh the text of the bar.
function textRenderer (seconds) {
  var sec = seconds % 60;  
  var min = Math.floor(seconds / 60); 

  /* 兩種作法都可以 */
  //min = min > 9 ? min : "0" + min;
  //sec = sec > 9 ? sec : "0" + sec;  
  min = min.toString().padStart(2, '0');
  sec = sec.toString().padStart(2, '0');
  
  $(".text").text(min + ":" + sec);		
}
              
            
!
999px

Console