jquery - 固定标题对齐问题

标签 jquery html css

我正在为一个 friend 建立一个网站,我刚刚实现了一个固定的 header 。

http://scsports.comeze.com/blog.php

问题是当您滚动标题时,标题向右移动了大约 7 像素。我不知道为什么,我尝试过的一切都不起作用。任何想法将不胜感激!

HTML+JS:

<div class="navbar">
    <div class="navbar-inner">
    <a class="brand" href="#"><img src="images/logo2.png" class="logo" alt="header    logo"></a>
    <?php include 'socialbtn.php';?>
    <div class="test">
    <ul class="nav">
    <!--<li class="active">-->
    <li><a id="index_link" href="index.php">Home</a></li>
    <li><a id="therapist_link" href="therapist_profile.php">Therapist Profile</a></li>
    <li><a href="services.php">Services & Prices</a></li>
    <li><a href="testimonials.php">Testimonials</a></li>
    <li><a href="contact.php">Contact Me</a></li>
    <li><a href="blog.php">Blog</a></li>
    <li><a href="raceready.php">Race Ready?</a></li>
    <li><a href="treatment.php">What We Treat</a></li>
    <li><a href="expect.php">What To Expect</a></li>

       </ul>
       </div>
</div>
</div>       

<script type="text/javascript">
       jQuery(function(){
        var menuOffset = jQuery('.test')[0].offsetTop;
         jQuery(document).bind('ready scroll',function() {
         var docScroll = jQuery(document).scrollTop();
        if(docScroll >= menuOffset +170) {
       jQuery('.test').addClass('fixed');
         } else {
        jQuery('.test').removeClass('fixed').removeAttr("width");
         }
             });
                }); 

       </script>

CSS:

.test {

} 

.test.fixed{
width: 930px;
position: fixed;
z-index: 9999;
top:0;
display:block;
min-height: 50px;
padding-right: 10px;
padding-left: 10px;
background-color: #2c3e50;
background-image: -moz-linear-gradient(top, #2c3e50, #2c3e50);
background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#2c3e50), to(#2c3e50));
background-image: -webkit-linear-gradient(top, #2c3e50, #2c3e50);
background-image: -o-linear-gradient(top, #2c3e50, #2c3e50);
background-image: linear-gradient(to bottom, #2c3e50, #2c3e50);
background-repeat: repeat-x;
border: 1px solid #233140;
-webkit-border-radius: 6px;
 -moz-border-radius: 6px;
      border-radius: 6px;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff2c3e50', endColorstr='#ff2c3e50', GradientType=0);
*zoom: 1;
-webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.065);
 -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.065);
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.065);

}

我很感激我的代码不是最好的,这里可能还有一些多余的东西,但我只是从整个商店中收集点点滴滴。

最佳答案

使用“固定”定位的问题在于它会使元素脱离流动。因此它不能相对于其父项重新定位,因为它就好像没有父项一样。但是,如果容器具有固定的已知宽度,您可以使用类似以下内容的方法:

.test.fixed { left: 50%;
              width: 916px;
              margin-left: -470px;
            }

我给定的宽度为 916px,因为它包含一些填充,但容器宽度为 940px。所以 margin-left 只是元素宽度的一半。

关于jquery - 固定标题对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17855521/

相关文章:

javascript - 使用 jQuery 如何使用函数使 div 对象可见?

html - 段落不尊重标题边距

javascript - jQuery 验证在有效时启用提交按钮

html - 溢出问题 :auto

HTML5 和访问 key

html - 如何在 Mailchimp 嵌入式表单中的同一行获取复选框和文本?

html - css代码如何创建多个div类

javascript - 通过按钮删除动态添加的 li

jquery - 监听本地存储更改事件

jQuery 日期选择器多选和取消选择