【发布时间】:2015-11-25 21:02:10
【问题描述】:
我需要一段代码在我的网站中使用(我几乎找不到),但是当我在我的 html 文件中使用它时,它不起作用。
我尝试将它放在 <html> 之前,在头部和身体中,都不起作用。
也试过$(document).ready(function() { ... });
我正在使用:
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js></script>
但也许我需要另一个
这里是 jsfiddle:http://jsfiddle.net/Tgm6Y/1447/。
var windw = this;
$.fn.followTo = function ( elem ) {
var $this = this,
$window = $(windw),
$bumper = $(elem),
bumperPos = $bumper.offset().top,
thisHeight = $this.outerHeight(),
setPosition = function(){
if ($window.scrollTop() > (bumperPos - thisHeight)) {
$this.css({
position: 'absolute',
top: (bumperPos - thisHeight)
});
} else {
$this.css({
position: 'fixed',
top: 0
});
}
};
$window.resize(function()
{
bumperPos = pos.offset().top;
thisHeight = $this.outerHeight();
setPosition();
});
$window.scroll(setPosition);
setPosition();
};
$('#one').followTo('#two');
【问题讨论】:
-
以上代码有什么问题,你的目标是什么?
-
猜猜这与你的 css 位置有关。
标签: javascript jquery html css ajax