These data attributes allow you to set options for the scrollto component during auto initialization.
Option
Type
Default
Description
data-kt-scrollto-parent
string
"body"
Defines the parent element to which the scroll will be applied. When scrolling, the specified element will be the container that handles the scroll behavior. E.g: #parent_element_id .
data-kt-scrollto-target
Selectors
This table details the custom classes and data attributes used by the scrollto component.
Name
Description
Data Attributes
data-kt-scrollto
Used to auto-initialize KTScrollto instances on page load and provides a string value serving as an ID selector, #element_id , for a target element.
Methods
Use KTScrollto component's API methods to programmatically control its behavior.
Method
Description
new KTScrollto(element, options)
Creates an object instance of KTScrollto class for the given DOM element and configuration options .
scroll()
Initiates a scroll action to the target element defined by the data-kt-scrollto attribute or target configuration option.
getOption(name)
Retrieves the value of a configuration option by parameter from a KTScrollto instance.
// Selects element with ID 'my_scrollto'const scrolltoEl = document.querySelector('#my_scrollto');// Gets existing KTScrollto instance for scrolltoElconst scrollto = KTScrollto.getInstance(scrolltoEl);// Creates new KTScrollto instance with optionsconst scrollto2 = new KTScrollto(scrolltoEl, { // Enables smooth scrolling smooth: true, // Sets 'body' as parent container parent: 'body', // Target element to scroll to target: '#my_element', // Adds 20px offset offset: 20,});// Triggers scroll to targetscrollto.scroll();scrollto2.scroll
Utilities
Manage and interact with KTScrollto instances using these static methods of KTScrollto JavaScript class.
Method
Description
init()
Automatically initializes KTScrollto object for all elements with the data-kt-scrollto="true" attribute on page load.
createInstances()
Allows to create KTScrollto instances for all elements that have been dynamically added to the DOM but haven't been activated yet.
getInstance(element)
Returns the KTScrollto object associated with the given DOM element .
// Initialize all scrolltosKTScrollto.init();// Initialzie pending scrolltosKTScrollto.createInstances();// Get scrollto objectconst scrolltoEl = document.querySelector('#my_scrollto');const scrollto = KTScrollto.getInstance(scrolltoEl);
TypeScript
Import the component and types from @keenthemes/ktui for typed options and instance:
import { KTScrollto, type KTScrolltoConfigInterface, type KTScrolltoInterface,} from '@keenthemes/ktui';const scrolltoEl = document.querySelector<HTMLElement>('#my_scrollto');if (!scrolltoEl) return;const scrollto: KTScrolltoInterface | null = KTScrollto.getInstance(scrolltoEl);if (scrollto) scrollto.scroll();const options: KTScrolltoConfigInterface = {};const instance: KTScrolltoInterface = new KTScrollto(scrolltoEl, options);
Specifies the target element to apply the scroll effect. When activated, the scroll behavior will be handled within this container. For example: #target_element_id .
data-kt-scrollto-smooth
boolean
true
Enables or disables smooth scrolling. If set to true , the scrolling will be animated smoothly to the target element.
data-kt-scrollto-offset
number
0
Specifies an offset to be applied when scrolling to the target element. This is useful for adjusting the scroll position, especially if there are fixed elements at the top of the page.
name
getElement()
Retrieves the DOM element linked to a specific KTScrollto instance.
dispose()
Removes the KTScrollto instance from an element, including any associated data stored on the DOM element.
();
element
getOrCreateInstance(element)
Returns the existing KTScrollto object for the provided DOM element element , or creates a new instance if none exists, then returns the same.