我已经实现了一个粘性标题here.
然而,当它到达它的粘着点时,文字会向下移出容器。
我希望它能粘在容器内。
我的HTML& JQ:
<div id="container">
<div id="menu">
<ul>
<li><a href='#'>Line 1</a></li>
<li><a href='#'>Line 2</a></li>
<li><a href='#'>Line 3</a></li>
</ul>
</div>
</div>
我的剧本:
<script type="text/javascript">
$(document).ready(function(){
$(window).scroll(function(){
if($(this).scrollTop()>=575)
{
$('#menu').addClass('fixed');
}else{
$('#menu').removeClass('fixed');
}
});
});
</script>
CSS:
#menu{
height: 35px;
background-color:brown;
z-index:200;
}
#menu ul li{
float: left;
list-style: none;
line-height: 35px;
}
#menu a {
color: #fff;
text-decoration: none;
font-size 16px;
font-weight: bold;
padding: 0 20px;
}
#menu.fixed{
position: fixed;
top:0;
width: 720px;
}
#container{
width: 720px;
margin: 0 auto;
}
提前致谢
迈克尔
答案 0 :(得分:1)
这种元素的转变是因为你的元素具有defalut边际。只需添加css
ul {
margin:0;
}
但是,你最好在样式表的开头添加css-reset,例如这个
/**
* Eric Meyer's Reset CSS v2.0 (http://meyerweb.com/eric/tools/css/reset/)
* http://cssreset.com
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
你应该使用css reset,因为所有浏览器都有默认的css规则,这可能导致你的页面从浏览器到浏览器的呈现方式不同