Recently, I've been working on a piece of code to keep my div fixed at the top of the page while scrolling. However, it doesn't seem to be functioning as intended.
Would anyone be able to point out where I might have gone wrong?
The element in question is located within a <div class='container'>
and a div row.
<div id="compareDiv">
<a id="compare" href="#animatedModal" disabled class="compare-products">Compare Domestic</a>
</div>
<script>
$(window).scroll(fixDiv);
function fixDiv() {
var $div = $("#compareDiv");
if ($(window).scrollTop() > $div.data("top")) {
$('#compareDiv').css({'position': 'fixed', 'top': '0', 'width': '100%'});
}
else {
$('#compareDiv').css({'position': 'static', 'top': 'auto', 'width': '100%'});
}
$("#compareDiv").data("top", $("#compareDiv").offset().top); // set original position on load
UPDATE
It turned out that the element was hidden behind another object. Adjusting the z-index resolved the issue.
Thank you for your assistance.