简体   繁体   English

向下滚动页面时如何使徽标出现在固定菜单中

[英]how to make logo appear in fixed menu when scrolling down page

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;). 在我的网站上,我有一个菜单,当您向下滚动到100 px时,该菜单将自身固定在浏览器的顶部(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;) 但是我需要的是在菜单处于其原始位置(顶部:100px;位置:相对)时不显示左侧菜单中的小徽标,而是在用户向下滚动页面且菜单固定至浏览器窗口的顶部(顶部:0px;位置:固定;)

My code is below. 我的代码如下。 I tried to do a JSFiddle but I cant dupicate what is in my html file. 我尝试做一个JSFiddle,但是我无法复制html文件中的内容。

CSS 的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) HTML(当菜单固定在浏览器顶部时,需要显示logo.png)

<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) Javascript(当用户向下滚动100px时,菜单将固定在顶部)

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

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

Give your image an id <img id="myImage"> 给您的图片一个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>

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM