- This topic has 15 replies, 6 voices, and was last updated 2 years, 1 month ago by David.
-
AuthorPosts
-
February 25, 2021 at 8:31 am #1672458Mouse
Hi there, I need a bar that moves as we scroll up and down showing the progress of the article. Besides that, is there a way if i can add how much time would it take on average to read teh blog post
February 25, 2021 at 2:17 pm #1672888LeoStaffCustomer SupportHi there,
There isn’t an option built-in GP for this.
You will need to look for a plugin that offers this solution.
February 26, 2021 at 12:20 am #1673260MouseHi thanks for helping me out.
Sorry for bugging you lately but its just about the time where I need to fix up a few things before I finally make up my mind to renew the theme for the next year…
SO can this be done without the plugin? (with css probably)
February 26, 2021 at 12:51 am #1673291ElvinStaffCustomer SupportHi there,
CSS won’t be enough as you’ll definitely need a script that calculates the position of the scroll on the viewport and reflect it on some sort of a progress bar.
The easy way is to find a plugin that does this for you.
February 26, 2021 at 1:48 am #1673384MouseCan you please recommend a nice plugin for that? That is good (with fewer vulnerabilities) and working with this theme?
February 26, 2021 at 6:12 am #1673616DavidStaffCustomer SupportHi there,
try this:
1. Create a new Hook Element:
https://docs.generatepress.com/article/hooks-element-overview/
Add this to the hook content:
<div class="progress-container"> <div class="progress-bar" id="progressBar"></div> </div> <style> body { margin-top: 8px; /* match height of bar */ } </style>
Select the
before_header
hook
Set the Priority to1
Set the Display Rules to where you want the bar to appear. eg.Posts > All Posts
Publish the Hook2. Create a new hook.
Add this to your Hook content:<script> window.onscroll = function() {progressScrollBar()}; function progressScrollBar() { var winScroll = document.body.scrollTop || document.documentElement.scrollTop; var height = document.documentElement.scrollHeight - document.documentElement.clientHeight; var scrolled = (winScroll / height) * 100; document.getElementById("progressBar").style.width = scrolled + "%"; } </script>
Select the
wp_footer
hook
Set the Display Rules to match the first hook.
Publish.3. Add this CSS to your site:
.progress-container { position: fixed; top: 0; left: 0; width: 100%; height: 8px; /* height of bar */ background: #ccc; /* Background color */ z-index: 10000; /* increase z-index to bring in front of slider */ } .progress-bar { height: 8px; background: #4caf50; /* Progress bar color */ width: 0%; } /* adjust position of progress when admin bar is present */ .admin-bar .progress-container { top: 33px; }
Reference site for where the code comes from:
https://www.w3schools.com/howto/howto_js_scroll_indicator.aspFebruary 27, 2021 at 12:44 am #1674609MouseAlright this is working fine. (added to one of the link shown below) But it has some issues:
1- Shows a white bar on all the other pages not being implemented on.
2- I am using sticky slider with css which blocks it (it runs behind the sticky slider)February 27, 2021 at 7:00 am #1675047DavidStaffCustomer SupportI updated the 1. HTML and 3. CSS above to fix that.
February 27, 2021 at 8:47 am #1675158MouseThanks. That fixed all.
February 27, 2021 at 2:39 pm #1675465DavidStaffCustomer SupportGlad to hear that
March 5, 2021 at 4:26 am #1683011RamonI have a tip for when someone tries to use this code, but it doesn’t work because another plugin uses window.onscroll. You can also use an event listener:
<script> window.addEventListener('scroll', () => progressScrollBar()) function progressScrollBar() { var winScroll = document.body.scrollTop || document.documentElement.scrollTop; var height = document.documentElement.scrollHeight - document.documentElement.clientHeight; var scrolled = (winScroll / height) * 100; document.getElementById("progressBar").style.width = scrolled + "%"; } </script>
March 5, 2021 at 5:02 am #1683054DavidStaffCustomer SupportThanks for sharing the solution! Much appreciated
February 26, 2022 at 2:04 pm #2134882AdrianHi,
Sorry to hijack this post, but the question is 100% related to the method described here.
I have applied this solution successfully, although I would like to know if it is possible to introduce one modification.
I would like the progress bar to reflect the height of the post not the page. I mean that the progress bar shows 100% when it reaches the end of the post and not when it reaches the end of the footer. Is that possible?
Thank you in advance
February 27, 2022 at 5:50 am #2135275DavidStaffCustomer SupportHi there,
to do that ( properly ) would require a plugin or custom development.
The most i can offer is the ‘hackish’ method i provided here:https://generatepress.com/forums/topic/progress-bar-in-blog-posts/#post-2069098
February 27, 2022 at 7:42 am #2135362AdrianHi David,
I can live with the “hackish” method. I had to set the figure to a much higher number due to the design of the page, but it worked perfectly. Although the calculation may not be as exact as in some of the plugins I have tried, the truth is that this code is much lighter.
Many thanks. -
AuthorPosts
- You must be logged in to reply to this topic.