惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
J
Java Code Geeks
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
U
Unit 42
B
Blog
宝玉的分享
宝玉的分享
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
博客园 - Franky
博客园 - 三生石上(FineUI控件)
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
云风的 BLOG
云风的 BLOG
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
博客园 - 叶小钗

Example blog

Ghost routing explained with routes.yaml examples — routes, collections & taxonomies Ghost redirects explained with practical redirects.yaml examples How to Serve JSON or XML Data in Ghost How to Change Your Default Post Template in Ghost Ghost Custom Theme Settings How to add Google Fonts to Ghost Themes How to Configure Ghost with Mailgun Ghost Theme Structure & Organization How to Make Custom Templates in Ghost Themes How to Install Ghost CMS via Ghost-CLI How to Add Syntax Highlighting to Ghost The best Ghost CMS hosting platforms Ghost 6.0 — Social Web and Native Ghost Analytics Ghost CMS Social web beta, email 2FA and more author social links How to keep custom design settings when updating your Ghost theme How to use the public preview card in Ghost How to add members-specific sections in Ghost How to customize the default content CTA in Ghost How to build a custom homepage in Ghost An overview of Ghost editor cards and and how they work Ghost CMS News: One-time payments & collecting sales tax How to customize the private site access template in Ghost How to add custom fonts in Ghost themes Ghost CMS News: Additional payment methods & internal linking How to use Code Injection in Ghost How to set meta data of your custom routes & collections in Ghost Ghost CMS News: TK reminders, ActivityPub and improvements How to format dates and use the date helper in Ghost Ghost CMS internationalization, custom fonts & spam protection Understanding and optimizing post and page settings in Ghost CMS
How to add a scroll-based progress bar to your Ghost CMS ...
Bright Themes · 2023-10-04 · via Example blog

Adding a scroll-based progress bar to your Ghost CMS site can greatly enhance the user experience. In this step-by-step tutorial, we will guide you through the process of creating a customizable scroll-based progress bar using HTML plain JavaScript and CSS.

We will do this from the Ghost CMS Admin, no need to edit theme files, everything can be done from Ghost Code Injection and will work no matter what theme you are using.

Let's explore how we can do this, starting with the HTML markup.

The Progress indicator element

The <progress> HTML element is a built-in tool for displaying the progress of a task or process. It allows you to visually represent the completion status using a progress bar. This is perfect for our purposes.

The <progress> element supports two essential attributes:

  • value: This attribute sets the current value of the progress bar. It represents the completion percentage and can range from 0 to the value specified in the max attribute.
  • max: This attribute defines the maximum value that the value attribute can reach. It represents 100% completion and determines the length of the progress bar.

Here's how our element will look like:

<progress class="progress-bar" value="0" max="100"></progress>

Style the progress indicator

The next step is creating the necessary CSS styles to define the appearance of the progress bar. Here's the minimum styling needed for the element:

.progress-bar {
    appearance: none;
    position: fixed;
    top: 0;
    width: 100%;
    height: 5px;
    background: transparent;
    z-index: 1000;
}

.progress-bar::-webkit-progress-bar { background: transparent; }
.progress-bar::-moz-progress-bar { background: var(--ghost-accent-color); }
.progress-bar::-webkit-progress-value { background: var(--ghost-accent-color); }

In the code above, we define the .progress-bar class to style our progress bar element. Adjust the CSS properties according to the desired appearance and layout of your Ghost CMS site.

The progress bar color will be based on --ghost-accent-color, which is your brand color defined in the Designs settings.

To bring the scroll-based progress bar to life we need to track the scroll event. For this we need JavaScript. Here's the necessary code:

const progressBar = document.querySelector('.progress-bar');

if (progressBar) {
  progressBar.setAttribute('value', getScrollPercent());  
  window.addEventListener('scroll', (event) => {
    progressBar.setAttribute('value', getScrollPercent());
  }, false);
}
    
function getScrollPercent() {
  const h = document.documentElement, 
        b = document.body,
        st = 'scrollTop',
        sh = 'scrollHeight';
  return Math.round((h[st]||b[st]) / ((h[sh]||b[sh]) - h.clientHeight) * 100);
}

The above code is responsible for handling the updates to the progress bar as the user scrolls through the page.

The getScrollPercent() function calculates the scroll percentage based on the current scroll position and the total height of the page. It returns the scroll percentage as a rounded integer value.

To ensure the progress bar updates dynamically, the scroll event listener is attached to the window object. As the user scrolls, the progress bar's value attribute is updated using the getScrollPercent() function.

Integrate the code in Ghost

At this point we have all we need to implement the feature, we just have to take everything and put it together.

Add the following in your Settings > Code injection header:

<style>
  .progress-bar {
    appearance: none;
    position: fixed;
    top: 0;
    width: 100%;
    height: 5px;
    background: transparent;
    z-index: 1000;
  }

  .progress-bar::-webkit-progress-bar { background: transparent; }
  .progress-bar::-moz-progress-bar { background: var(--ghost-accent-color); }
  .progress-bar::-webkit-progress-value { background: var(--ghost-accent-color); }
</style>

Add the following in your Settings > Code injection footer:

<progress class="progress-bar" value="0" max="100"></progress>
<script>
  const progressBar = document.querySelector('.progress-bar');

  if (progressBar) {
    progressBar.setAttribute('value', getScrollPercent());  
    window.addEventListener('scroll', (event) => {
      progressBar.setAttribute('value', getScrollPercent());
    }, false);
  }
      
  function getScrollPercent() {
    const h = document.documentElement, 
          b = document.body,
          st = 'scrollTop',
          sh = 'scrollHeight';
    return Math.round((h[st]||b[st]) / ((h[sh]||b[sh]) - h.clientHeight) * 100);
  }
</script>

At this point, you have successfully integrated a scroll-based progress bar into your Ghost CMS site.

However, you may want to further customize its appearance to align with your theme's design. Feel free to modify the CSS styles or add additional styling to the .progress-bar class. You can adjust the position, size, and colors of the progress bar by tweaking the corresponding CSS properties within the existing styles.

Additionally, you can make the progress bar appear only on posts, by adding the following in your Settings > Code injection header:

<style>
.progress-bar { display: none; }
.post-template .progress-bar { display: block; }
</style>

Conclusion

By following this comprehensive guide, you have learned how to effortlessly incorporate a scroll-based progress bar into your Ghost CMS site. You can fully personalize the appearance and functionality of the progress bar.

By providing visual feedback on the user's progress, you enhance the user experience and keep your audience engaged.