lots0logs
6/15/2016 - 4:53 PM

WordPress :: Divi Builder :: Reinitialize Accordions & Toggles After AJAX Page Load

WordPress :: Divi Builder :: Reinitialize Accordions & Toggles After AJAX Page Load

<script>
(function($) {
	$(document).on('js_event_wpv_parametric_search_results_updated', function() {
		$('.et_pb_toggle_title').click(function() {
			var $this_heading = $(this),
				$module = $this_heading.closest('.et_pb_toggle'),
				$section = $module.parents('.et_pb_section'),
				$content = $module.find('.et_pb_toggle_content'),
				$accordion = $module.closest('.et_pb_accordion'),
				is_accordion = $accordion.length,
				is_accordion_toggling = $accordion.hasClass('et_pb_accordion_toggling'),
				window_offset_top = $(window).scrollTop(),
				fixed_header_height = 0,
				$accordion_active_toggle,
				module_offset;

			if (is_accordion) {
				if ($module.hasClass('et_pb_toggle_open') || is_accordion_toggling) {
					return false;
				}

				$accordion.addClass('et_pb_accordion_toggling');
				$accordion_active_toggle = $module.siblings('.et_pb_toggle_open');
			}

			if ($content.is(':animated')) {
				return;
			}

			$content.slideToggle(700, function() {
				if ($module.hasClass('et_pb_toggle_close')) {
					$module.removeClass('et_pb_toggle_close').addClass('et_pb_toggle_open');
				} else {
					$module.removeClass('et_pb_toggle_open').addClass('et_pb_toggle_close');
				}

				if ($section.hasClass('et_pb_section_parallax') && !$section.children().hasClass('et_pb_parallax_css')) {
					$.proxy(et_parallax_set_height, $section)();
				}
			});

			if (is_accordion) {
				$accordion_active_toggle.find('.et_pb_toggle_content').slideToggle(700, function() {
					$accordion_active_toggle.removeClass('et_pb_toggle_open').addClass('et_pb_toggle_close');
					$accordion.removeClass('et_pb_accordion_toggling');

					module_offset = $module.offset();

					// Calculate height of fixed nav
					if ($('#wpadminbar').length) {
						fixed_header_height += $('#wpadminbar').height();
					}

					if ($('#top-header').length) {
						fixed_header_height += $('#top-header').height();
					}

					if ($('#main-header').length) {
						fixed_header_height += $('#main-header').height();
					}

					// Compare accordion offset against window's offset and adjust accordingly
					if ((window_offset_top + fixed_header_height) > module_offset.top) {
						$('html, body').animate({
							scrollTop: (module_offset.top - fixed_header_height - 50)
						});
					}
				});
			}
		});
	});
})(jQuery);
</script>