简体   繁体   English

jQuery sticky div页面顶部

[英]jQuery sticky div top of page

Im trying to make my div stick to the top of the window when scrolled down to it. 当我向下滚动时,我试图让我的div粘在窗口的顶部。 I cant for some reason even seem to target it. 我出于某些原因甚至无法瞄准目标。

I've attached a fiddle to show you my current code. 我附上了一个小提示,向您展示我当前的代码。 Can anybody see where i may be going wrong? 任何人都可以看到我可能出错的地方?

http://jsfiddle.net/rn3yc/ http://jsfiddle.net/rn3yc/

JS: JS:

var $sidebar   = $(".sidebar"),
    $window    = $(window),
    offset     = $sidebar.offset(),
    topPadding = 15;

$window.scroll(function() {
    if ($window.scrollTop() > offset.top) {
        $sidebar.addClass('fixed');
    } else {
        $sidebar.removeClass('fixed');
    }
});

You have no fixed class. 你没有固定的课程。 Write one and add it to the css. 写一个并将其添加到CSS。 Additionally, your fiddle had unclosed braces in the js. 另外,你的小提琴在js中有未闭合的括号。

jsfiddle.net/Nn7d2/show/ is a fixed version. jsfiddle.net/Nn7d2/show/是一个固定版本。

I have created a fiddle for you. 我为你创造了一个小提琴。 Just changed the class with ID and applied 刚刚使用ID更改了类并应用了

position:fixed

in CSS. 在CSS中。 Here is the fiddle jsfiddle.net/rn3yc/10/ 这是小提琴jsfiddle.net/rn3yc/10/

i dont know if this is what you're looking for, but just try to see. 我不知道这是你正在寻找的,但只是试着看。

jsfiddle.net/4RNLC/ jsfiddle.net/4RNLC/

You can use the position property of css to make your header fixed. 您可以使用css的position属性来修复标题。

.fixedHeader{ .fixedHeader {
position : fixed; 位置:固定;
top : 0px; 顶部:0px;
} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM