1 !(function(){ 2 $.fn.affix = function(options){ 3 var defualtOption = { 4 top:0, 5 container:$("html, body"), 6 bottom:0 7 } 8 var option = $.extend( defualtOption, options); 9 var offsetTop = $(this).offset().top; 10 var height = option.container.height(); 11 var cOffsetTop = option.container.offset().top; 12 var limmit = height + cOffsetTop - option.bottom; 13 var _this = this; 14 $(window).on("scroll",function(){ 15 var scorllTop = $(window).scrollTop(); 16 console.log("dddd") 17 console.log(offsetTop) 18 console.log(scorllTop + cOffsetTop) 19 if(offsetTop < scorllTop && scorllTop < limmit){ 20 if(window.XMLHttpRequest){ 21 $(_this).css({ 22 position:"fixed", 23 top:0 24 }) 25 }else{ 26 $(_this).css({ 27 position:"absolute", 28 top: offsetTop - scorllTop 29 }) 30 } 31 }else{ 32 $(_this).css({ 33 position:"static" 34 }) 35 } 36 }) 37 } 38 })(jQuery)