Help with sticky menu?

Discussion in 'HTML & Website Design' started by danblukk, Dec 12, 2013.

  1. #1
    Using available code, Ive have a sticky menu that sticks to the top of the page when the user scrolls using JQuery. But for the world of me I just cant get it to centre, it stays left. I've tried padding but that messes up the JQuery bit.
    I just want it centred under my title and to stay centred when scrolled.
    I've tried text-align:center, I've tried creating a new div id with rules margin:auto; text-align:center; but it just wont centre.
    can anybody help?

    Heres the code i have

    JQuery
    <script>
      $(function() {
      var sticky_navigation_offset_top = $('#sticky_navigation').offset().top;
     
      var sticky_navigation = function(){
      var scroll_top = $(window).scrollTop(); // our current vertical position from the top
     
      if (scroll_top > sticky_navigation_offset_top) {
      $('#sticky_navigation').css({ 'position': 'fixed', 'top':0 });
      } else {
      $('#sticky_navigation').css({ 'position': 'relative' });
      } 
      };
     
      sticky_navigation();
     
      $(window).scroll(function() {
      sticky_navigation();
      });
    }); 
      </script>
    Code (markup):

    HTML
    <div id="demo_top_wrapper">
      <!-- a header with a logo just to have some content before the menu -->
     
      <!-- this will be our navigation menu -->
      <div id="sticky_navigation_wrapper">
     
      <div id="sticky_navigation">
      <div class="demo_container">
     
      <div id="demo_container">
     
     
      <ul>
      <li><center><a href="#">About Me</a></center></li>
      <li><center><a href="#">My Work</a></center></li>
      <li><center><a href="#">Experience</a></center></li>
      <li><center><a href="#">Contact Me</a></center></li>
     
      </ul>
      </div>
      </div>
      </div>
      </div>
    </div><!-- #demo_top_wrapper -->
    
    HTML:
    CSS
    #demo_container { margin:auto; text-align:center; }
    .demo_container { width:980px; }
    #demo_top_wrapper { margin:0 0 20px 0; }
    #demo_top { height:100px; padding:20px 0 0 0; }
    #my_logo { font:70px Georgia, serif; }
    /* our menu styles */
    #sticky_navigation_wrapper {  margin:0 auto; width:100%; height:50px; }
    #sticky_navigation { text-align:center; margin:auto; width:auto;  height:50px; background:url(trans-black-60.png); -moz-box-shadow: 0 0 5px #999; -webkit-box-shadow: 0 0 5px #999; box-shadow: 0 0 5px #999;float:centre }
    #sticky_navigation ul { list-style:none; margin:0; padding:5px; }
    #sticky_navigation ul li { margin:0; padding:0; display:inline; }
    #sticky_navigation ul li a { display:block; float:left; margin:0 0 0 5px; padding:0 20px; height:40px; line-height:40px; font-size:14px; font-family:Arial, serif; font-weight:bold; color:#ddd; background:#333; -moz-border-radius:3px; -webkit-border-radius:3px; border-radius:3px; }
    Code (markup):

     
    danblukk, Dec 12, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    If you want the sticky navigation to center you need to set a width to it and then set margin:0 auto;
     
    HuggyStudios, Dec 13, 2013 IP
  3. danblukk

    danblukk Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    I did that and it still didnt work
     
    danblukk, Dec 20, 2013 IP
  4. wiicker95

    wiicker95 Well-Known Member

    Messages:
    438
    Likes Received:
    37
    Best Answers:
    10
    Trophy Points:
    100
    #4
    I'm not going to comment your methods as I usually would, but seriously -- 5 wrapper DIV (for the record, it needs NONE) with only one child and each having a class and/or ID means you just didn't grasp inheritance. There's jQuery, so that partially explains it, but don't worry, that CENTER atrocity won't help center anything, much less when it's used in 2013. I always laugh when I see that type of comment: <!-- this will be our navigation menu -->. Not sure if you realized, but you've got an ID named "sticky_navigation". That's the reason we don't call them D6e617669676174696f6e (that says "navigation" in base16).

    Now, back to your problem. The non-centering is a float problem, but then you have a huge amount of useless properties that overwrite the good ones, some are declared 7 times. Losing the float is a step, but it's not enough. You have to get rid of width:980px, then you would have to apply display:inline-block to your LI, get rid of a number of margins, and give your UL text-align:center.
    In one sentence, you should get rid of this trash and start over. There's really nothing worth salvaging here.
     
    wiicker95, Dec 20, 2013 IP
    ryan_uk and deathshadow like this.
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    HEY!!! That's my line.

    ... and a spot on assessment.
     
    deathshadow, Dec 21, 2013 IP