I am working on this website currently.
If you resize the window to less than 650px then the main menu is replaced by a button which when pressed triggers a left fly-out menu similar to the Facebook uses on the mobile website.
When viewed on a computer the menu works very well, but on mobile devices the performance is not so good and the slide-in effect is not smooth. I need help with the performance.
<!-- Mobile Nav.. the navigation items are generated dynamically using jQuery -->
<div id="mobNav" style="background: url(images/top_bg.jpg) repeat; background-size: 100%; position:absolute; left:-200px; width:200px; height:100%;">
</div>
<!-- This is just a wrapper for the rest of the page content -->
<div id="shift">
content Goes Here
</div>
jQuery:
jQuery(document).ready(function($) {
var mobNav = $("#mobNav"),
mainNav = $("#mainnav"),
top = $("#top"),
shift = $("#shift");
// add navigation items to mobile nav
mobNav.html(mainNav.html());
$(window).on("scroll", function() {
if($(window).scrollTop() > 300){
top.fadeIn();
} else{
top.fadeOut();
}
if(parseInt(mobNav.css("left"), 10) == 0){
// if is visible then hide
mobNav.css("left", -mobNav.outerWidth());
shift.css("margin-left", "0");
}
});
$(window).on("resize", function() {
if(parseInt(mobNav.css("left"), 10) == 0){
// if is visible then hide
mobNav.css("left", -mobNav.outerWidth());
shift.css("margin-left", "0");
}
});
// button that triggers mobile nav visibility
$("#mob_nav_trigger").click(function(e) {
e.preventDefault();
var winWidth = $(window).width(),
docHeight = $(document).height();
mobNav.css("minHeight", docHeight).addClass("visible_now").animate({
left: parseInt(mobNav.css('left'),10) == 0 ? -mobNav.outerWidth() : 0
});
shift.animate({
marginLeft: parseInt(shift.css('margin-left'),10) == 200 ? 0 : 200
}).css("minWidth", winWidth);
});
});
Of course I'll be refactoring the code and take some code which is repeating and convert it into a function like:
if(parseInt(mobNav.css("left"), 10) == 0){
// if is visible then hide
mobNav.css("left", -mobNav.outerWidth());
shift.css("margin-left", "0");
}