jQuery(function($) { // Anchor var eiAnchorLink = '.ei-anchor-link', eiAnchorSet = '.ei-anchor-set', eiAnchorOrigin = '.ei-anchor-origin', eiAnchorContent = '.ei-anchor-post-meta', eiAnchorActive = 'ei-active', eiAnchorPrefix = 'ei-anchor-active', eiAnchorFadeIn = 200, eiAnchorFadeOut = 200, eiAnchorCurrent = null; function eiAnchorReset(id) { if (!id) return; $('#' + id).fadeOut(eiAnchorFadeOut, function () { $(this).appendTo($(eiAnchorOrigin + '[data-id="' + id + '"]')).fadeIn(0); }); $(eiAnchorLink).removeClass(eiAnchorActive); $('body').removeClass(eiAnchorPrefix); } $(eiAnchorLink).click(function(e) { e.stopPropagation(); var id = $(this).data('anchor'); if (!id || !$('#' + id).length) return; var same = eiAnchorCurrent === id; if (eiAnchorCurrent) eiAnchorReset(eiAnchorCurrent); if (!same) { $('#' + id).fadeOut(0).insertAfter(eiAnchorContent).fadeIn(eiAnchorFadeIn); $(this).addClass(eiAnchorActive); $('body').addClass(eiAnchorPrefix); eiAnchorCurrent = id; } else { eiAnchorCurrent = null; } }); $(document).click(function(e) { if (eiAnchorCurrent && !$(e.target).closest(eiAnchorSet + ',' + eiAnchorLink).length) { eiAnchorReset(eiAnchorCurrent); eiAnchorCurrent = null; } }); });