Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Great Challenge!</p> <h2>New Version:</h2> <p>Here is a much better version as it makes the blocks stay in their rows. I added a <code>css</code> function so that your <code>nth-child</code> styles could be applied even in the rows. Even maintains same HTML Structure.</p> <p><strong>Demo:</strong> <a href="http://jsfiddle.net/MadLittleMods/fDDZB/23/" rel="nofollow noreferrer">http://jsfiddle.net/MadLittleMods/fDDZB/23/</a></p> <p>The jQuery for this new revision looks like:</p> <pre><code>$('.block').on('click', function() { var block = $(this); // Keep the blocks in line makeRows($('body')); $('.block').not(this).each(function() { // If sibling on the same level, horizontal toggle // We also want ignore the toggleMethod if it is shown because we might need to reassign if (($(this).position().top == block.position().top &amp;&amp; (($(this).data('toggle') == -1) || $(this).data('toggle') == null)) || ($(this).data('toggle') != -1 &amp;&amp; $(this).data('toggleMethod') == 'side')) { $(this).data('toggleMethod', 'side'); // Hide block if ($(this).data('toggle') == -1 || $(this).data('toggle') == null) { // Set properties for later use in show block $(this).data('overflowBefore', $(this).css('overflow')); $(this).css('overflow', 'hidden'); $(this).data('marginBefore', $(this).css('margin')); var width = $(this).width(); $(this).animate({ width: 0, margin: 0 }, function() { $(this).data('toggle', width); }); } // Show block else { $(this).css('overflow', $(this).data('overflowBefore')); $(this).animate({ width: $(this).data('toggle'), margin: $(this).data('marginBefore') }, function() { $(this).data('toggle', -1); }); } } // Do a normal vertical toggle else { $(this).data('toggleMethod', 'top'); $(this).slideToggle('slow'); } }); }); // Make rows to make the blocks in line function makeRows(container) { // Make rows so that the elements stay where they should var containerWidth = container.width(); var currentRowWidth = 0; // Add styles first so nothing gets messed up container.children().each(function() { var itemCSS = css($(this)); $(this).css(itemCSS); }); // Now assemble the rows container.children().each(function() { var blockWidth = $(this).outerWidth() + parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right')); if((currentRowWidth + blockWidth) &lt; containerWidth) { currentRowWidth += blockWidth; } else { Array.prototype.reverse.call($(this).prevUntil('.row')).wrapAll('&lt;div class="row"&gt;&lt;/div&gt;'); $(this).prev().append('&lt;div class="row_clear" style="clear: both;"&gt;&lt;/div&gt;'); currentRowWidth = 0; } }); } // Remove the rows added function deleteRows() { var content = $('.row').contents() $('.row').replaceWith(content); $('.row_clear').remove(); } $(window).resize(function() { deleteRows(); }); // Functions courtesy of marknadal // https://stackoverflow.com/a/5830517/796832 function css(a) { var sheets = document.styleSheets, o = {}; for(var i in sheets) { var rules = sheets[i].rules || sheets[i].cssRules; for(var r in rules) { if(a.is(rules[r].selectorText)) { o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style'))); } } } return o; } function css2json(css) { var s = {}; if(!css) return s; if(css instanceof CSSStyleDeclaration) { for(var i in css) { if((css[i]).toLowerCase) { s[(css[i]).toLowerCase()] = (css[css[i]]); } } } else if(typeof css == "string") { css = css.split("; "); for (var i in css) { var l = css[i].split(": "); s[l[0].toLowerCase()] = (l[1]); }; } return s; } </code></pre> <p>I added a <code>makeRows</code> and <code>deleteRows</code> functions so that the blocks would stay in their rows instead of getting smaller and moving into the row above. I call <code>deleteRows</code> whenever the window resizes so that it can maintain a responsive layout. Then if the blocks need to be toggled, I recreate the rows.</p> <p><code>css</code> and <code>css2json</code> functions are courtesy of <a href="https://stackoverflow.com/a/5830517/796832">marknadal</a></p> <hr> <hr> <h2>Old version:</h2> <p>I came up with a solution with <code>.animate</code> so that it could ease horizontally.</p> <p><strong>Here is a demo:</strong> <a href="http://jsfiddle.net/MadLittleMods/fDDZB/8/" rel="nofollow noreferrer">http://jsfiddle.net/MadLittleMods/fDDZB/8/</a></p> <p>The jQuery looks like:</p> <pre><code>$('.block').on('click', function() { var block = $(this); $(this).siblings().each(function() { // If sibling on the same level, horizontal toggle // We also want ignore the toggleMethod if it is shown because we might need to reassign if (($(this).position().top == block.position().top &amp;&amp; ($(this).data('toggle') == -1) || $(this).data('toggle') == null) || ($(this).data('toggle') != -1 &amp;&amp; $(this).data('toggleMethod') == 'side')) { $(this).data('toggleMethod', 'side'); // Hide block if ($(this).data('toggle') == -1 || $(this).data('toggle') == null) { // Set properties for later use in show block $(this).data('overflowBefore', $(this).css('overflow')); $(this).css('overflow', 'hidden'); $(this).data('marginBefore', $(this).css('margin')); var width = $(this).width(); $(this).animate({ width: 0, margin: 0 }, function() { $(this).data('toggle', width); }); } // Show block else { $(this).css('overflow', $(this).data('overflowBefore')); $(this).animate({ width: $(this).data('toggle'), margin: $(this).data('marginBefore') }, function() { $(this).data('toggle', -1); }); } } // Do a normal vertical toggle else { $(this).data('toggleMethod', 'top'); $(this).slideToggle('slow'); } }); });​ </code></pre> <p>The key was to separate the blocks that were toggled with <code>.slideToggle</code> and <code>.animate</code> because you have to apply the same when they show and hide.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload