I'm adding a position property to a div on specific width of the div container and is removing the same property on anothe other width of that div container, it's working but it doesn't toggle, I mean it sticks to what has been applied first, similar issue is happening with the class, am I missing something? I want to toggle, which means that the both style and class should be added when width is greater than 711px, and both of them should be removed when the width is less than 711px, following is my code:
Javascript:
$(function () {
$(window).resize(function () {
var windowSize = $('#mainContainer').width();
if (windowSize > 711) {
$('#navBar').attr('style', 'position: fixed');
$('#mainContent').addClass('offset2');
}
if (windowSize < 711) {
$('#navBar').removeAttribute('style');
$('#mainContent').removeClass('offset2');
}
});
});
HTML:
<div class="container-fluid">
<div class="row-fluid" id="mainContainer">
<div class="span2" id="navBar" >
<nav>
<ul class="nav nav-tabs nav-stacked ">
<li><a href="#">Home</a></li>
<li><a href="#">Spotlight</a></li>
<li><a href="#">Movies</a></li>
<li><a href="#">Tv Shows</a></li>
<li><a href="#">Show Case</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Subscribed</a></li>
</ul>
</nav>
</div>
<div class="span10" id="mainContent">
<p>
Enter a long paragrah of dummy text here, in order to see if the sidebar is scrolling along!
</p>
</div>
</div>