Change logo image on scroll down event (and change it back again on scroll up)
Ad
I've got a logo image that I want to change on scroll event, I have this code that I've got but nothing is happening, what am I doing wrong?
This is the PHP/HTML:
<a class="logo" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" 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>
Ad
Answer
Ad
Try this:
var lastTop = 0;
$(window).scroll( function (evt) {
var thisTop = $(this).scrollTop();
if (thisTop > lastTop) {
// scroll down
console.log('down');
}
else {
// scroll up
console.log('up');
}
lastTop = thisTop;
});
Where you see the console.log, place your image change code there. The reason for preserving the lastTop is that the user can continue to scroll down and you need to keep the image the same as the direction is the same.
Ad
source: stackoverflow.com
Related Questions
Ad
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad