我试图在div内获得一个固定的div,一旦达到某个元素就会解开。期望的结果显示在下面的链接中。
http://www.pixelbind.com/make-a-div-stick-when-you-scroll/
我无法让unstick部分正常工作。非常感谢任何帮助。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var s = $("#sticker");
var pos = s.position();
var stickermax = $(document).outerHeight() - $("#nosticker").outerHeight() - s.outerHeight() - 40; //40 value is the total of the top and bottom margin
$(window).scroll(function() {
var windowpos = $(window).scrollTop();
//s.html("Distance from top:" + pos.top + "<br />Scroll position: " + windowpos);
if (windowpos >= pos.top && windowpos < stickermax) {
s.attr("style", ""); //kill absolute positioning
s.addClass("stick"); //stick it
} else if (windowpos >= stickermax) {
s.removeClass(); //un-stick
s.css({position: "relative", top: stickermax + "px"}); //set sticker right above the footer
} else {
s.removeClass(); //top of page
}
});
//alert(stickermax); //uncomment to show max sticker postition value on doc.ready
});
</script>
我正在处理的网站位于http://iemajen.com/fosterpettit/
您可以在中途看到购买表格,并了解我想要完成的任务。
谢谢!