Javascript Html、css和jQuery。我的代码中有些东西不起作用

Javascript Html、css和jQuery。我的代码中有些东西不起作用,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我是jQuery的新手,正在看一个关于导航条的教程,结果出了问题,我想知道该怎么办 在我的脚本文件中,我有它,所以它可以在加载时运行此代码[并且我有一个css类用于nav占位符包装] 嗯,边距属性似乎不起作用,如果你运行站点并沿着导航条一直滚动到一边,它应该居中(边距作业)。我不知道为什么它不这样做,这可能是愚蠢的,但请帮助` HERES THE CODE__ Script.js: http://pastebin.com/XYWR5tKJ Css: http://pastebin.com/Y5

我是jQuery的新手,正在看一个关于导航条的教程,结果出了问题,我想知道该怎么办

在我的脚本文件中,我有它,所以它可以在加载时运行此代码[并且我有一个css类用于nav占位符包装]

嗯,边距属性似乎不起作用,如果你运行站点并沿着导航条一直滚动到一边,它应该居中(边距作业)。我不知道为什么它不这样做,这可能是愚蠢的,但请帮助`

HERES THE CODE__

Script.js: http://pastebin.com/XYWR5tKJ
Css: http://pastebin.com/Y51rYJVE
HTML: http://pastebin.com/tTftEJKh
__

THANKS!


  [1]: http://pastebin.com/tTftEJKh

固定居中元素的基本设置为:

包含位置固定的元素


其中的一个元素具有边距:0 auto;和一个设置的宽度。

我想说,您的left:0 inside fixed在很大程度上抵消了一些东西

既然你没有指定,这对你来说够好吗?


同样可能更好的是,作为分享JSFIDLE、Codepen或JSBin之类的代码的提示,举几个例子……也很适合在内部学习。

这个问题让我想起了我不久前为了学习相同的概念而做的一次尝试。我用注释对其进行了更新,使其更具可读性

它并没有真正回答您的问题,但我认为它将有助于理解在JQuery中实现粘性导航效果时所涉及的各种组件,对于遇到此线程的任何不确定的人来说

JavaScript/JQuery代码: (有关附带的html/css,请参阅JSFIDLE)

//窗口-保存和呈现文档的容器。
//文档-窗口中呈现的html。文档可以比窗口大,如本例所示,因为需要滚动。。
//有关附加到导航栏和继续元素的类的更多信息,请参阅css。
$(文档).ready(函数(){
//将滚动功能添加到需要
$(文档).on('scroll',函数(){
//获取窗口距文档顶部的像素数。开始时为零。
var scrollTop=$(this.scrollTop();
//在此处插入粘性导航元素的名称以代替.scrollFixed
$('.scrollFixed')。每个(函数(){
//scrollFix变量初始化为.scrollFixed对象及其所有属性。
var scrollFix=$(这个);
//获取粘性导航元素的偏移量(以像素为单位)
var topDistance=scrollFix.offset().top;
var previousElement=scrollFix.prev();
//这只是为了调试和学习。
$('.fixed_info').html('导航栏是距离文档顶部'+topDistance+'像素');
//如果您在此处输入topDistance而不是手动输入数字,导航栏将闪烁。
//不确定为什么。。
//检查导航元素是否已滚动过窗口顶部。
如果((298)=scrollTop&&scrollFix.hasClass('Stick')){
//使粘性导航成为可滚动的元素
scrollFix.removeClass('stacked');
//使前进元件变短,以补偿粘性导航将其下方的元件向下推。
$(“.element_位于固定导航栏下方”).removeClass(“扩展”);
//指示元素是可滚动的!
html(“元素现在可移动!”);
}
});
});
});
这与您链接的JavaScript代码背后的逻辑非常相似

  • 从文档顶部查找窗口的像素数。文档首次加载时从0开始,除非另有编码。每次滚动时都会更新

  • 从文档顶部查找导航栏元素的像素数(偏移量)

  • 通过检查导航条到窗口的偏移来检查导航条是否已到达窗口顶部。如果已到达,则将其固定

  • 检查导航条的原始偏移量是否已低于窗口顶部。如果已降低,则使导航条可滚动


  • 它并没有真正回答您的问题,但我认为它将有助于了解在JQuery中实现粘性导航效果时涉及的各种组件,对于任何遇到此线程但不确定的人来说。

    链接中的代码已被删除。请发布正确的链接。请将标题修改为描述性。每个问题所以可以用“我的代码中有些东西不起作用”来概括。并且从那里删除标记。你说的“边距属性似乎不起作用”是什么意思?什么元素上的边距?老实说,堆栈溢出不允许我这么做,但不管怎样。只要复制并粘贴链接就行了
    //window - the container that holds and renders the document. 
    //document - the rendered html within the window. the document can be bigger than the window as with this example as scrolling is needed..
    //refer to css for more info on the appended classes to nav bar and proceeding element.
    $(document).ready(function () {
    //add a scroll function to the document that 
    $(document).on('scroll', function () {
    
        //get the  number of pixels that the window is from the top of the document. this is zero at first.
        var scrollTop = $(this).scrollTop();
    
        //insert the name of your sticky nav element here in place of .scrollFixed
        $('.scrollFixed').each(function () {
    
            //scrollFix variable is initialized as .scrollFixed object with all its attributes.
            var scrollFix = $(this);
    
            //gets offset of sticky nav element in pixels 
            var topDistance = scrollFix.offset().top;
            var previousElement = scrollFix.prev();
    
            //this is just for debugging and learning purposes.
            $('.fixed_info').html('nav bar is ' + topDistance + ' pixels from top of document');
    
            //if you put topDistance here instead of the number manually, the nav bar will flicker.
            //unsure why..
            //checks to see whether nav element has been scrolled past the top of window.
            if ((298) < scrollTop) {
    
                //make sticky nav a fixed element
                scrollFix.addClass("stuck");
    
                //extend the element below for this example so proceeding elements don't visually jump up
                //when closing the empty gap.
                $(".element_below_fixed_nav_bar").addClass("extend");
    
                //indicates element is fixed!
                scrollFix.html("Element now fixed!");
    
            //you will have to manually put the topDistance here as well...
                //this checks whether the nav element's original top has passed the top of 
                //the enclosing window.
                //it needs to become scrollable again
            } else if (298 >= scrollTop && scrollFix.hasClass('stuck')) {
    
                //make sticky nav a scrollable element
                scrollFix.removeClass('stuck');
    
                //make proceeding element shorter to compensate for sticky nav pushing elements below it down.
                $(".element_below_fixed_nav_bar").removeClass("extend");
    
                //indicates element is scrollable!
                scrollFix.html("Element now moveable!");
            }
        });
    });
    });