(function ($) { $.fn.stickySidebar = function (options) { var config = $.extend({ headerSelector: 'header', navSelector: '.content_t', contentSelector: '#portal_tab' }, options); var fixSidebr = function () { var sidebarSelector = $(this); var headerHeight = $(config.headerSelector).outerHeight(); var navHeight = $(config.navSelector).outerHeight(); var contentHeight = $(config.contentSelector).outerHeight(); var sidebarHeight = sidebarSelector.outerHeight(); var scroll_top = $(window).scrollTop(); var breakingPoint1 = headerHeight + navHeight + 8; var breakingPoint2 = breakingPoint1 + contentHeight - sidebarHeight; if ((scroll_top < breakingPoint1) || (scroll_top > breakingPoint2)) { sidebarSelector.removeClass('sticky'); } else { sidebarSelector.addClass('sticky'); } }; return this.each(function () { $(window).on('scroll', $.proxy(fixSidebr, this)); $(window).on('resize', $.proxy(fixSidebr, this)); $.proxy(fixSidebr, this)(); }); }; }(jQuery));