gpt4 book ai didi

html - 导航栏在固定时跟随滚动

转载 作者:行者123 更新时间:2023-11-28 01:13:04 25 4
gpt4 key购买 nike

我在网站上有一个产品页面。我有一个像家一样的顶级菜单,关于等等....然后我试图在左侧有一个与产品列表保持固定的菜单。看图: menu idea

使用我当前的代码,但是左侧的菜单跟随滚动,这里是代码:

<div id="product-list">
<ul>
<li><a href="bfm.html">item 1</a></li>
<li><a href="hbm.html">item 1</a></li>
<li><a href="laminated.html">item 1</a></li>
<li><a href="ps.html">item 1</a></li>
<li><a href="pm.html">item 1</a></li>
<li><a href="wm.html">item 1</a></li>
</ul>
</div>

#product-list{
position:fixed;
}
#product-list ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 12%;
background-color: #FFF;
position:fixed;
height: 100%;
overflow: hidden;
text-overflow: ellipsis;
padding-top:10%;
}
#product-list li{
overflow: hidden;
text-overflow: ellipsis;
}
#product-list li a {
display: block;
color: #000;
padding: 8px 0 8px 16px;
text-decoration: none;
}
#product-list li a:hover:not(.active) {
background-color: #555;
color: white;
}

最佳答案

Plunkr Example

我所做的改变

我对您的代码做了几件事。

  1. 在您的样式定义中,我删除了一些重复的代码。 #product-list 有几个位置,这使它成为一个固定元素,这很烦人。
  2. 我添加了一个“特殊情况”类 #product-list.fixed,它删除了列表顶部的填充并将其 position 更改为 固定顶部偏移量为0
  3. 我稍微更改了您的 HTML。我删除了产品列表 ul 的包装器 div 并将 #product-list id 添加到 ul .
  4. 我还添加了一些 javacript,以便在滚动大于元素的原始位置时向 #product-list 元素添加/删除 .fixed 类。
  5. 我添加了一个 transition:all linear 0.3 元素来平滑我们添加 fixed 类时的变化
  6. 在下面的代码中,我只是将弹出的图像更改为 placehold.it 图像,这样我就不会占用您的带宽。
  7. 编辑更改了 z-index 以允许侧边菜单滑入页脚下方。

代码

下面代码的顺序是

  1. Javascript(我用的是jQuery,超流行的框架)
  2. CSS(您可能想研究像 Bootstrap 这样的 CSS 框架,它可以减少您的开发时间)
  3. HTML

$(document).ready(function() {
var side_offset = $('#product-list').offset();
//Initial offset.
$(document).on('scroll', function() {
$('#product-list').toggleClass('fixed', $(document).scrollTop() > side_offset.top);
});
})
@charset "utf-8";

/* CSS Document */

html * {
font-family: verdana;
!important;
}
#header {
background-color: #565656;
color: white;
text-align: center;
height: 10%;
position: relative;
z-index: 999;
}
#header ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
text-align: center;
vertical-align: middle;
}
#header li {
display: inline;
}
#header li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 30px;
text-decoration: none;
vertical-align: middle;
}
#header li a.active {
text-decoration: underline;
}
#header li a:hover {
background-color: #111;
}
#header li a.a-no-hover:hover {
background-color: transparent;
}
#product-list {
list-style-type: none;
margin: 0;
padding: 0;
width: 12%;
background-color: #FFF;
position: absolute;
height: 100%;
overflow: hidden;
text-overflow: ellipsis;
padding-top: 10%;
z-index: 100;
transition: all linear 0.3s;
}
#product-list.fixed {
position: fixed !important;
padding-top: 0;
top: 0;
}
#product-list li {
overflow: hidden;
text-overflow: ellipsis;
}
#product-list li a {
display: block;
color: #000;
padding: 8px 0 8px 16px;
text-decoration: none;
}
#product-list li a:hover:not(.active) {
background-color: #555;
color: white;
}
#nav {
line-height: 30px;
background-color: #eeeeee;
height: 300px;
width: 100px;
float: left;
padding: 5px;
}
#section1 {
width: 90%;
height: auto;
overflow: hidden;
margin: 0 auto;
text-align: center;
}
#section1 img {
width: 100%;
height: auto;
opacity: 1;
transition: opacity 0.5s linear;
}
#section3 {
width: 90%;
margin: 0 auto;
text-align: center;
/*float:left;
padding:10px;*/
margin-bottom: 2%;
}
#section2 {
width: 50%;
height: auto;
margin-top: 5%;
margin-bottom: 5%;
margin-left: auto;
margin-right: auto;
text-align: left;
/*float:left;*/
text-align: justify;
text-justify: inter-word;
}
.centerer {
text-align: center;
}
#section5 {
width: 90%;
height: auto;
margin-top: 5%;
margin-bottom: 5%;
margin-left: auto;
margin-right: auto;
text-align: center;
vertical-align: middle;
margin-left: 10%;
}
img {
max-width: 100%;
max-height: 100%;
}
.container {
width: 70%;
margin: 10px auto;
position: relative;
text-align: center;
}
.block {
height: auto;
width: auto;
display: inline-block;
margin: 2%;
vertical-align: middle;
padding-left: 2%;
padding-right: 2%;
font-size: 1.2em;
}
.floating-product {
display: inline-block;
width: 20%;
height: 20%;
/*border: 3px solid #565656;*/
padding: 1%;
margin: 0 auto;
margin-top: 2%;
margin-bottom: 2%;
text-align: center;
vertical-align: middle;
}
.floating-product img {
margin: 0 auto;
max-width: 100%;
max-height: 100%;
display: block;
}
.floating-product a {
color: #000;
}
.floating-product a:hover {
color: #565656;
}
.block a {
color: #000;
}
.block a:hover {
color: #565656;
}
#footer {
background-color: #565656;
color: white;
overflow: auto;
/*clear: both;*/
text-align: left;
padding: 1% 5%;
height: 10%;
vertical-align: middle;
position: relative;
z-index: 999;
}
hr {
width: 70%;
}
h1 {
text-align: left;
font-size: 1.5em;
margin-left: 11%;
}
.margin {
margin-left: 11%;
}
span {
background: transparent;
}
table {
margin-top: 5%;
}
.tg {
border-collapse: collapse;
border-spacing: 0;
width: 99%;
margin: 0 auto;
font-size: 1.1em;
font-weight: 100;
}
.tg td {
padding: 5px 10px;
border-style: none;
border-width: 1px;
overflow: hidden;
word-break: normal;
font-weight: 100;
}
.tg th {
font-weight: 100;
padding: 5px 10px;
border-style: none;
border-width: 1px;
overflow: hidden;
word-break: normal;
}
.tg .tg-lqy6 {
text-align: right;
vertical-align: top;
padding-right: 3%;
}
.tg .tg-yw4l {
vertical-align: top;
text-align: left;
padding-left: 3%;
}
a.fancybox img {
border: none;
box-shadow: 0 1px 7px rgba(0, 0, 0, 0.6);
-o-transform: scale(1, 1);
-ms-transform: scale(1, 1);
-moz-transform: scale(1, 1);
-webkit-transform: scale(1, 1);
transform: scale(1, 1);
-o-transition: all 0.2s ease-in-out;
-ms-transition: all 0.2s ease-in-out;
-moz-transition: all 0.2s ease-in-out;
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
a.fancybox:hover img {
position: relative;
z-index: 999;
-o-transform: scale(1.03, 1.03);
-ms-transform: scale(1.03, 1.03);
-moz-transform: scale(1.03, 1.03);
-webkit-transform: scale(1.03, 1.03);
transform: scale(1.03, 1.03);
}
.imager {
display: inline-block;
width: 15%;
height: 10%;
padding: 1%;
margin: 0 auto;
margin-top: 2%;
margin-bottom: 2%;
text-align: center;
vertical-align: middle;
}
.imager img {
margin: 0 auto;
max-width: 100%;
max-height: 100%;
display: block;
}
.black a {
float: right;
color: #000
}
.black a:hover {
color: #565656;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header">
<ul>
<li>
<a class="a-no-hover" href="index.html">
<img src="placehold.it/300x300" />
</a>
</li>
<li>
<a href="index.html">HOME</a>
</li>
<li>
<a class="active" href="product.html">PRODUCTS</a>
</li>
<li>
<a href="order.php">ORDER</a>
</li>
<li>
<a href="about.html">ABOUT US</a>
</li>
<li>
<a href="contact.html">CONTACT US</a>
</li>
</ul>
</div>
<ul id="product-list">
<li>
<a href="bfm.html">Composite Bag Filter Media</a>
</li>
<li>
<a href="hbm.html">High Dust-Loading Bag Filter Media</a>
</li>
<li>
<a href="laminated.html">Laminated Composite Filter Media</a>
</li>
<li>
<a href="ps.html">Polyester Synthetic Fibre Filter Media</a>
</li>
<li>
<a href="pm.html">Polypropylene Melt-Blown Filter Media</a>
</li>
<li>
<a href="wm.html">Wire Mesh Backed Composite Filter Media</a>
</li>
</ul>
<div id="section5">
<h1>Product Listing</h1>
<div class="floating-product">
<a href="bfm.html">
<img class="displayed" src="//placehold.it/300x300" />
<br />
<h4>Composite Bag Filter Media</h4>
</a>
<p>
<a style="color:#06F" href="bfm.html">Read more...</a>
</p>
</div>
<div class="floating-product">
<a href="hbm.html">
<img class="displayed" src="//placehold.it/300x300" />
<br />
<h4>High Dust-Loading Bag Filter Media</h4>
</a>
<p>
<a style="color:#06F" href="hbm.html">Read more...</a>
</p>
</div>
<div class="floating-product">
<a href="laminated.html">
<img class="displayed" src="//placehold.it/300x300" />
<br />
<h4>Laminated Composite Filter Media</h4>
</a>
<p>
<a style="color:#06F" href="laminated.html">Read more...</a>
</p>
</div>
<div class="floating-product">
<a href="ps.html">
<img class="displayed" src="//placehold.it/300x300" />
<br />
<h4>Polyester Synthetic Fibre Filter Media</h4>
</a>
<p>
<a style="color:#06F" href="ps.html">Read more...</a>
</p>
</div>
<div class="floating-product">
<a href="pm.html">
<img class="displayed" src="//placehold.it/300x300" />
<br />
<h4>Polypropylene Melt-Blown Filter Media</h4>
</a>
<p>
<a style="color:#06F" href="pm.html">Read more...</a>
</p>
</div>
<div class="floating-product">
<a href="wm.html">
<img class="displayed" src="//placehold.it/300x300" />
<br />
<h4>Wire Mesh Backed Composite Filter Media</h4>
</a>
<p>
<a style="color:#06F" href="wm.html">Read more...</a>
</p>
</div>
</div>
<div id="footer">
<span style="float:left;">
<b>Contact Us</b>
<br />

Email: contact@dongguanhy.com <br />

Contact Phone: +86-769-23150100 <br />

Contact Fax: +86-769-23152700 <br />

Company Address: Liu Yong Wei Industry Area, DongGuan, GuandDong Province, China <br />
</span>
<span style="float:right;">
<b>Follow Us</b>
<br />
<br />
<a href="http://facebook.com/dongguanhy/" target="_blank">
<img src="placehold.it/300x300" />
</a>
<!--<img src="http://mmo-stream.net/dong/images/linkedin.png"/>-->
</span>
</div>

关于html - 导航栏在固定时跟随滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36815765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com