As I continue scrolling downward, a new element is added to the parent element. However, if I scroll back up, how can I revert the element back to its

Exploring a sticky navigation feature using jQuery. Click the link below to see the navigation and add to cart button in action:

The navigation sticks to the top along with the add to cart button (which is not a child element of the navigation but may be appended as the last child) when scrolling down.

As you scroll up, the navigation returns to its original position while the add to cart button remains in place.

To handle this functionality, here is the code snippet:

<script>
jQuery.noConflict();
jQuery(window).scroll(function() {
var height = jQuery(window).scrollTop();

if(height  > 150) {
    jQuery('.header-minicart').appendTo('#custommenu');
    jQuery('#header-nav').addClass('fixed-menu');
    jQuery('.header-minicart').addClass('fixed-minicart');

}else{
    /*Enter your code for the up-scroll behavior here */
    jQuery('#header-nav').removeClass('fixed-menu');
    jQuery('.header-minicart').removeClass('fixed-minicart');

}
});

</script>

<style>
.fixed-menu{ 
    position: fixed;
    left: 0px;
    top: 0px;
    z-index: 9999999999;
    width: 100% !important;
}
.fixed-minicart{ 
    display: inline-block;
    position: absolute;
    right: 0;
    top: 0;
}

This is the HTML code for the add to cart button:

<div class="header-minicart">              
        <a href="-" data-target-element="#header-cart" class="skip-link skip-cart  no-count">
        <span class="icon"></span>
        <span class="label">Cart</span>
        <span class="count">0</span>
        </a>
    </div>
    <div id="header-nav" class="skip-content">
    <div class="nav-container" id="wp-nav-container">  
    <div id="custommenu" class="" style=""><div class="menu">
    <div class="parentMenu menu0">
        <a href="-">
            <span>Home</span>
        </a>
    </div>
        ...
    <div class="clearBoth"></div></div>
    </div>
    </div>

You can reposition the add to cart button by inserting it before the .clearBoth element.

Answer №1

Here is a suggestion for the code you can use:

<script>
jQuery.noConflict();
jQuery(window).scroll(function() {
var height = jQuery(window).scrollTop();

if(height  > 150) {
    jQuery('.header-minicart').appendTo('#custommenu');
    jQuery('#header-nav').addClass('fixed-menu');
    jQuery('.header-minicart').addClass('fixed-minicart');

}else{
    jQuery('.account-cart-wrapper').append(jQuery('.header-minicart'));
    jQuery('#header-nav').removeClass('fixed-menu');
    jQuery('.header-minicart').removeClass('fixed-minicart');

}
});

</script>

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

What steps can you follow to allow Form Control to accept empty fields while rejecting all white space characters?

In my current project, I'm working on validating a field in a reactive form. The tricky part is that the field can be left blank as it's not mandatory, but it shouldn't consist only of white space characters. To tackle this issue, I decided ...

Ways to verify the content within two separate p elements

<p> expanded state:</p> <P> true </p> Merging the text from both tags into a single output. Final Output: expanded state: true ...

Learn how to retrieve the TextBox value from a button click within a Nested Gridview

I am trying to extract the value of a textbox inside a Nested Gridview using jQuery in ASP.NET. When a Button within the Nested Gridview is clicked, I want to display the textbox value in an alert box. Here is an example setup: <asp:GridView ID="Grid ...

Unable to make a POST request with AngularJS

Hello, I am currently experiencing some issues while trying to post data with AngularJS. Below is my JS code: var app = angular.module('myApp', []); app.constant('appUrl', 'http://localhost/zara/rest/'); /*---- categ ...

Using Angular 4 to import an HTML file

I am trying to save test.svg in a component variable 'a' or svgicon.component.html. To achieve this, I have created the svgicon.component.ts file. However, it's not working. What steps should I take next? svgicon.component.ts import ...

How come the background is appearing beneath the footer on Qualtrics?

I'm puzzled as to why my background appears under the footer in Qualtrics. This is the code I've written: <br /> <style type="text/css">body { background-image:url('https://uwmadison.qualtrics.com/CP/Graphic.php?IM=IM_00pfOKS ...

Adjust the dimensions of a Three.js generated 3D cube dynamically during execution

Is it possible to dynamically change the dimensions (width/height/length) of a 3D Cube created with Three.js? For example, I found a Fiddle on Stack Overflow that changes the color of a Cube at runtime: http://jsfiddle.net/mpXrv/1/ Similarly, can we modi ...

Guide on updating location and reloading page in AngularJS

I have a special function: $scope.insert = function(){ var info = { 'username' : $scope.username, 'password' : $scope.password, 'full_name' : $scope.full_name } $http({ method: &ap ...

Watch YouTube videos without having to open a new tab

When I open the href link in this fiddle in a new tab by using ctrl + click: https://jsfiddle.net/fNPvf/30636/, the video is opened in a new tab but does not play unless I give focus to that tab. How can I make the youtube video play automatically when ope ...

Prevent ajax requests from piling up using Jquery

There are two possible approaches to this situation. One is from the perspective of presentation, and the other is from a processing standpoint. The current setup involves a table of data. Various actions (such as typing a search query or clicking on diff ...

SASS causing conflicts with CSS breakpoints selector overrides

I am working with a partial file named _display.scss. It includes various @mixin and styling classes related to the display CSS property. _display.scss @mixin d-block{ display: block; } @mixin d-none{ display: none; } .d-block{ @include d- ...

Jquery's Ajax Promise fails to execute

I'm developing an app and encountering difficulties with firing a promise. I simplified the code to a basic example that is still not functioning, utilizing EasyPHP... ajaxDialog = function( target ) { // Promise to indicate completion return ...

Using jQuery to trigger events depending on the Hash portion of the URL

Hello everyone, I am relatively new to javaScript/jQuery and still learning the basics. I recently discovered that it is possible to change the hash of a URL onClick. However, I am curious if there is a way to addClass/removeClass from an element, hide/sh ...

Could someone assist me with rearranging blocks in Squarespace by utilizing CSS?

Greetings, fellow readers! After a period of silent observation, I am finally making my presence known. My wife and I are in the process of creating her freelance services website. However, we have encountered a troublesome issue that has left me quite pe ...

obtaining an HTML element within a blank div using either jQuery or Knockout.js

I need help with a functionality involving 3 buttons. Each button, when clicked, should add a specific container to an empty div located outside of the buttons. <button id="a" data-bind="clickme" value="1">a</button> <div id="empty1"> & ...

Encountering an issue while trying to create a user in Firebase

I am currently following a tutorial on Vue.js from Savvy Apps, which utilizes Firebase with Firestore. As the tutorial mentions that Firestore is still in Beta, I anticipate potential changes - and it seems like that might be happening in this case. While ...

How can Angular 7 incorporate inline JavaScript scripts in a component?

I am facing an issue while trying to integrate the places.js library into my Angular 7 project. I have added the necessary script in my 'index.html' file as follows: <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-prot ...

PHP receives no content with jQuery AJAX POST request

I've been hesitant to ask, but I'm at a loss for finding a solution. I used to think it was just a simple mistake, but I can't seem to locate it. Here is the jQuery 2.1 ajax POST I have set up to communicate with my PHP server: $.ajax({ ...

PHP is not compatible with cookies, however, they can be effectively used with Javascript

In order to view the cart of products, I am looking to initially load it using PHP and then handle subsequent updates or deletions through jQuery post requests. However, I am encountering an issue. [I receive variables in JSON format within the same PHP ...

The issue of Django/Python static files not loading is causing disruption

mysite/settings.py """ Configuration settings for the Django project called mysite. This file was generated by 'django-admin startproject' using Django 1.9.5. For more details about this file, visit https://docs.djangoproject.com/en/1.9/topics ...