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

推荐订阅源

博客园_首页
阮一峰的网络日志
阮一峰的网络日志
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Threat Research - Cisco Blogs
P
Privacy & Cybersecurity Law Blog
The Hacker News
The Hacker News
H
Heimdal Security Blog
W
WeLiveSecurity
L
LINUX DO - 热门话题
Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
The Last Watchdog
The Last Watchdog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
D
DataBreaches.Net
I
Intezer
Webroot Blog
Webroot Blog
C
Cisco Blogs
AWS News Blog
AWS News Blog
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
罗磊的独立博客
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
Schneier on Security
Schneier on Security
宝玉的分享
宝玉的分享
博客园 - 叶小钗
PCI Perspectives
PCI Perspectives
D
Docker
Scott Helme
Scott Helme
NISL@THU
NISL@THU
J
Java Code Geeks
B
Blog RSS Feed
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Exploit Database - CXSecurity.com
AI
AI
美团技术团队
Cloudbric
Cloudbric
月光博客
月光博客
P
Proofpoint News Feed
T
Tailwind CSS Blog
Google DeepMind News
Google DeepMind News
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

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 Trigger the Ghost Portal popup based on page scroll progress How to Deploy Your Ghost Theme with GitHub Actions How to create a custom recommendations page for your Ghost CMS theme Ghost CMS News: New editor, refreshed settings & recommendations Ghost CMS News: Source - the new default theme and native image editing How to Add Different Logos for Dark & Light Themes in Ghost Upgrade to Node v18 for Ghost CMS & common errors Ghost Snippets: Adding Schema markup in Ghost How to Create a Custom Navigation Template in Ghost How to Segment Subscribers with Member Labels in Ghost The best way to create a contact page in Ghost How to Use the Ghost Subscription Forms in Your Theme & Posts How to update your Ghost site URL when self-hosting Ghost CMS News: Signup cards and better landing pages How to Add Breadcrumbs to Your Ghost Theme How to integrate the Applause Button into your Ghost theme How to Hide a Post From the Home Page in Ghost CMS Ghost CMS block and contentFor helper overview & tips How to add Image Lightbox to Ghost Themes Ghost CMS News: Signup form embed, new editor and bulk actions. Biron Themes becomes Bright Themes 🎉 How to Audit & Test a Ghost Theme How to Create Related Posts Section in Ghost How to Use Ghost Filter Expressions Ghost CMS newsletter settings and post analytics How to Update Your Ghost Theme When We Release New Versions Ghost CMS News: Automatic list cleaning & email troubleshooting How to Create a Featured Post Slider in Ghost How to Add Reading Time to Ghost Blog Posts How to Create a Post Archive for Your Ghost Theme Ghost CMS Price Helper Overview & Tips Self-Hosted Ghost CMS with DigitalOcean Ghost CMS News: Member Growth Sources & Audience Feedback How to Open External Links in a New Tab in Ghost How to Integrate Google Analytics with Ghost Ghost CMS News: History Log & Newsletter Analytics How to Automatically Add Member Labels From Referral Traffic in Ghost? Ghost CMS News: Free trials & email notifications Ghost CMS Match Helper Usage & Tips Ghost CMS News: Native Comments Ghost CMS News: Ghost Explore & Premium Newsletter Previews Ghost Snippets: Custom Progress Cards & Steps Ghost CMS News: Native Search The Best Ghost CMS Search Plugins & Libraries Ghost Snippets: Custom Stylish Tables How to Add a Scroll Top & Progress Indicator Button in Ghost Ghost Snippets: Accordions How to Add Tag Dropdown to Your Ghost Theme Ghost 5.0 released 🎉 Ghost Snippets: Custom Subscribe Forms Ghost Snippets: Custom Alert Boxes How to Show Post or Newsletter Issue Number in Ghost How to Self-Host Google Fonts in Ghost CMS Ghost CMS News: Membership, Tiers & Activity Feed Custom Pages in Ghost CMS Themes Ghost CMS Search Using Fuse.js Ghost CMS News: Offers and Discounts & New Cards Ghost CMS News: Membership tiers, email CTA & filters Ghost CMS News: Grammarly Support & Feature Image Captions. Ghost CMS News: Improved Member Accounts & Personalized Newsletter. Ghost 4.0 Launched With Native Newsletters & Membership Ghost CMS News: Cancellation Feedback & Newsletter Stats. Ghost CMS News: Improved Member Dashboard & Sidebar Views. Ghost CMS News: Google Pay support, email newsletters & secondary navigation Ghost CMS News: Member Support, Portal & Content Snippets. Ghost CMS News: Referral Programs & Responsive Images. Ghost Themes as Progressive Web Apps Ghost 3.0 is here with brand new features Ghost 2.0 is here with new features How to Add Search to Ghost Using ghostHunter
How to add a scroll-based progress bar to your Ghost CMS site
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.