Make elements remain at the top of the viewport, like a sticky navigation.
To keep an element remaining at the top of the viewport when scrolling down the site, wrap the element with the vk-sticky
component.
<vk-sticky><div>...</div></vk-sticky>
Note Example elements on this page are only sticky until you scroll down to the next heading and disappear afterwards. This was done so as not to clutter your screen with all variations of sticky containers.
You can position the element below the viewport edge. Set the offset
prop with the distance in pixels.
<vk-sticky :offset="100"></vk-sticky>
To apply the sticky behavior with a delay, set the top
prop, which can be defined either by a number representing the offset in pixels, but also viewport height or a CSS selector.
<!-- sticks after 100px of scrolling -->
<vk-sticky :top="100"></vk-sticky>
<vk-sticky :top="100"></vk-sticky>
<!-- sticks after 100vh -->
<vk-sticky top="100vh"></vk-sticky>
<!-- sticks to the top of the container -->
<div id="my-id">
<vk-sticky top="#my-id"></vk-sticky>
</div>
Set an animation
prop with one of the UIkit Animations without the prefix in order to have the sticky element reappear smoothly.
<vk-sticky animation="slide-top"></vk-sticky>
You can make the sticky element show only when scrolling up to save space. Together with an animation, this makes for a very smooth experience.
<vk-sticky show-on-up></vk-sticky>
Bind the sticky behavior to a specific element, so that it disappears after scrolling past that point of the page.
<!-- sticks until the bottom of its parent container -->
<div>
<vk-sticky bottom></vk-sticky>
</div>
<!-- sticks until the second container -->
<vk-sticky bottom="#my-id"></vk-sticky>
<div id="my-id"></div>
You also have the possibility of disabling the sticky behavior for different devices by setting the media
prop with the appropriate viewport width. The element will be sticky from the specified viewport width and upwards, but not below.
<vk-sticky media="640"></vk-sticky>
Name | Type | Default | Description |
---|---|---|---|
top | Number, String | 0 | The top reference when the element should become sticky set in pixels, viewport height or a CSS selector. |
bottom | Boolean, String | false | The bottom reference when the element should stop being sticky. Set a CSS selector or as true to stick until it parent container. |
offset | Number | 0 | Positions the element below the viewport edge, value in pixels. |
animation | String | -- | Animation without the prefix used when transitioning states. |
showOnUp | Boolean | false | When enabled the sticky element will appear only when scrolling up. |
media | Number, String | -- | Disables the sticky behavior for different devices. The element will be sticky from the specified viewport width and upwards, but not below. |
target | Boolean | false | Initially make sure that the Sticky is not over a targeted element via location hash. |