show fixed div when in view

After seeking inspiration from this specific thread on SO, I still need some help.

The challenge I'm facing involves displaying a sticky footer within a parent div that is positioned halfway down the page. I've tried multiple tutorials, but none have worked for me.

Here is the structure of the page:

HEADER
HERO
CONTENT RIGHT-SIDE(id="wrap-vs")
CONTENT-FULL-WIDTH
FOOTER

My goal is to only show the sticky div within the RIGHT-SIDE content when it is within the viewport. Since RIGHT-SIDE is not visible on page load and requires scrolling to reach, the sticky div should appear when it is in view and disappear when the user scrolls below it.

var targetdiv = document.querySelector('.tabs');
console.log(targetdiv);
targetdiv.style.display = "none";

function CheckIfVisible(elem, targetdiv) {
  var ElemPos = elem.getBoundingClientRect().top;
  targetdiv.style.display = (ElemPos > 0 && ElemPos < document.body.parentNode.offsetHeight) ? "block" : "none";
}

window.addEventListener("onscroll", function() {
  var elem = document.querySelector('#wrap-vs');
  CheckIfVisible(elem, targetdiv);
});
#wrap-vs {
  height: 100%;
  background-color: yellow;
}

.tabs {
  position: fixed;
  bottom: 0;
}
<div id="wrap-vs">
  <div class="tabs">
    right-side content sticky div
  </div>
</div>

Answer №1

Here's the solution I implemented:

 

// Setting up a new observer
var observer = new IntersectionObserver(function (entries) {
    entries.forEach(function (entry) {
        // Check if the element is in the viewport and log it
    
        console.log(entry.isIntersecting);

        if(entry.isIntersecting == true){
            document.querySelector('.tabs').style.display = 'block';
        } else {
            document.querySelector('.tabs').style.display = 'none';
        }
        

    });
});

// Specifying the element to observe
var app = document.querySelector('#wrap-vs');

// Associating the observer with the element
observer.observe(app);
#wrap-vs {
  height: 100%;
  background-color: yellow;
}

.tabs {
  position: fixed;
  bottom: 0;
}
<div id="wrap-vs">
  <div class="tabs">
    right-side content sticky div
  </div>
</div>

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

Error: The object does not have the property createContext necessary to call React.createContext

I'm currently exploring the new React Context API in my app. I've also implemented flow for type checking. However, when I add // @flow to a file that contains the code: const MyContext = React.createContext() An error pops up stating: Cannot ...

The function was not invoked as expected and instead returned an error stating "this.method is not

I need help with an issue in my index.vue file. The problem lies in the fancy box under the after close where I am trying to call the messageAlert method, but it keeps returning this.messageAlert is not a function Can someone please assist me in resolving ...

Unable to Show JSON Data in jTable

My challenge involves integrating JSON data from a Laravel controller into a jQuery jTable for display. Although the table successfully receives the data upon request, it fails to display it as intended. The jTable is configured using the code below: $(& ...

Placing the word "repeatedly" in a d3 js tree

I am currently working on building a tree structure and incorporating edge labels. The tree allows nodes to be dynamically added using a plus button that appears when you click on a parent node. One issue arises when adding a new child. (apologies for the ...

The seamless integration of AngularJS and the chosen library is proving to be

When I use a chosen select to load data from a JSON file in an AngularJS application, the data loads successfully with a standard HTML select. However, if I switch to using the chosen select, the data does not load. I understand that the data is fetched af ...

The jQuery hover function triggers events on various elements

I'm struggling with a hover method in my code that is supposed to target only one image at a time and add a preview icon when hovered over. However, the issue is that the preview icon is being added to every element instead of just the one being hover ...

Breaking down a intricate JavaScript expression in order to reformat it into a different structure

As I explore the task of refactoring a legacy application, I find myself faced with complex JavaScript expressions stored in a database column. These expressions contain validation and conditional rendering logic that need to be translated into structured ...

What could be causing the error when trying to use a PUT request with fetch in Vue?

Whenever I attempt to send a PUT request, an error pops up on the console. export function putUserData() { fetch(`${url}/user/${getCookie("ID")}`, { method: "PUT", headers: { "Authorization": `B ...

AngularJS is experiencing issues with the sorting filter 'orderBy'

I am experiencing an issue with sorting a table list that has three columns. I have implemented the ability to sort all columns in ascending and descending order. However, when I click on the -Tag to initiate the sorting process, I encounter the following ...

Issue with VueJS where changes made to properties in the mounted hook do not properly trigger the

I'm working on a VueJS component that features a button with computed properties for its class and text. These properties change every time the button is clicked, updating smoothly with each interaction. However, I've encountered an issue when at ...

Error encountered: No geographic indices found for executing a geoNear operation with Mongoose

Initially, I had divided the schemas but later nested them inside my overall document. Despite adding indexes and removing coordinates from location, there seems to be an issue with the nested points. Upon running get Indexes, it shows that there is an i ...

problem with selection of date and month in dropdown list with jquery

Recently, I've been dabbling in jQuery and decided to create two list boxes using it. One box contains dates while the other contains months. It's quite handy since I need them in different sections of my HTML page. However, when attempting to en ...

Is it possible to execute a program on MacOS through a local HTML website?

Are there any straightforward methods to launch Mac programs using HTML? I've created an HTML page featuring a text field and several buttons. The goal is for users to enter a code (numbers) that will then be copied to the clipboard. By clicking on t ...

Modifying JavaScript Objects

After referring to the discussion at , I encountered a challenge with my javascript ajax function that retrieves JSON data. My goal is to trigger different js events based on the key-value pairs of the JSON response, but unfortunately, I am facing diffic ...

Jquery Flexigrid at your service

Can someone help me understand how to implement Flexigrid jQuery on my website? Your assistance is greatly appreciated. ...

What is the best way to remove text using javascript?

Hey everyone! I am new to coding in javascript and I'm hoping for some help with stripping text. I have a string that is formatted like this: <iframe src="http://embed.videokoo.com/61YVek?client_file_id=390856&width=720&height=480" style=" ...

Initially Missing Child Props in Parent Component

I am currently working on an application that utilizes a nutrition API to fetch information such as calories and more. One of the key features I am developing is the ability for users to set their daily calorie target along with the percentage breakdown fo ...

Inquiry about an IP board forum

When using an IP board forum, you may have noticed that you can easily click on the "quote" button for any message you wish to quote. Then, when you navigate to the "add reply" page, the quoted messages are automatically included in your response form. I ...

Tooltip positioned within a custom container using Material UI

Currently, as part of my Chrome extension development utilizing React and Material UI, I am implementing an inject page strategy. I have managed to set up a tooltip for the Fab button, but unfortunately, it appears outside the DOM of my extension. Upon ins ...

Tips for Making a JQuery Ajax Request Properly

Should I be chaining multiple Ajax calls together? While I understand that Ajax calls are asynchronous, I am faced with a situation where I need to initiate another call based on the success or failure of the first call. Here is an example snippet of cod ...