1/** 2 * @file 3 * JavaScript behaviors for webform scroll top. 4 */ 5 6(function ($, Drupal) { 7 8 'use strict'; 9 10 Drupal.webform = Drupal.webform || {}; 11 // Allow scrollTopOffset to be custom defined or based on whether there is a 12 // floating toolbar. 13 Drupal.webform.scrollTopOffset = Drupal.webform.scrollTopOffset || ($('#toolbar-administration').length ? 140 : 10); 14 15 /** 16 * Scroll to top ajax command. 17 * 18 * @param {Element} element 19 * The element to scroll to. 20 * @param {string} target 21 * Scroll to target. (form or page) 22 */ 23 Drupal.webformScrollTop = function (element, target) { 24 if (!target) { 25 return; 26 } 27 28 var $element = $(element); 29 30 // Scroll to the top of the view. This will allow users 31 // to browse newly loaded content after e.g. clicking a pager 32 // link. 33 var offset = $element.offset(); 34 // We can't guarantee that the scrollable object should be 35 // the body, as the view could be embedded in something 36 // more complex such as a modal popup. Recurse up the DOM 37 // and scroll the first element that has a non-zero top. 38 var $scrollTarget = $element; 39 while ($scrollTarget.scrollTop() === 0 && $($scrollTarget).parent()) { 40 $scrollTarget = $scrollTarget.parent(); 41 } 42 43 if (target === 'page' && $scrollTarget.length && $scrollTarget[0].tagName === 'HTML') { 44 // Scroll to top when scroll target is the entire page. 45 // @see https://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport 46 var rect = $($scrollTarget)[0].getBoundingClientRect(); 47 if (!(rect.top >= 0 && rect.left >= 0 && rect.bottom <= $(window).height() && rect.right <= $(window).width())) { 48 $scrollTarget.animate({scrollTop: 0}, 500); 49 } 50 } 51 else { 52 // Only scroll upward. 53 if (offset.top - Drupal.webform.scrollTopOffset < $scrollTarget.scrollTop()) { 54 $scrollTarget.animate({scrollTop: (offset.top - Drupal.webform.scrollTopOffset)}, 500); 55 } 56 } 57 }; 58 59 /** 60 * Scroll element into view. 61 * 62 * @param {jQuery} $element 63 * An element. 64 */ 65 Drupal.webformScrolledIntoView = function ($element) { 66 if (!Drupal.webformIsScrolledIntoView($element)) { 67 $('html, body').animate({scrollTop: $element.offset().top - Drupal.webform.scrollTopOffset}, 500); 68 } 69 }; 70 71 /** 72 * Determine if element is visible in the viewport. 73 * 74 * @param {Element} element 75 * An element. 76 * 77 * @return {boolean} 78 * TRUE if element is visible in the viewport. 79 * 80 * @see https://stackoverflow.com/questions/487073/check-if-element-is-visible-after-scrolling 81 */ 82 Drupal.webformIsScrolledIntoView = function (element) { 83 var docViewTop = $(window).scrollTop(); 84 var docViewBottom = docViewTop + $(window).height(); 85 86 var elemTop = $(element).offset().top; 87 var elemBottom = elemTop + $(element).height(); 88 89 return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)); 90 }; 91 92})(jQuery, Drupal);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.