Bootstrap - change affix offset top dynamically

I'm using bootstrap 3 to design a page with a navbar with some text content above it. I wanted the navbar to be "sticky" so I used the affix plug in. because the page is resizing the height of the text conent is changing, so I need to change the "data-offset-top" property of the navbar. I've tried to do it like so:

$( window ).resize(function() {
     $('#main-nav').affix({offset: {top: get_affix_by_width()} });

(where get_affix_by_width() is a function that returns the correct affix based on the window's width)

But it only works one time. That is, when I resize the window the offset doesn't change. My solution (which is stupid) is to refresh the page each time the affix should change, and it works. But it is very waist-full and buggy. Can someone help?