The issue with adjusting the top position in jQuery

As I continued scrolling on my page, I encountered an issue with adjusting the top offset using jQuery on my element.

I want the element with id goToLog to become fixed on the page after scrolling 80px. This functionality is working correctly. However, when I scroll past 260px, my Navbar also becomes fixed at the top of the page, causing the button goToLog to be positioned under the Navbar. Therefore, I need to adjust the top from .2rem to 70px.

I have attempted adding !important after the top value in jQuery and changing the top value in css to px, but neither method seems to have any effect.

$(document).ready(function() {
  $(window).scroll(function() {
    var scroll = $(window).scrollTop();
    if (scroll >= 80) {
      $('#goToLog').css('display', 'block');
      $('#goToLog').on('click', function() {
        window.scrollTo(0, 0);
      });
    } else if (scroll >= 260) {
      $('#goToLog').css('top', '70px');
    } else {
      $('#goToLog').css('display', 'none');
    }

    if (scroll >= 260) {
      $('#mainnav').addClass('stickynav');
    } else {
      $('#mainnav').removeClass('stickynav');
    }
  });
});
#info {
  height: 5rem;
  background: #fff;
  display: flex;
  justify-content: space-between;
  padding: 0 1rem;
  position: relative;
}

#goToLog,
#logform input[type=submit] {
  width: 150px;
  height: 39px;
}

#goToLog {
  display: none;
  position: fixed;
  z-index: 5;
}

nav {
  width: 100%;
}

.stickynav {
  position: fixed !important;
  top: 0px;
  background: #9C9FB3 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="info" class="container">
  <form id="logform" class="flex" method=POST action="">
    <div><input type="submit" name="login" value="Login"></div>
  </form><button id="goToLog" class="container" type="button">Login <span class="fas fa-arrow-circle-up"></span></button>
</div>
<header id="mainheader" class="parallax-header" data-parallax="scroll">
  <div style="height:150px"></div>
  <nav id="mainnav" class="">
    <ul class="unlist container">
      <li>nav item</li>
      <li>nav item</li>
    </ul>
  </nav>
</header>

If setting the display to block works in jQuery, why doesn't setting top to 70px work as expected?

Answer №1

The condition to set the top position at 70px is already met with if (scroll >= 80), so the subsequent condition is not executed.

Instead of using else if (scroll >= 260), I suggest nesting if (scroll >= 260) inside if (scroll >= 80). The revised code would appear as follows:

if (scroll >= 80) {
  $('#goToLog').css('display', 'block');
  $('#goToLog').on('click', function() {
    window.scrollTo(0, 0);
  });
  if (scroll >= 260) {
    $('#goToLog').css('top', '70px');
  }
} else {
  $('#goToLog').css('display', 'none');
}

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

How can I extract the "href" attribute from an anchor tag using XPath?

I am working with HTML code that looks like this: <a href="/images/big_1.jpg" class="class-a"> <img class="class-img" src="/images/small_1.jpg"/> <span class="class-span"> <img src="/images/img_1.png"> </ ...

Shuffle the elements within each container in the DOM

Currently, I am focusing on designing a card-based layout where each card contains details about the product. One important detail is the phone number, which is displayed in the format 555-555-5555. My current goal is to clear the text value of the phone ...

Bringing in typefaces to enhance your email signature

I am trying to design a unique email signature with a custom Adobe font. My email account is hosted by Strato, a German mail server. However, when I attempted to insert the signature using Apple Mail, I realized that the CSS <style> part was not bein ...

Only switch a radio button when the Ajax call results in success

Within an HTML form, I am working with a group of Radio buttons that trigger an Ajax call when the onchange() event is fired. This Ajax call communicates with the server to process the value sent by the call. The response can either be a string of "succes ...

Showing a hidden div after an unsuccessful AJAX call

Encountering an issue with displaying a div notification using the code snippet below: $.ajax({ url: url, cache: false, async: false, success: function (data) { response = jQuery.parseJSON(data); }, error: functi ...

How can you find the "et-custom-css" section in WordPress?

One of the challenges I'm facing with my WordPress page is that some CSS classes are located under <style type="text/css" id="et-custom-css">. Additionally, there is a comment at the top of the CSS saying: /* - - - - - - Additional page info - ...

The jQuery template fails to render any content on the page

Recently, I've been exploring jQuery Javascript Templates and trying to follow a tutorial that involves fetching tweets from Twitter. The tutorial can be found here: . After carefully implementing the code as instructed, you can view my progress on t ...

Having trouble with JQuery's $.post function not functioning on your web server?

I'm encountering an issue with my script that uses Ajax to send a request to my PHP file for updating a txt file. Interestingly, the script works perfectly fine on my localhost but fails to work on the webserver. Testing HTML code: <!DOCTYPE html ...

Tips for utilizing OpenLayers.Control.SelectFeature with a KML layer positioned below the Layer.Markers

I stumbled upon an intriguing issue and decided to share my solution in hopes of receiving better suggestions from others. Context: This involves an OpenLayers map with 3 layers: Google (base layer), a KML layer displaying polygons, and a Layer.Markers l ...

What is the purpose of utilizing "({ })" syntax in jQuery?

What is the purpose of using ({ }) in this context? Does it involve delegation? Can you explain the significance of utilizing this syntax? What elements are being encapsulated within it? For instance: $.ajaxSetup ({ // <-- HERE error: fError, ...

Utilize a personalized container for Bootstrap 4 dropdowns

I'm currently working on a Megamenu using Bootstrap 4 dropdown functionality. My goal is to implement a custom container like <div class="dropdown-container"> to control the visibility of the dropdown content, instead of relying on Bootstrap&ap ...

Remove a row from a table and implement a Bootstrap modal for confirmation

I am currently working on a task in my CodeIgniter framework project where I need to delete a row from a table of users. The desired functionality is to click the delete button, have a modal window appear asking the user for confirmation to delete the rec ...

Send a bunch of checkbox values to the controller upon clicking in order to apply a filter to a query using

How can I filter the number of students by campus, year, and group using checkboxes? I have successfully filtered the result by Campus using this code, but the uncheck functionality is not working as expected. It still sends the checked value. I need to ...

CSS - stacking containers vertically with scrollbars

I am in need of a vertical stack of blocks, which may include scrolls, contained within a fixed-sized container without scrolls. The number of visible blocks is dynamically managed by JavaScript code. Some blocks can be hidden or shown, and when a block is ...

Display the second dropdown after the user has made a selection in the first dropdown

Recently, I've been delving into the world of html/javascript and navigating through the process of implementing the code found in this specific link. While I can see the solution outlined in the link provided below, I'm struggling with how to i ...

Using Javascript to Highlight a Single Row in a Table

Greetings esteemed members of the skilled community at StackOverflow, I must humbly ask for your expertise in solving a dilemma that I am currently facing. The situation is as follows: I have a table generated from an SQL query, and it is crucial for the ...

The issue of selecting multiple classes simultaneously is not being resolved

Here is my JavaScript code snippet: $('.normal-box').click(function(){ //unique row id var id = $(this).data('row-id'); //its index according to the content array var index = $(this).data('index'); //which car ...

Combine and blur multiple background images using CSS blending techniques

I am currently working on a website, which is just a demo built in ReactJS: The issue I'm facing is related to the background. The idea behind the app is simple - it consists of 4 parts, with the top part displaying weather information without a bac ...

Executing Python script via AJAX or jQuery

I need to execute Python scripts from JavaScript. The goal is to provide an input String to the Python script as an argument and then showcase the output on our webpage. Below is the Python code that runs smoothly on my Linux box: ./sample.py 12345 produc ...

Using the ng-class directive to dynamically set classes based on the value returned from

I am facing an issue with setting the icon style based on a value returned from the controller. The console log confirms that the value is triggered correctly, but it appears that there is a problem with the Ng-class expression. Any assistance on this matt ...