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