I have a fluid grid layout. I need some of the columns (less necessary - like Twitter updates) to be "HIDDEN" on smaller resolutions and "SHOWN" on click to its heading.
Here's what I have so far:
HTML
<div id="wrapper">
<div class="toggle">
<h2>Heading</h2>
This content is visible always
</div>
<div class="toggle-small">
<h2 class="toggle-small-heading">Heading</h2>
<div class="toggle-small-content">
This content is hidden on max-width 600px. You can reveal it by clicking the heading.
</div>
</div>
<div class="toggle-small">
<h2 class="toggle-small-heading">Heading</h2>
<div class="toggle-small-content">
This content is hidden on max-width 600px. You can reveal it by clicking the heading.
</div>
</div>
</div>
JQUERY
// D E F I N E T H E F U N C T I O N
function ToggleSmall () {
// CONDITIONALS
// Clickable heading
var ToggleSmallVar = $('#wrapper').find('.toggle-small-heading');
// Content to toggle
var ToggleSmallCol = $('#wrapper').find('.toggle-small-content');
// FUNCTION
// Content to toggle - hide it to be shown on click
$(ToggleSmallCol).addClass('none');
// Toggle function
$(ToggleSmallVar).click(function () {
// Find col to toggle
var ToggleSmallColTarget = $(this).closest('.toggle-small-section').find('.toggle-small-col');
// Toggle the DIV
$(ToggleSmallColTarget).slideToggle('slow');
});
}
// C A L L T H E F U N C T I O N
$(document).ready(function () {
if ($(window).width() < 681) {
ToggleSmall();
}
});
It works on load, but I try to make it work "responsively" on window resize as well: Execute ToggleSmall() function on resolutions smaller than 681 and stop it on bigger ones.
I tried to bind the function on "resize" event (e.g. jQuery as Media Queries on resize), but for some reason "the toggling" executes 3-4x times (document is "ready" multiple times?).
I've found some other sollutions - mainly to define a variable - but none worked so far. I am also a jQuery novice, so maybe I missed something. :) Any ideas?
resizeisn't a one time event, it fires many times. Each time it fires you are adding a new click handler without canceling the previous one. Should have conditionals in your code to see if it has already been run