Skip to content

Snippet: ScrollTo and Open VC Toggle/FAQ

// ScrollTo and Open VC Toggle/FAQ
// Usage: Give your toggle a unique ID
//        Then link to it like this site.com/?toggle-id
add_action( 'wp_footer', function() { ?>

	

		( function( $ ) {

			'use strict';

			// Run on window load (best imo)
			$( window ).on( 'load', function() {

				// Get current URl and check the end to see if we are trying to link to a div
				var url         = location.href,
					urlParts    = url.split('/'),
					urlLastPart = urlParts[urlParts.length-1],
					urlLastPart = urlLastPart.replace( '?', '' ),
					maybeToggle = $( '#' + urlLastPart );
				
				// Return if no toggles found matching URL
				if ( ! maybeToggle || ! maybeToggle.hasClass( 'vc_toggle' ) || ! wpex ) {
					return;
				}

				// Toggle to open
				var $toggle = maybeToggle;

				// Get toggle offset for scrolling to it
				// We check the wpex core javascript to grab an offset incase sticky header
				// is enabled
				var offset = $toggle.offset().top - wpex.config.localScrollOffset - 20;

				// Scroll to toggle
				$( 'html, body' ).stop( true, true ).animate( {
					scrollTop: offset
				}, 500, function() {

					// Open toggle by triggering it's click event after we scroll to it
					$toggle.find( '.vc_toggle_title' ).trigger( 'click' );

				} );

			} );

		} ( jQuery ) );

	

<?php }, 50 );
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top