Conceal a div once the user scrolls to a certain position using vanilla JavaScript

As a beginner in the field of web development, I am currently working on creating a blog website.

Code Function - One of the challenges I'm facing is implementing a floating pagination bar that needs to be hidden when a visitor scrolls near the footer of the page.

My code and what I've attempted so far-

I have tried using JavaScript to calculate the position of the footer element on the page and then determine when to hide the pagination bar based on the visitor's scroll position. Here is a snippet of my code:

var footerPos = document.querySelector("footer").offsetTop;
var partOfScreen = screen.height * 0.08;
var pointToHide = footerPos - screen.height + partOfScreen;

console.log(`footerPos- ${footerPos} ; pointToHide- ${pointToHide} ; screen.height - ${screen.height}`);

window.onscroll = function() {
  if (window.scrollY > pointToHide) {
    document.getElementById("pagination").style.transform = "scale(0)";
  } else {
    document.getElementById("pagination").style.transform = "scale(1)";
  }
}
<ul class="pagination pagination-rounded justify-content-center" id="pagination" style="
              position: fixed;
              bottom: 4vh;

              transform: scale(1); 
    /*this is the property which should change to - scale(0) - on scroll*/

              margin: 0 auto;
              z-index: 1000;
              transition: all 0.2s, transform 0.2s cubic-bezier(0.5, 0, 1, 2);
          ">

  <li class="page-item" style="border-radius: 50px;-webkit-box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 1);-moz-box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 1);box-shadow: 1px 1px 3px 0px rgba(0, 0, 0, 0.8);">
    <a class="page-link" href="#" onclick="window.history.back()" style="border-radius: 55px;">
      <i class="int-arrow-left"></i>
    </a>
  </li>

  <li class="page-item active" style="border-radius: 50px;-webkit-box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 1);-moz-box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 1);box-shadow: 1px 1px 3px 0px rgba(0, 0, 0, 0.8);">
    <a class="page-link" href="#">1 </a>
  </li>

  <li class="page-item" style="border-radius: 50px;-webkit-box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 1);-moz-box-shadow: 2px 2px 5px 0px rgba(0, 0, 0, 1);box-shadow: 1px 1px 3px 0px rgba(0, 0, 0, 0.8);">
    <a class="page-link" href="https://www.zealforgood.com/test-categories?index=9" style="border-radius: 55px;">
      <i class="int-arrow-right"></i>
    </a>
  </li>

</ul>

In the above <script>

The script involves calculating the position of the footer element and setting up an event listener for scrolling to determine when to hide the pagination bar near the footer. I have shared the live source code link for reference.You can see this code live on this website's source

Despite several attempts, I have not been able to achieve the desired functionality. I would appreciate any help or suggestions to troubleshoot the issue as I am more comfortable with plain JavaScript rather than jQuery.

Answer №1

After reviewing your code, I believe it to be functional. The only issue I encountered was the absence of a footer element in the provided snippet, causing it not to work correctly. However, upon adding a footer and scrollable text, the code functioned properly, albeit lacking in CSS styling.

Upon visiting the specified website, I observed that the code did not perform as expected.

The root of the problem appears to lie in other sections of the code that are also implementing event listeners for scrolling actions. By inspecting the Event listeners section in your browser's developer tools, I noticed a total of 4 listeners active. Implementing a console.log within the test-categories portion of your code revealed that it was never reached due to these additional event listeners.

Removing the initial event listener (in jquery) from the list resolved the issue, allowing your code to function correctly. As you scroll down the page, the pagination list disappears as you approach the footer.

Regrettably, my knowledge of jquery is limited, and the minified source code complicates efforts to identify where and why this particular event listener is established.

If I had sufficient reputation, I would have posted a comment on your question instead of providing an incomplete answer. Hopefully, the information about event listeners points you towards a solution.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

react-video-recorder` facing challenges with loading

I recently integrated react-video-recorder into my application. After checking out the demos on Stackblitz and Storybook hosted on Vercel, I was impressed with how well it worked in my browsers. However, when I added it to my codebase, the component would ...

Having trouble with loading JavaScript during ng build --prod process

The JavaScript file I'm using for multiple emails (multiple_emails.js plugin) works well with ng serve. Here is my code: (function( $ ){ $.fn.multiple_emails = function(options) { // Default options var defaults = { ...

Having trouble with Javascript files failing to load while hosting a website on digital ocean?

Recently, I developed a web application using an express backend and the ejs view engine. Everything works perfectly fine when tested on my local machine. However, I encountered issues when trying to host it on a digitalocean droplet (Ubuntu 22.10 x64). Af ...

jQuery Mobile panel buttons are functioning properly within a maximum width of 300 pixels

I have constructed a panel that adjusts its size based on the device's screen using responsive design techniques. The code for this panel is as follows: <div data-role="panel" data-position="right" data-display="overlay" data-theme="a" id="add-for ...

Tips for merging two text boxes in a form with CSS

Is it possible to combine two text boxes in a form using CSS, similar to the design on Tumblr's login page at ? If CSS is not the solution, what alternative methods can be used? Any assistance would be greatly appreciated. I am aiming to enhance the ...

Is caching a feature in AngularJS, and are there methods available for disabling it?

var modalInstance = $modal.open({ templateUrl: '/template/edit-modal.html', controller: ModalInstanceCtrl2, resolve: { locations: function () { return locationToEdit; } }, scope: $scope.$new() }); ...

Synchronous CORS XHR encounters issues with 302 redirect, while asynchronous function successfully redirects

Why does Firefox seem to be the only browser that doesn't throw an error when performing a synchronous request? Any insights on this peculiar behavior? // Ensure your JS console is open when running this script var url = '//api.soundcloud.com/ ...

Passing image source from parent component to child component in Vue.js

I encountered an issue where I stored the image file name in a variable within the parent component and passed it to the child component using props. However, despite this setup, the child element is not displaying the image as expected. Here is the data ...

Is there a way to prevent my token from being exposed when making an AJAX call?

When working on my HTML file, I encountered an issue with a Python Django URL integration where I need to pass a token to retrieve certain information. However, the problem is that my token is exposed when inspecting the page source in the HTML document. ...

Load information from a JavaScript object when the user clicks dynamically

My challenge involves utilizing a JavaScript object that contains video information such as title and source URL. Within my HTML, I have image placeholders and the goal is to trigger a modal pop-up (using Twitter Bootstrap modal) of the specific video when ...

Choosing from a dropdown menu by pressing keys

When I press the first letter of an option in my dropdown list, it works fine. However, if I try to press two, three, or four letters consecutively, the control vanishes. For example, if the option is 'Jquery' and I press J only, it works but pre ...

What is the best way to access a nested promise element in Protractor?

I've recently put together a function in protractor that I'd like to share: function findChildElementByText(parentElement, tagName, textToSearch) { return parentElement.all(by.tagName(tagName)) .then((items) => { items.map( item ...

The concept of transparency and visual representation

Is there a way to prevent the opacity of the parent div from affecting the opacity of the img? <div class="parent_div" style="opacity:0.2"> <p>Some text</p> <img class="gif" style="opacity: 1;" src="ht ...

Access the child component within an @ChildComponent directive in Angular

Is it possible to retrieve child components of another component? For instance, consider the following QueryList: @ContentChildren(SysColumn) syscolumns: QueryList<SysColumn>; This QueryList will contain all instances of the SysColumns class, which ...

Change the color of the first element when hovering over any of its siblings using only CSS

I am looking for a solution using only CSS We have 3 circles here. When I hover over circles with the class name "Mycircle", the circle with the class name "BigCircle" should change to red color HTML <div class="BigCircle"></div> <div cl ...

``How can I easily navigate to the top of the page when changing routes in React Router DOM v6?

What is the best way to scroll to the top when a route changes in react router dom v6? In the past, I used a solution from this StackOverflow post to make my page automatically scroll to the top every time a route changed with react-router-dom v5. However ...

Enable and disable subscriptions in real-time to control the amount of cached data and prevent the error message "Uncaught TypeError: Converting circular structure to JSON"

In an attempt to control the cache on the client side, we had the idea of toggling the subscription to a specific Collection on and off by placing the Meteor.subscribe call within a reactive context as recommended in the Meteor documentation - "In addition ...

Accessing state in Vuex modules is crucial for managing and manipulating data effectively

Feeling a bit lost here... I'm utilizing Vuex in my project and have a module called common stored in the file common.js: const initState = { fruits: [] } export default { state: initState, mutations: { SET_FRUITS(state, fruits) { cons ...

getting v-model value updated when clicking button

How can I update the value of v-model DataJournals.Description in a looping data when a button is clicked? This is what I have attempted: template <tr v-for="(DataJournals, index) in DataJournal" :key="DataJournals.value"> <td> & ...

The React useEffect hook runs whenever there is a change in the state

Within my React component, I have the following code snippet (excluding the return statement for relevance): const App = () => { let webSocket = new WebSocket(WS_URL); const [image, setImage] = useState({}); const [bannerName, setBannerName] = use ...