1

i know there are other solutions to this problem, but i simply cant get them to work

i have a fixed div that sticks to the bottom. i would like i to stop when it hits the #footer

the site

is have is css so basically i need a script that changes the class from .widget.widget_nav_menu to .widget.widget_nav_menu.fixed_button when is hits the border of the footer

is that possible?

.widget.widget_nav_menu{
  background: none repeat scroll 0 0 #2E3337;
  bottom: 0;
  padding: 25px;
  position: fixed;
  right: 2%;
  color: #707480;
    font-size: 0.8em;
    opacity: 1;
    text-transform: uppercase;
}
.widget.widget_nav_menu.fixed_button {
  margin-right: -210px;
  position: absolute !important;
  top: -180px;
  background: none repeat scroll 0 0 #2E3337;
  padding: 25px;
  right: 2%;
}
user2979600
  • 17
  • 2
  • 5

1 Answers1

4

There's a couple of answers floating around on StackOverflow that answer your question if JavaScript can dectect if the page has hit the bottom or not:

With the help of the above, you change simply change the Class once it has:

<script>
$(function() {
    $(window).scroll(function(){

       //Cache the Footer Widget
       var $footerMenu = $('.widget.widget_nav_menu');

       //Check if it hits the bottom, toggle Relevant Class.
       if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight)
          $footerMenu.addClass('fixed_button');
       else
          $footerMenu.removeClass('fixed_button');

    });
});
</script>

Demo Fiddle: http://jsfiddle.net/fVKZe/1/

To include this into a WordPress setup, you'll need to add a custom .js file and queue it until jQuery has loaded in WordPress as a dependency.

stickymenu.js

jQuery(document).ready(function($) {
    $(window).scroll(function(){
        //The Above Footer Widget Code
    });
});

functions.php

function stickyfooter_method() {
    wp_enqueue_script(
        'stickymenu',
         get_stylesheet_directory_uri() . '/js/stickymenu.js',
         array( 'jquery' )
    );
}

add_action( 'wp_enqueue_scripts', 'stickyfooter_method' );
Community
  • 1
  • 1
MackieeE
  • 11,751
  • 4
  • 39
  • 56
  • i should put this script in my header? – user2979600 Nov 12 '13 at 11:16
  • Your site is a WordPress site (I think), thus you'll need to include your JavaScript after jQuery is loaded (Currently it's loading before jQuery is loaded in the footer). You should include it in a .js file, then queue it in functions.php. – MackieeE Nov 12 '13 at 11:20
  • @user2979600 Updated my answer to include Wordpress custom JavaScript – MackieeE Nov 12 '13 at 11:25
  • I addet the script to my header.php. created a stickymenu.js to my /js/ folder. add the code to my functions.php.. still not working.. im not good at this JS thing so a really can't help my self.. :).. – user2979600 Nov 12 '13 at 12:39
  • You where a bit fast with your answer :)... I addet the script to my header.php. created a stickymenu.js to my /js/ folder. add the code to my functions.php.. still not working.. im not good at this JS thing so a really can't help my self.. :).. – user2979600 Nov 12 '13 at 12:41
  • Sorry! I made a mistake on the functions.php, please change `add_action( 'wp_enqueue_scripts', 'stickyfooter' );` to `add_action( 'wp_enqueue_scripts', 'stickyfooter_method' );` and change `/js/stickyfooter.js` to `/js/stickymenu.js/` so sorry! – MackieeE Nov 12 '13 at 12:43
  • Finally, you can just copy-paste this JavaScript on this link into `stickymenu.js` http://jsfiddle.net/a95N2/ :)! (The jQuery(document)..) part, and not to worry! – MackieeE Nov 12 '13 at 12:46
  • so where do i put the script? – user2979600 Nov 12 '13 at 12:47
  • @user2979600 Sorry for the two mistakes I made, if you correct them and place the linked JavaScript into `stickymenu.js`, it should work! – MackieeE Nov 12 '13 at 12:48
  • Well the page doesn't have `.widget_nav_menu` anymore so it won't change anything, it's now `#menu_bottom` right? `$('.widget.widget_nav_menu');` to `$('#menu_bottom')` – MackieeE Nov 12 '13 at 12:56
  • I think your `functions.php` still references my previous mistake, sorry! `get_stylesheet_directory_uri() . '/js/stickyfooter.js'`, to `get_stylesheet_directory_uri() . '/js/stickymenu.js'`, – MackieeE Nov 12 '13 at 13:09
  • Nice one! :D Now it can find your stickymenu.js script, last thing to do is change it go `#menu_bottom` ;) – MackieeE Nov 12 '13 at 13:17
  • That would of been much faster if I didn't make the couple of mistakes in my answer, Sorry! Glad it works :)!! @user2979600 – MackieeE Nov 12 '13 at 13:19
  • one more question, if you got more time to help me. What if i wanted the menu to stop 168px above the bottom :) – user2979600 Nov 12 '13 at 14:08
  • You'd change the `if` statement to subtract the `
    ` height, adjust: `if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight)` into: `if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - document.getElementById('footer').offsetHeight )`
    – MackieeE Nov 12 '13 at 14:31
  • Anytime =)! Glad to help! – MackieeE Nov 12 '13 at 14:36