滾動監聽實現

 $.fn.smartFloat = function () {
        var position = function (element) {
            var top = element.offset().top,array=[],content=$(".content .items");
            for(var i=1;i<content.length+1;i++){
                array[i]= $('.content .items:nth-child('+i+')').offset().top;
            }

            $(window).scroll(function () {
                var scrolls = $(this).scrollTop();
                for(var i=1;i<content.length+1;i++){
                    if(scrolls>array[i]&&scrolls<array[i+1]){
                        element.find("a").removeClass("active");
                        $('.slide li:nth-child('+i+')').find("a").addClass("active")
                    }else if(scrolls>array[i]){
                        element.find("a").removeClass("active");
                        $('.slide li:nth-child('+i+')').find("a").addClass("active")
                    }
                }
                if (scrolls > top) {
                    if (window.XMLHttpRequest) {
                        element.css({
                            "position": "fixed",
                            "top": "70px",
                            "z-index": "1000"
                        })
                    } else { //如果是ie6
                        element.css({top: scrolls});
                    }
                } else {
                    element.css({"position": "relative","top":"0"})
                }
            });
        };
        return $(this).each(function () {
            position($(this));
        });
    };
    $("slide").smartFloat();

兩個for循環實現監聽分別獲取高度和改變active的狀態。
smartFloat()這個方法 ,不加這兩個循環,是隨屏幕滾動。。

**說明:**content=(.content.items) (‘.slide’) 相當於bootstrop 滾動監聽的導航條。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章