0

How can I disable the scroll to top behavior in an Ajax based View? Preferably, with Javascript.

The scroll behavior is triggered when running a search from an Exposed Form.

1

I found the solution from an old issue.

The scroll behavior is added as an ajax command viewsScrollTop, you can unset it in hook_views_ajax_data_alter.

Example

/**
 * Implement hook_views_ajax_data_alter().
 */
function YOURMODULE_views_ajax_data_alter(&$commands, $view) {
  // Remove scroll to top behaviour from views ajax
  foreach ($commands as $key => $command) {
    if ($command['command'] == 'viewsScrollTop') {
      unset($commands[$key]);
      break;
    }
  }
}
  • 1
    works as expected, thanks mate – David Apr 3 at 14:56
0

The solution above is for Drupal 7. In Drupal 8, hook_views_ajax_data_alter is deprecated and was replaced by hook_ajax_render_alter.

For those of you looking for it, here is how to do it in Drupal 8 (source here).

function YOURMODULE_ajax_render_alter(array &$data) {
  foreach ($data as $key => $command) {
    if ($command['command'] === 'viewsScrollTop') {
      unset($data[$key]);
      break;
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.