We are using the below code to scroll to the top of page after search results updated, but our view is place in the middle of the page, is it possible to scroll to the top of the view or a page element ID?
jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function() {
/**
* data.view_unique_id (string) The View unique ID hash
* data.layout (object) The jQuery object for the View layout wrapper
*/
jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
You can scroll to any element by ID using the jQuery offset method to calculate its position in the document:
jQuery('html, body').animate({ scrollTop: jQuery("#elementtoScrollToID").offset().top }, 'slow');
http://api.jquery.com/offset/
I tried the below code but it's not working -
jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function() {
jQuery('html, body').animate({ scrollTop: jQuery("#search-space").offset().top }, 'slow');
Am I missing something?
May I see this View on your site? I would like to see what's happening in the browser.
hidden link (bottom of the page)
The code is repeated, and both times it's missing a closing brace and parenthesis. You can see the error if you open the browser console - "Unexpected end of input". Try this:
// View: Available space with Sort - start
//-----------------------------------------
jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function() {
/**
* data.view_unique_id (string) The View unique ID hash
* data.layout (object) The jQuery object for the View layout wrapper
*/
jQuery('html, body').animate({ scrollTop: jQuery("#search-space").offset().top }, 'slow');
}); // this line was missing before
//-----------------------------------------
// View: Available space with Sort - end
//-----------------------------------------
Amazing that worked! Thank you so much!
Hi Christian, not sure if you can help on this thread anymore however this response not working for me:
I have search form on home page, which when submitted goes to my product page, this works fine, but i need it to then automatically scroll down to an id i have labelled #product-select.
i have used your code to say the following:
jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function() {
/**
* data.view_unique_id (string) The View unique ID hash
* data.layout (object) The jQuery object for the View layout wrapper
*/
jQuery('html, body').animate({ scrollTop: jQuery("#product-select").offset().top }, 'slow');
}); // this line was missing before
but am having no joy with the scroll. it stays at top of page and the url doesnt change to show an added ID.
Can you advise?