2015-11-01 52 views
3

有3個div,第一個div有一個標誌,第二個有滑塊,第三個div是導航,我想我的導航div固定在視口的底部,當它到達頂部時變得粘稠。我用這個代碼實現了這一點。定位在視口底部的導航欄也始終保持在div的底部

nav{ 
position:absolute; 
width:100%; 
height: 56px; 
background-color:#ffffff; 
bottom:0px; 
display:block; 
border-bottom: 1px solid #ededed; 
background: #FFF; 
font-family: 'Cinzel', serif; 
font-weight:600; 
font-size: 10px; 
line-height: 1.5; 
letter-spacing: 2px; 
} 

的代碼運行良好,但是當瀏覽器的大小時,其位於底部的導航打破了佈局,因爲它涉及了滑蓋的,我想,當瀏覽器大小的div上述資產淨值的高度適應視口的高度,我想創建的效果的工作示例是here

#topbar{ 
 
display:block; 
 
position: fixed; 
 
width:100%; 
 
height: 30px; 
 
color:#fff; 
 
background-color:#073860; 
 
top:0px; 
 
display:block; 
 
} 
 
#head{ 
 
background-color:#ededed; 
 
height:500px; 
 
} 
 

 
#logo{ 
 
font-family: 'lainiedayshregular'; 
 
display: block; 
 
height:134px; 
 
text-align: center; 
 
margin: 29px 0px 0px; 
 
padding: 20px 20px 5px; 
 
font-size:3em; 
 
} 
 
#logo a{ 
 
position:relative; 
 
height: 109px; 
 
width: 377px; 
 
text-decoration:none; 
 
color:#073860; 
 
font-size:2em; 
 
} 
 

 
nav{ 
 
position:absolute; 
 
width:100%; 
 
height: 56px; 
 
background-color:#ffffff; 
 
bottom:0px; 
 
display:block; 
 
border-bottom: 1px solid #ededed; 
 
background: #FFF; 
 
font-family: 'Cinzel', serif; 
 
font-weight:600; 
 
font-size: 10px; 
 
line-height: 1.5; 
 
letter-spacing: 2px; 
 
} 
 

 
nav ul li { 
 
display:inline; 
 
text-decoration:none; 
 
} 
 

 
nav ul li a{ 
 
text-decoration:none; 
 
text-transform: uppercase; 
 
color:#073860; 
 
outline: 0px none; 
 
} 
 
.menu{ 
 
left: 0px; 
 
width: 100%; 
 
height: 56px; 
 
position:sticky; 
 
top:30px; 
 
} 
 
.navbarlogo{ 
 
font-family: 'lainiedayshregular'; 
 
color:#073860; 
 
} 
 
#test{ 
 
height:1000px; 
 
background-color: #fafbf9; 
 
} 
 
#footer{ 
 
border-top: 1px solid #EDEDED; 
 
background-color: #FFF; 
 
height: 37px; 
 
z-index: 3; 
 
bottom: -35px; 
 
transition: bottom 0.6s ease-in-out 0s; 
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 
\t <head> 
 
\t \t 
 
\t \t <!--CSS Links--> 
 
\t \t <link rel="stylesheet" type="text/css" href="style.css"><!--Custom stylesheet--> \t 
 
\t \t <title>Title here</title> 
 
\t </head> 
 
\t <body> 
 
\t \t <div id="topbar"> </div> 
 
\t \t <div id="logo"><a href="#"> 
 
\t \t LOGO 
 
\t \t </a> </div> 
 
\t \t <div id="head" class="row"> 
 
\t \t \t 
 
\t \t </div> 
 
\t \t <nav class="menu"> 
 
\t \t 
 
\t \t </nav> 
 
\t \t 
 
\t \t <div id="test" class="row"></div> 
 
\t \t <footer id="footer" style='display:none; position:fixed; bottom:0px; left:0px; width:100%;'>footer test</footer> 
 
\t </body> 
 
</html>

回答

0

您鏈接到使用JavaScript來實現這一目標的網站。我不認爲用純CSS做這件事是可能的。

他們使用的是聽衆和JQuery的$(document).scrollTop()來計算,其中有人被滾動到頁面上,並且如果它們低於某一位置,添加了一個類的<body>標籤,然後可以用來參考的CSS將導航定位。