0

I am having difficulty resolving a jquery issue,

my code works in a fiddle but when i add it into my document is wont work.

please see link

Any help would be great !!!!

http://paulobriendesign.co.uk/chartego/test.html (not working) http://jsfiddle.net/7Gacp/ (working)

my html

<html>
<head>
    <title>Page Title</title>

</head>

<style>
#profile-container {
    padding-top:52px;
    /* Red bar height */
    margin:0;
    /* Clear default browser margin */
}
#profile-container.fixed {
    padding-top:82px;
    /* Red bar height + yellow bar height */
}
#profile-container {
    height:2000px;
}
#fixed-header {
    width:100%;
    position:fixed;
    height:50px;
    border:1px solid black;
    top:0px;
    background-color:red;
}
.container {
    height:300px;
    width:100%;
    background-color:blue;
}
.sticky-header {
    width:700px;
    height:50px;
    background:orange;
}
.sticky-header {
    height:30px;
    width:100%;
    background:yellow;
}
.fixed .sticky-header {
    position: fixed;
    top:52px;
    margin-bottom:52px;
}
.img {
    width:200px;
    height:200px;
    border:1px solid grey;
    float:left;
}
</style>


<script>
var offset = $(".sticky-header").offset();
var sticky = document.getElementById("sticky-header")
var additionalPixels = 50;

$(window).scroll(function () {
    if ($(window).scrollTop() > offset.top - additionalPixels) {
        $('#profile-container').addClass('fixed');
    } else {
        $('#profile-container').removeClass('fixed');
    }
});
</script>


<body>

<div id="profile-container">
    <div id="fixed-header"></div>
    <div class="container"></div>
    <div class="sticky-header">This needs to be fixed when hits top of screen</div>
    <div class="img">needs to be smooth</div>
    <div class="img"></div>
    <div class="img"></div>
</div>


<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</body>
</html>
4

1 回答 1

2

您的脚本标记出现在 jquery 脚本上方。所以它只是还不知道是什么$。将您的脚本放在 jquery 下面并查看它是否有效(将您的脚本放在document.readyhtml 中的元素中或之后,以便可以选择元素。)。在 jsFiddle 中,当您load在下拉菜单中选择时,它会将您的脚本包装在 中window.onload,在头部添加 jquery 的脚本标签,这就是它起作用的原因。

<script>
var offset = $(".sticky-header").offset();
var sticky = document.getElementById("sticky-header")
var additionalPixels = 50;

$(window).scroll(function () {
    if ($(window).scrollTop() > offset.top - additionalPixels) {
        $('#profile-container').addClass('fixed');
    } else {
        $('#profile-container').removeClass('fixed');
    }
});
</script>


<body>

<div id="profile-container">
    <div id="fixed-header"></div>
    <div class="container"></div>
    <div class="sticky-header">This needs to be fixed when hits top of screen</div>
    <div class="img">needs to be smooth</div>
    <div class="img"></div>
    <div class="img"></div>
</div>


<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

尝试

    <body>

    <div id="profile-container">
        <div id="fixed-header"></div>
        <div class="container"></div>
        <div class="sticky-header">This needs to be fixed when hits top of screen</div>
        <div class="img">needs to be smooth</div>
        <div class="img"></div>
        <div class="img"></div>
    </div>


    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
 <script>
 $(function(){ //<--- Here document.ready
    var offset = $(".sticky-header").offset();
    var sticky = document.getElementById("sticky-header")
    var additionalPixels = 50;

    $(window).scroll(function () {
        if ($(window).scrollTop() > offset.top - additionalPixels) {
            $('#profile-container').addClass('fixed');
        } else {
            $('#profile-container').removeClass('fixed');
        }
    });
  });
 </script>
于 2013-10-14T16:33:15.407 回答