javascript - Run code on window resize -
i'm trying run code when window resizing, code:
$(document).ready(function () { updatecontainer(); $(window).resize(function() { updatecontainer(); }); $('#slider').lightslider({ item:1, slidemove:1, slidemargin: 30, loop: true, autowidth: slidewidth }); }); function updatecontainer() { var $winwidth = $(window).width(); if ($winwidth > 1100) { var slidewidth = false; /* toggle navigation ======================================================================== */ $('.menu, .search, .categories').click(function(e) { $(this).toggleclass('active'); e.preventdefault(); }); /* top ======================================================================== */ $('#back-top').click(function(e) { e.preventdefault(); $('html, body').animate({ scrolltop: 0 }, 500); }); } if ($winwidth > 700) { var slidewidth = true; } }
one error appearing in console: var slidewidth not definited.
what doing wrong? have tried several ways of writing none of them work...
during document
ready, haven't defined here:
$('#slider').lightslider({ item:1, slidemove:1, slidemargin: 30, loop: true, autowidth: slidewidth });
at above stage not defined. make accessible in higher scope:
$(document).ready(function () { var slidewidth = false; updatecontainer(); $(window).resize(function() { updatecontainer(); }); $('#slider').lightslider({ item:1, slidemove:1, slidemargin: 30, loop: true, autowidth: slidewidth }); });
make var slidewidth = false;
goes global scope. , when execute updatecontainer()
update global variable.
and make sure, in updatecontainer()
, not using var
:
function updatecontainer() { var $winwidth = $(window).width(); if ($winwidth > 1100) { slidewidth = false; // other code }
Comments
Post a Comment