pace README

The project's README from github.com/CodeByZach/pace. As one markdown file: README.md.

PACE

Latest Release jsDelivr hits (npm) Stars Forks License

An automatic web page progress bar.

Demo

Documentation

Include pace.js and the theme css of your choice on your page (as early as possible), and you're done!

Pace will automatically monitor your ajax requests, event loop lag, document ready state, and elements on your page to decide the progress. On ajax navigation it will begin again!

If you use AMD or Browserify, require pace.js and call pace.start() as early in the loading process as possible.

Example

<head>
  <script src="https://cdn.jsdelivr.net/npm/pace-js@latest/pace.min.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pace-js@latest/pace-theme-default.min.css">
</head>

Configuration

Pace is fully automatic, no configuration is necessary to get started.

If you would like to make some tweaks, here's how:

You can set window.paceOptions before bringing in the file:

paceOptions = {
  // Disable the 'elements' source
  elements: false,

  // Only show the progress on regular and ajax-y page navigation,
  // not every request
  restartOnRequestAfter: false
}

You can also put options on the script tag:

<script data-pace-options='{ "ajax": false }' src="https://cdn.jsdelivr.net/npm/pace-js@latest/pace.min.js"></script>

If you're using AMD or Browserify, you can pass your options to start:

define(['pace'], function(pace){
  pace.start({
    document: false
  });
});

Themes

Pace includes a bunch of themes to get you started. Just include the appropriate css file. Send us a PR with any interesting themes you create.

If you have minor styling changes and don't want to extend theme css, you can add custom class names to the progress bar using the "className" option:

paceOptions = {
  className: 'my-custom-class'
}

Collectors

Collectors are the bits of code which gather progress information. Pace includes four default collectors:

They can each be configured or disabled through configuration options of the same name.

paceOptions = {
  ajax: false, // disabled
  document: false, // disabled
  eventLag: false, // disabled
  elements: {
    selectors: ['.my-page']
  }
};

Add your own classes to paceOptions.extraSources to add more sources. Each source should either have a .progress property, or a .elements property which is a list of objects with .progress properties. Pace will automatically handle all scaling to make the progress changes look smooth to the user.

Elements

Elements being rendered to the screen is one way for us to decide that the page has been rendered. If you would like to use that source of information (not required at all), specify one or more selectors. You can comma separate the selectors to properly handle error states, where the progress bar should disappear, but the element we are looking for may never appear:

paceOptions = {
  elements: {
    selectors: ['.timeline, .timeline-error', '.user-profile, .profile-error']
  }
}

Pace will consider the elements test successful when each selector matches something. For this example, when either .timeline or .timeline-error exist, and either .user-profile or .profile-error exist.

Restart Rules

Most users want the progress bar to automatically restart when a pushState event occurs (generally means ajax navigation is occuring). You can disable this:

paceOptions = {
  restartOnPushState: false
}

You can also have pace restart on every ajax request which lasts longer than x ms. You'll want to disable this if you make ajax requests the user doesn't need to know about, like precaching:

paceOptions = {
  restartOnRequestAfter: false
}

You can always trigger a restart manually by calling Pace.restart()

See the source for a full list of options.

API

Pace exposes the following methods:

Events

Pace fires the following events:

You can bind onto events using the on, off and once methods:

Tracking

By default, Pace will show any ajax requests which begin as a part of a normal or ajax-y page load, or which last longer than 500ms.

You can disable all ajax tracking by setting ajax to false:

Pace.options = {
  ajax: false
}

You can disable ajax tracking except on page navigation by setting restartOnRequestAfter to false:

Pace.options = {
  restartOnRequestAfter: false
}

You can manually disable tracking for a specific request or requests by triggering them within a Pace.ignore callback:

Pace.ignore(function() {
  $.ajax(...)
});

You can force the progress bar to be shown for a specific request by triggering them within a Pace.track callback:

Pace.track(function() {
  $.ajax(...)
});

You can also ignore URLs based on a pattern:

Pace.options = {
  ajax: {
    ignoreURLs: ['some-substring', /some-regexp/]
  }
}

Dependencies

None!

Support

Pace is designed to support IE8+ (standards mode), FF 3.5+, Chrome, Safari 4+, Opera 10.5+, and all modern mobile browsers. If you run into a compatibility issue, or can make a case for supporting something else, please create an issue.

Size

pace.js is 4kb minified and gzipped. The themes vary between 0.5 and 4kb.

Issues

We have obviously not tested this on every website. If you run into an issue, or find a way the automatic detection could be better, please create an Issue. If you can include a test case, that's even better.

Credits

HubSpot

CodeByZach

Javascript by Zack Bloom CSS by Adam Schwartz

Themes inspired by Mary Lou

Project inspired by nprogress

pace (CodeByZach/pace)

Searching Database…

15.6K ·GitHub
这是什么东西?Pace 是一个开源 JavaScript 库,能自动给任意网页添加进度条。它会监控页面的活动并显示加载进度,不需要手动做任何配置。
怎么把它加到我的网站上?尽早地在页面上引入 pace.js 和你喜欢的主题 css 就可以了。如果你使用 AMD 或 Browserify,则在加载过程中尽早 require pace.js 并调用 pace.start()。
它有依赖吗?体积大不大?没有任何依赖 —— README 里写的依赖是 'None!'。pace.js 压缩并 gzip 后只有 4kb,主题大小在 0.5 到 4kb 之间。
能让它不显示后台 ajax 请求的进度吗?可以 —— 默认情况下,进度条只对页面加载期间发起的请求或耗时超过 500ms 的请求显示。你还可以用 Pace.ignore 包裹请求来跳过它们,或用 Pace.track 强制显示,也可以按 URL 模式忽略特定请求。
支持哪些浏览器?支持标准模式下的 IE8+、Firefox 3.5+、Chrome、Safari 4+、Opera 10.5+,以及所有现代移动浏览器。
我能从自己的代码里给它提供进度吗?可以 —— 把你自己的数据源加到 paceOptions.extraSources 里。每个数据源需要有一个 .progress 属性,或者一个包含带 .progress 的对象的 .elements 列表,Pace 会对变化做平滑处理,让更新看起来更自然。
页面加载完成后会发生什么?Pace 会触发一个 done 事件,然后隐藏进度条;hide 事件可能会稍晚一点触发,具体取决于 ghostTime 和 minTime 的设置。
Creator
CodeByZach
Size
1.4 MB
Stars
15.6K
Age
13y
Commits · 90 Days