一、JS Code:

        <script type="text/javascript">

            $.fn.smartFloat = function () {

                var position = function (element) {

                    var top = element.position().top, pos = element.css("position");

                    $(window).scroll(function () {

                        var scrolls = $(this).scrollTop();

                        if (scrolls > top) {

                            if (window.XMLHttpRequest) {

                                element.css({

                                    position: "fixed",

                                    top: 0

                                });

                            } else {

                                element.css({

                                    top: scrolls

                                });

                            }

                        } else {

                            element.css({

                                position: pos,

                                top: top

                            });

                        }

                    });

                };

                return $(this).each(function () {

                    position($(this));

                });

            };

            //绑定

            $("#float").smartFloat();

        </script>

 

二、Html Code:

        <div class="float" id="float" style="border: 1px solid #e0e0e0; padding: 10px;">

            我是置顶时悬浮的区域</div>

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />

        <br />