how to make logo appear in fixed menu when scrolli

2019-08-30 08:24发布

on my website I have a menu that when you scroll down to 100 px the menu fixes itself to the top of the browser (top:0px; position:fixed;).

However what I need is for a small logo within the menu on the left to not display while the menu is in its original position (top:100px; position:relative) but appear when the user scrolls down the page and the menu fixes to the top of the browser window (top:0px; position:fixed;)

My code is below. I tried to do a JSFiddle but I cant dupicate what is in my html file.

CSS

#menu, #menu ul {
margin: 0 auto;
padding: 0;
background-color: #FFFFFF;
}
#menu {
display: table;
width: 100%;
list-style: none;
position: relative;
top: 0px;
text-align: center;
-webkit-box-shadow: 0px 3px 23px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow: 0px 3px 23px 0px rgba(50, 50, 50, 0.75);
box-shadow: 0px 3px 23px 0px rgba(50, 50, 50, 0.75);
font-size: 18px;
height: 30px;
z-index: 101;
}
#menu.fixed {
position: fixed;
top: 0;
width: 100%;
}
#menu li {
display: table-cell;
list-style: none;
padding-right: 50px;
left: 50px;
vertical-align:middle;
}
#menu > li:hover > ul {
background:#FFF;
display: block;
left: 0;
width: 100%;
-webkit-box-shadow: 0px 3px 23px 0px rgba(50, 50, 50, 0.75);
-moz-box-shadow:    0px 3px 23px 0px rgba(50, 50, 50, 0.75);
box-shadow:         0px 3px 23px 0px rgba(50, 50, 50, 0.75);
}
#menu > li > ul {
display: none;
position: absolute;
 text-align: center;

}
#menu li a {
display: block;
padding: 10px 10px;
text-decoration: none;
font-weight: lighter;
white-space: nowrap;
color: #333;

}
#menu li a:hover {
color: #CCCCCC;
font-size: 18px;
vertical-align: middle;
}
#menu li ul li {display: inline-block;
 float:none; }

HTML (logo.png is what needs to appear when menu is fixed top top of browser)

<ul id="menu" name="menu">
 <li><img src="logo.png" width="100" height="31" />
</li>
<li>
  <div><a href="#">About Us</a>
  </div>
</li>
<li><a href="#">Services</a>
    <ul>
        <li><a href="#">Plumbing</a>
        </li>
        <li><a href="#">Heating</a>
        </li>
        <li><a href="#">Plastering</a>
        </li>
         <li><a href="#">Decorating</a>
        </li>
        <li><a href="#">Varnish</a></li>
        <li><a href="#">Greenery</a></li>
    </ul>
</li>
<li><a href="#">Community</a>
  <ul>
        <li><a href="#">Help US!</a>
    </li>
        <li><a href="#">Charity Work</a>
        </li>
        <li><a href="#">Impress Us...</a>
        </li>
        <li><a href="#">Careers</a>
        </li>
  </ul>
</li>
<li><a href="#">Contact</a>

</li>
<li><a href="#"><img src="logocrc.png" width="100" height="25" /></a>
</li>
</li>
<li><a href="#"><img src="logoruskin.png" width="100" height="28" /></a>
</li>
</li>
<li><a href="#">Blog</a>
</li>

Javascript (this makes the menu fixed to top when user scrolls down 100px)

<script>
$(document).scroll(function () {
var y = $(document).scrollTop(),
    header = $("#menu");

if (y >= 100) {
    header.addClass('fixed');
} else {
    header.removeClass('fixed');
}
});
</script>

2条回答
Rolldiameter
2楼-- · 2019-08-30 08:39

This all you need?

var logo = $('#menu li:first-child img');
if (y >= 100) {
    header.addClass('fixed');
    logo.show();
} else {
    header.removeClass('fixed');
    logo.hide();
}
查看更多
三岁会撩人
3楼-- · 2019-08-30 08:50

Give your image an id <img id="myImage">

<script>
    $(document).ready(function(){
         //hides them logo when the page loads
         $("#myImage").hide();
    });

    $(document).scroll(function () {
    var y = $(document).scrollTop(),
       image = $("#myImage"),
       header = $("#menu");


    if (y >= 100) {
        //show the image and make the header fixed
        header.addClass('fixed');
        image.show();
    } else {
        //put the header in original position and hide image
        header.removeClass('fixed');
        image.hide();
    }
    });
</script>
查看更多
登录 后发表回答