Menu with sticky scroll disappears when the position is offset by 500 pixels from the bottom

I am working on a sticky menu feature that appears when the user scrolls down 500px from the top of the view. I would like it to also hide when the user reaches 500px from the bottom of the page.

Any suggestions or tips are greatly appreciated.

var stickTop = $('.sidebar-stick').offset().top + 500;
$(window).scroll(function(){
    if( $(window).scrollTop() > stickTop ) {
        $('.sidebar-stick').css({opacity: '1'});
        $('.stick-dummy').css('display', 'block');
    } else {
        $('.sidebar-stick').css({opacity: '0'});
        $('.stick-dummy').css('display', 'none');
    }
});

Answer №1

To enhance the functionality, considering both top and bottom limits for calculation is recommended. By adding the bottom limit condition as well, the accuracy of the result can be improved:

var stickTop = $('.sidebar-stick').offset().top + 500;
var stickBottom = $(document).height() - 500;

$(window).scroll(function(){
    if( $(window).scrollTop() > stickTop && $(window).scrollTop() < stickBottom) {
        $('.sidebar-stick').css({opacity: '1'});
        $('.stick-dummy').css('display', 'block');
    } else {
        $('.sidebar-stick').css({opacity: '0'});
        $('.stick-dummy').css('display', 'none');
    }
});

For more customized effects, it might be beneficial to consider the window height in your calculations as well.

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

External script implementing StratifiedJSIt is possible for an external script

Recently, I stumbled upon the amazing StratifiedJS library that offers many features I find essential. It functions flawlessly when implemented directly in my HTML file, like so: <script src="libraries/stratified.js"></script> <scr ...

Guide to periodically updating and displaying a <b-img> element in VueJS

Recently delving into the world of JS and Vue. Within my Bootstrap-Vue project, there's an image element that looks like this: <b-img src="/api/camera" fluid alt="camera"></b-img> Whenever the page is refreshed, the br ...

Issue with creating a new jstree node

I recently put together a basic jstree. When I click on a link, it triggers the createNode() function. This function utilizes the create feature from the API to generate a new node. It appears to be a common issue with jQuery that I am encountering. Any ...

What is the process for triggering a sorted output from my MongoDB database by simply clicking a button?

I'm struggling with sorting my collection by rating in my code. I have this snippet where I'm sending my collection to index.ejs: router.get('/', (req, res) =>{ FILM .find().limit(6) .then(films => res.render(& ...

Rearranging the layout of the image in bootstrap

Greetings, I have a question that may seem amateurish, but I am facing some difficulties. Below is the CSS code I am using: body{ background-image: url(street-photography1.jpg); background-size: cover; color: white; background-repeat: no-repeat; backgr ...

Responsive Gallery Grid Slider with uniform height boxes

Looking to develop a responsive gallery grid slider with equal height boxes. For instance, at 650px wide or wider, display 3 columns and 2 rows. When the width is 550px or less, switch to 2 columns and 3 rows. If the width is 450px or less, go down to 1 ...

Is there a reason we are able to simultaneously assign the disabled and selected attributes to <option> elements?

What is the rationale behind permitting the simultaneous setting of disabled and selected attributes on <option> elements, knowing that their values will not be submitted to a server? I was surprised that I did not receive an error in VS Code when I ...

How to Style an Element Based on Fragment Identifier in Href Attribute Using CSS

I've implemented the tabview feature from the YUI3 library. This is how the markup code appears: <div id="demo" class="yui3-tabview-content"> <ul class="yui3-tabview-list"> <li class="yui3-tab yui3-widget yui3-tab-selected"> ...

Looking for a way to trigger a function on JSTree's refresh event

I have searched through many posts here but cannot find what I am looking for. The version of JSTree that I am using is 3.3.3. My goal is to select a node after a create_node event triggers an AJAX request. Subsequently, JSTree fires the rename_node eve ...

Interact with the database using jQuery Ajax in Rails 3

Currently, I am in the process of developing an estimate form that can accommodate multiple line items. Once a line item is completed by inputting data for the following fields: properties height letter_quantity The goal is to fetch a unit price from a ...

Element is not being overlapped by higher z-index

#navbarContentHamburger { background-image: url(../img/657904-64.png); background-size: contain; z-index: 3; position: fixed; width: 22px; height: 20px; top: 7.7px; left:8px; } .open { width: 4% !important; transiti ...

jQuery Error: xxx has not been defined as a function

This error is really frustrating me. Every time I try, I get the message "TypeError: $(".yoxview").yoxview is not a function". Any ideas on what might be causing this issue with my code or formatting? <script type="text/javascript"> $(docum ...

Utilize regular expressions on a JSON response dataset

Imagine a scenario where a client makes a GET request and the response is in JSON format similar to the following: var result = { "enabled": true, "state": "schedule", "schedules": [ { "rule": { "start": "2014-06-2 ...

Is there a way to track and observe the vertical movement of an element within a directive's link function?

Can you help me figure out the best way to $watch or bind the height position of an element? I have a scenario where I display three divs, one at a time in different tabs (using bootstrap). Each div has a different height, and my directive comes after thes ...

Curious as to why JavaScript restricts the use of two variables entering at the same time

Currently, I am tackling an Ajax-php livesearch programming challenge. I am facing an issue where JavaScript is unable to receive the values of 2 parameters that I want to input in HTML input boxes. Any idea why this might be happening? This is my HTML co ...

Problem Encountered with Navigation Bar following the Cloning and File Transfer in a React (Next) Project

I cloned the repository and transferred the files using these commands: 1. git clone https://github.com/adrianhajdin/portfolio.git 2. cd portfolio 3. mv * .. Upon executing npm run dev, everything appears fine, but upon clicking on the About button or ...

There is a lack of CSS import in the HTML document

I have organized my files in the following structure: - index.html - css/styles.css This is the HTML code I am using: <!DOCTYPE html> <html lang="en> <head> <title>Bootstrap Case</title> <meta charset="utf-8"> & ...

Unable to store object data within an Angular component

This is a sample component class: export class AppComponent { categories = { country: [], author: [] } constructor(){} getOptions(options) { options.forEach(option => { const key = option.name; this.categories[k ...

What are some effective methods for organizing HTML in a clean and orderly manner?

I have limited experience with HTML, but I need to layout a form in a precise manner similar to WinForms. Here is my attempt using the little CSS and HTML knowledge I possess: The issue lies in everything inside the white area being ABSOLUTELY LAID OUT .. ...

React: The 'Redirect' function is not included in the export list of 'react-router-dom'

When I try to run npm run start in the terminal, an error message appears. 'Redirect' is not exported from 'react-router-dom', causing an import error. I have attempted various solutions like reinstalling node_modules, react-router-d ...