我需要在我的网站上使用一段代码(我几乎找不到),但是当我在我的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');
答案 0 :(得分:1)
它在文档就绪功能中工作正常。你的javascript可能会出错,因为ajax脚本是404ing。我刚刚逐字测试了这段代码并且工作正常:
<html>
<head>
<style>
body, html{
height:200%;
}
#one {
width:100%;
height: 200px;
background-color: aqua;
position: fixed;
}
#two {
width: 100%;
height:50px;
background-color: red;
margin-top:150%;
position:absolute;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script>
$(document).ready(function() {
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');
});
</script>
</head>
<body>
<div id="one">FIXED...</div>
<div id="two">...BUT STOPS HERE</div>
</body>
</html>
如果它仍然无法正常工作,请检查浏览器中的控制台,您的javascript中可能会出现随机语法错误。将此代码粘贴到文件中将演示jsfiddle正在执行的操作。
答案 1 :(得分:0)
你想要什么相同的jsfiddle工作然后它工作正常,如果你使用你在这里提供的任何代码你的缺失引号@second js文件路径,或者如果你想要任何其他东西比这个小提琴更多你能在这里提到...... < / p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
以下代码正常工作
<!DOCTYPE html>
<html>
<head>
<style>
body, html{
height:200%;
}
#one {
width:100%;
height: 200px;
background-color: aqua;
position: fixed;
}
#two {
width: 100%;
height:50px;
background-color: red;
margin-top:150%;
position:absolute;
}
</style>
<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> -->
</head>
<body>
<div id="one">FIXED...</div>
<div id="two">...BUT STOPS HERE</div>
<script>
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');
</script>
</body>