2015-10-01 30 views
1

我創造了這個功能,計算偏移量和樣式的菜單我想用引導詞綴插件貼上:如何運行窗口上的自舉詞綴功能調整

http://jsfiddle.net/jssdfqfp/

function MyAffix(options) { 
    var e = options.element, 
     eWidth = e.width(), 
     eOffsetTop = e.offset().top; 

    // Calculate offsets 

    e.affix({ 
    offset: { 
     top: function() { 
     return (this.top = eOffsetTop - 40); 
     }, 
     bottom: function() { 
     return (this.bottom = $("footer").outerHeight(true)); 
     } 
    } 
    }); 

    // Apply styles 

    e.on({ 

    "affixed.bs.affix": function() { 
     $(this).css({ 
     "position": "fixed", 
     "width": eWidth, 
     "top": 40 
     }) 
    }, 
    "affixed-top.bs.affix": function() { 
     $(this).attr("style", ""); 
    } 

    }); 

} 

var uiMenuAffix = new MyAffix({element: $("#ui-menu")}); 

這似乎工作正常。

我想要的是讓它更新窗口調整大小的數據。我相對較新的JS,所以我不知道我應該在哪裏運行$(window).resize()函數,我應該把它放在哪裏。任何幫助,將不勝感激。謝謝。

回答

0

你可以做如下所示:

$(document).ready(function() 
{ 
    $(window).resize(function() 
    { 
     returnUIMenuAffix(); 
    }); 
}); 


function returnUIMenuAffix() 
{ 
    var uiMenuAffix = new MyAffix({element: $("#ui-menu")}); 
    return uiMenuAffix; 
} 

JSFIDDLE EXAMPLE