Scrolling an HTML element to a specific value within its content

Code:

/* -------------------------------------NAV & BANNER------------------------ */
#logo{
  height: 60px;
  width: 60px;
}
.nav-color{
  transition: 0.5s;
  background-color: rgba(0, 0, 0, 0.75);
}
.nav-null-color{
  transition: 0.5s;
}
.banner{
  background-image: url('');
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
  height: 100vh;
  width: 100vw;
  border-bottom: 1px solid #ff8000;
}
.title {
  background-color: rgba(0,0,0, 0.3);
  width: 50vw;
  border-radius: 25px;
}
.shadow{
  background-color: #ffffff;
  opacity: 25%;

}
#large-text{
  font-size: 60px;
}
/* --------------------------------About Us---------------------------------- */
.corporate{
  background-image: url('');
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
  width: 100vw;
  height: 100vh;
  border-bottom: 1px solid #ff8000;
}
.blank{
  background-color: rgba(255, 255, 255, 0.5);
  border-radius: 15px;
}
/* --------------------------------Worldwide---------------------------------- */
.worldwide{
  background: #ffffff;
  width: 81vw;
  border-bottom: 1px solid #ff8000;
}
.map{
  border-radius: 15px;
}
/* --------------------------------Careers Title---------------------------------- */
.careers{
  background-image: url('');
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
  width: 81vw;
  height: 100vh;
  border-bottom: 1px solid #ff8000;
}
/* --------------------------------Contact Us---------------------------------- */
.contact{
  background-image: url('');
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
  width: 81vw;
  height: 100vh;
}
.btn-lg{
  width: 335px;
}
/* --------------------------------Scroll Menu---------------------------------- */
.scroll-spy{
  /* position: -webkit-sticky; */
  position: sticky;
  top: 2rem;
  height: 10vh;
  border-right: 1px solid #ebebeb;
}
/* -------------------------------------------Footer----------------------- */
.footer{
  border-top: 1px solid #ff8000;
  width: 100vw;
  height: 60vh;
  background-color: #acb7ba;
  font-size: 12px;
}
#icon{
  font-size: 25px;
}
#youtube{
  height: 200px;
  weight: 200px;
}

I am currently working on making some adjustments to the lateral menu as it overlaps with the footer when scrolling down. I have attempted to address this issue using JQuery by changing the position property to relative once the scroll surpasses a specific value. This modification appears to be functioning correctly. However, I am encountering difficulties in reverting the position property back to sticky when the scroll value is below the designated threshold. Can you provide any insights or suggestions on what might be missing from my implementation? Your assistance is greatly appreciated. Thank you.

Answer №1

One effective way to boost script performance is to steer clear of hardcoded values and opt for dynamic calculations instead. Using window.scrollY for precise measurements in the scroll-spy function results in better optimization.

<script>
$(window).scroll(function() {
if ($("#menu").offset().top > 56) {
$("#menu").addClass("nav-color");
} else {
$("#menu").removeClass("nav-color");
}
$('#prueba').val(window.scrollY);
if (window.scrollY > 2500) {
$('#prueba2').val(1);
$('.scroll-spy').css('position', 'relative');
$('.scroll-spy').css('top', '1800px');
} else {
$('#prueba2').val(2);
$('.scroll-spy').css('position', 'sticky');
$('.scroll-spy').css('top', '2rem');
}
});

</script>

ENHANCED

To improve the script further, consider updating fixed values with calculated measurements. Include align-items-start in the parent container of .scroll-spy within the template structure.

<div class="row align-items-start">
<div class="col-2 scroll-spy" id="">

In the CSS section, remove height specifications for .scroll-spy. Check out the provided JavaScript snippet for more detailed guidance:

$(window).scroll(function() {
if ($("#menu").offset().top > 56) {
$("#menu").addClass("nav-color");
} else {
$("#menu").removeClass("nav-color");
}

let scrollSpyHeight = $('.scroll-spy').outerHeight();
let footerOffsetTop = $('.footer').offset().top;
let windowTop = jQuery(window).scrollTop();

$('#prueba').val(window.scrollY);
if ($('.scroll-spy').offset().top > (footerOffsetTop-scrollSpyHeight)) {
$('#prueba2').val(1);
$('.scroll-spy').css('position', 'relative');
$('.scroll-spy').css('top', '1800px');
} else {
$('#prueba2').val(2);
$('.scroll-spy').css('position', 'sticky');
$('.scroll-spy').css('top', '2rem');
}
});
/* -------------------------------------NAV & BANNER------------------------ */
#logo{
height: 60px;
width: 60px;
}
.nav-color{
transition: 0.5s;
background-color: rgba(0, 0, 0, 0.75);
}
.nav-null-color{
transition: 0.5s;
}
.banner{
background-image: url('');
background-repeat: no-repeat;
background-position: center;
...

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

Is there a way to set up ESLint for VSCode without relying on node or any

We have recently transitioned from the Atom editor to VSCode for teaching beginner JavaScript concepts. One challenge we are facing is that VSCode requires installation of node and then running npm install eslint, whereas in Atom, we could easily use the a ...

Angular 2: The *ngFor directive is unable to locate a suitable differing framework

Below is the code for client.service.ts clients: Client[]; getClientList() { let headers = new Headers(); headers.append('Content-Type', 'application/json'); let authToken = localStorage.getItem('auth_token&apo ...

Using the combined power of CSS and jQuery to create dynamic visual effects based

Having some trouble figuring out why this code isn't functioning as expected... I've got a bunch of DIVs that have the class .rightColumnButton. Some of them currently have a height set to 30px: .rightColumnButton{ height:30px; width:206px; mar ...

Tips on passing a User defined object to a controller using ajax/jquery:

This is an example of my HTML code: <input type="text" name="name" id="name" /> <input type="text" name="age" id="age" /> <input type="button" id="button" value="Submit"/> Here is the corresponding JavaScript code: <script t ...

Is there a way to synchronize breakpoint values across different media queries?

Currently utilizing Next.js version 9.53. Utilizing the built-in CSS module support provided by Next.js without the use of CSS pre-processors (less, sass, etc.) or CSS-in-JS (styled-components, etc.). Presently, my approach involves writing CSS in a mobi ...

I am looking to access a public method from a different component in Angular 2

Trying to access the headerExpand property from app.component is causing an error message in the console: metadata_resolver.js:559 Uncaught Error: Invalid providers for "Page1" - only instances of Provider and Type are allowed, got: [?undefined?] page1 ...

Discover the default text highlighting color of a browser with the use of JavaScript or Dart programming

Did you know that you can change the browser's default text highlight (selection) background color using CSS? For example: ::selection { background: #ffb7b7; } Find out more about the browser/OS specific default background color when selecting tex ...

Using Bootstrap, jQuery, and PHP to dynamically load input fields in a modal window

I am looking to dynamically load input fields in a modal window using PHP. While researching, I came across suggestions to utilize AJAX for this purpose. However, I have limited knowledge about AJAX. The plan is as follows: 1) When the user clicks on the ...

Utilizing Angular JavaScript for detecting and setting up JDK installations

I am working on an application that requires the installation of Java JDK. Therefore, I need to develop a detection function for JDK in the system. If it is not found, I plan to install it using a setup provided by me and also set it in the system variabl ...

HTML fields that are serialized as deeply nested JSON objects

Currently, I am working on a Java REST web service that handles POST data from a basic HTML form. My goal is to ensure that the client code sends the correct JSON to the web service in order for the server code to populate my objects accurately. The data ...

Load page content dynamically with Ajax in a specific div while still allowing the option to open the content in a new tab by right-clicking

As I work on developing a new website that utilizes a MySQL database to sort various items into categories and subcategories, I have implemented a script that dynamically loads category content into a div without requiring a page reload. This seamless load ...

Angular: Transforming a JSON Response to a JSONP Response

I'm currently using the openweathermap API to fetch JSON data, but I'm interested in utilizing JSONP data instead. How can I achieve this in my Angular service? app.factory('forecast', ['$http', function($http) { t ...

Enhanced JavaScript Autocompletion in Integrated Development Environments

What is the inner workings of Intellisense in IDEs like Webstorm and Eclipse for JavaScript? From which source do the suggestions originate? Is it possible to tweak the code to enhance the accuracy of the suggestions? https://i.sstatic.net/ZVBB3.png ...

Is there a way to execute a function immediately after an element has completed rendering?

I am facing a challenge where I need to dynamically set the height of one element based on the height of another element. Initially, when the target element finishes rendering, it's 490px tall. However, when I try to retrieve the height of the other ...

The Google map is not showing up on the screen despite having entered the API Key

Trying to showcase a Google map on my website. Check out the code below:- <script> function initializeMap() { var coords = {lat: -25.363, lng: 131.044}; var mapObj = new google.maps.Map(document.getElementById('mapz') ...

Press here to unveil and modify using the Redactor JS WYSIWYG HTML editor

I am working on a project where I have three separate divs that are set to be editable using the attribute contenteditable="true". Additionally, I have configured the redactor wysiwyg toolbar to remain fixed on the page. Here is the structure in HTML: &l ...

Tags for classes are not appearing or being activated on my website

I recently purchased a template from casethemes () and I am experiencing an issue. Despite using the same tags as their demo site, the classes are not being executed properly and appear broken on my website. Could it be that I am missing some necessary lib ...

Mastering the art of utilizing middleware in every HTTP request using Node.js Express

I am currently developing a middleware for my nodejs application and I need to include a header in the request within the middleware before sending it to my index.js endpoint. middleware1.js exports.mw1 = function(req, res, next) { next(); }; middlewa ...

Changing CSS properties in React

Currently, I am working on a dynamic table component using CSS grid. My goal is to customize the grid-template-columns CSS property. I am familiar with using repeat in CSS to achieve this. However, I am facing a challenge when it comes to dynamically chang ...

Transform an object into an array of objects by adding extra properties to each one

The following code snippet represents data in the form of an object with each key containing a date. The properties within the object include Open and Closed. If the value for Closed is 0, then that property is not included. let data = { "20 ...