Back to Bootstrap class

Method scrollspy

public static void
scrollspy
(mixed $selector = '', mixed $options = [])
Add javascript support for Bootstrap Scrollspy
Parameters
  • string $selector The ID selector for the ScrollSpy element.
  • array $options An array of options for the ScrollSpy.
Returns
  • void
Since
  • 3.0
Class: Bootstrap
Project: Joomla

Method scrollspy - Source code

/**
 * Add javascript support for Bootstrap Scrollspy
 *
 * @param   string  $selector  The ID selector for the ScrollSpy element.
 * @param   array   $options   An array of options for the ScrollSpy.
 *
 * @return  void
 *
 * @since   3.0
 *
 * Options for the Scrollspy can be:
 * - offset  number  Pixels to offset from top when calculating position of scroll.
 * - method  string  Finds which section the spied element is in.
 * - target  string  Specifies element to apply Scrollspy plugin.
 */
public static function scrollspy($selector = '', $options = []) : void
{
    // Only load once
    if (isset(static::$loaded[__METHOD__][$selector])) {
        return;
    }
    if ($selector !== '') {
        // Setup options object
        $opt['offset'] = isset($options['offset']) ? (int) $options['offset'] : 10;
        $opt['method'] = isset($options['method']) ? $options['method'] : 'auto';
        $opt['target'] = isset($options['target']) ? $options['target'] : null;
        Factory::getDocument()->addScriptOptions('bootstrap.scrollspy', [$selector => (object) array_filter((array) $opt)]);
    }
    // Include the Bootstrap component
    Factory::getApplication()->getDocument()->getWebAssetManager()->useScript('bootstrap.scrollspy');
    static::$loaded[__METHOD__][$selector] = true;
}