Switch up logo on scrolling down (and revert back when scrolling up)

What should I do to change the logo image on scroll event? I have tried using this code but it's not working, can anyone point out my mistake?

Here is the PHP/HTML:

<a class="logo" href="<?php echo esc_url( home_url( '/' ) ); ?>" data-fixed-height="<?php echo esc_attr( et_get_option( 'fixed_nav_logo_height', '51' ) ); ?>">
  <img src="<?php echo esc_attr( $logo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" id="logo" />
</a>

This is the jQuery:

<script>

    ( document ).ready(function() {
        ('.ie #main-header #logo').attr('src','my_logo_image_url');

    (window).scroll( function () {
        ('body:not(.ie) #main-header #logo').not('.et-fixed-header').attr('src','my_logo_image_url');
        ('body:not(.ie) #main-header.et-fixed-header #logo').attr('src','my_alternative_logo_image_url');
    });
});

</script>

Answer №1

Give this a shot:

let previousScroll = 0;
$(window).scroll( function (evt) {
  let currentScroll = $(this).scrollTop();
  if (currentScroll > previousScroll) {
    // scrolling downward
    console.log('down');
  }
  else {
    // scrolling upward
    console.log('up');
  }
  previousScroll = currentScroll;
});

Insert your image change code where the console.log statements are located. Keep track of the last scroll position to ensure the image changes appropriately as the user continues to scroll in the same direction.

Answer №2

There is a handy plugin that can detect mouse wheel movement and speed within a specified area, known as jquery-mousewheel

If you're unsure about what is required, here's a suggestion: Do you need to update the logo source based on different scrolling increments? (perhaps when various page elements come into view) You could monitor when they are scrolled into using their scroll offset position:

$(window).on('mousewheel', function(e)
{
     console.log(event.deltaX, event.deltaY, event.deltaFactor);
     if(e.deltaY >= $("element_to_match_0").offset().top)
        $("#your_logo").attr('src','my_alternative_logo_image_url');

     // another element being scrolled over
     if(e.deltaY >= $("element_to_match_1").offset().top)
        $("#your_logo").attr('src','my_alternative_logo_image_url');
});

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

Developers guide to using Selenium IDE

After downloading the source code of Selenium IDE, I'm eager to make some edits but unfortunately, I can't seem to find any documentation on how to do so. Hey folks, anyone aware of any resources available for developers working on Selenium IDE? ...

Arrangement of divs using Bootstrap version 3

Currently working on a project using Bootstrap 3 and aiming to create a layout similar to the one shown in the image provided: Any suggestions on how to achieve this? Any code snippets would be greatly appreciated. Thank you Experimenting with something ...

Exploring the power of Angular 2 in conjunction with web APIs

After experimenting with Angular 2 for some time, I decided to explore using web APIs like geolocation (utilizing the navigator object) and browser Notifications for some fun projects. Check out this link about notifications in web APIs Here's a res ...

Tips on creating responsive email designs

What is the best way to make emails responsive in a php application when external css files are not supported and media queries cannot be included inline? Looking for alternatives to make emails responsive without external css files and media queries, any ...

My jquery is malfunctioning when trying to retrieve values

I am facing an issue with accessing server-side values in my jQuery function. When I use my localhost path (NewsRecord.php) as the AJAX URL, it works fine. However, when I use the server path, it does not work. The strange thing is that the server URL pr ...

Every single word encompassed in a Drupal block title span

I am interested in wrapping a span tag around each word in my block title. Specifically, I want the last two words to be displayed in a larger font size and a unique color. ...

Crafting a stylish vertical timeline using Tailwind and CSS: Step-by-step guide

I am attempting to design a timeline for a react app where odd children are displayed on the left side of the timeline and even children on the right side. Here is an image for reference: https://i.sstatic.net/YnNFU.png The issue I am facing is that I am ...

Can you tell me the alternatives for getServerSideProps and getStaticProps in Next.js version 14?

I'm trying to wrap my head around the rendering behavior of SSR/SSG/ISR in Next.js version 14 with the updated app router. Previously, Next.js provided predefined functions like getServerSideProps for server-side fetching and processing (SSR), or getS ...

The error "converting circular structure to json" occurred when trying to create a JSON web token, resulting in an unhandled TypeError

screenshot of error I recently encountered an issue while implementing JSON web token authentication on my application. The error message displayed was: " Unhandled rejection TypeError: converting circular structure to JSON " Upon further investigation, ...

Tips for placing a background image on an extended <a> hyperlink in Internet Explorer 6

There seems to be an issue with the background image of a link on my exam project. The requirement is for it to work seamlessly on all browsers, including IE 6. However, the problem arises when the link spans multiple lines in IE 6, causing the background ...

Creating a simple Node.js project using material web components - a step-by-step guide

I have recently started experimenting with Node.js. This is a basic program I created to test the integration of material-components-web. I followed the instructions provided in the readme file and here's what I have implemented so far: package.json ...

In JavaScript, eliminate any links with empty href attributes and substitute them with text only

I attempted to implement this code snippet: $('a[href=""]').each(function(){ var linkText = $(this).text(); $(this).after(linkText); $(this).remove(); }); I'm uncertain if my syntax is correct. Can someone provide assistance, p ...

Troubleshooting why the second statement is not being triggered by the Vuejs

Incorporating a lambda expression into the methods section of a Vuejs component has been a recent challenge for me. Here's an example: I initiate alertyou() and upon receiving the alert, I click okay. Subsequently, in the Vue developer tools, I notic ...

In the scenario where there are duplicate 'id' keys within the array of objects, what is the best method to remove the object with the duplicate key?

When there are duplicate 'id' keys among the objects in the array, how can you remove the object with the duplicated 'id'? I attempted to use filter, map, and set methods, but none of them were successful. Since the array is not one-di ...

How come the styles in my CSS file aren't being applied to my images based on their class or ID?

When I apply a className or id to my img tag in my JavaScript (using React.js) and then add a style that references that id or class, the style does not get applied. However, when I do the same for a div, everything works perfectly fine. <--JavaScript- ...

How to retrieve the complete file path of an image during the image upload process

I am having trouble uploading an image as it only takes the file name with extension, not the full path. <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">< ...

Using different browsers can cause web fonts to appear rough and pixelated

While exploring the issue of "jagged" or "pixelated" fonts, I made an interesting discovery. After creating a website locally and viewing it in Firefox, the font from Google Webfonts appeared flawless - almost like it was straight out of Photoshop. It wasn ...

Issues encountered when trying to use ng-repeat alongside a multiselect plugin

<select class="form_control" id="district" name="district" multiple ng-model="$scope.district"> <option value="{{tp_id}}" ng-repeat="tp_id in district" >{{tp_id}} </option> </select> I am facing an issue where no v ...

Display the current time and continuously update it in real-time on the DOM using React

Incorporating a live clock feature into my app is important to me. I want the time to update automatically, without requiring the user to manually refresh the page. My goal is to have a clock display the time, for example 11:59, and then transition to 12: ...

There was no popcorn mix-up in your document

Encountering an issue while using grunt for staging (grunt serve): Running "bower-install:app" (bower-install) task popcornjs was not injected into your file. Please check the "app\bower_components\popcornjs" directory for the required file, an ...