Class Sticky


public class Sticky extends StyledElement<Sticky>
A container that sticks to a specified position when scrolling.

Sticky provides CSS position: sticky behavior, allowing an element to scroll normally until it reaches a threshold, then "sticking" in place relative to its nearest scrolling ancestor.

Common Use Cases:

  • Sticky headers that remain visible when scrolling down
  • Sticky sidebars that follow the viewport
  • Sticky footers within scrollable containers
  • Sticky table headers

Usage:


 // Sticky header at top
 Sticky header = Sticky.top(0);
 header.addElement(navbar);

 // Sticky with offset
 Sticky sidebar = Sticky.top(80);  // 80px from top
 sidebar.addElement(navigation);

 // Sticky at bottom
 Sticky footer = Sticky.bottom(0);

 // With background and z-index
 Sticky header = Sticky.top(0)
     .setBackground("#ffffff")
     .setZIndex(100);
 

Note: For sticky positioning to work, the parent container must have enough content to scroll, and the sticky element should not have overflow: hidden on any ancestor.

Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Sticky

      public Sticky()
      Constructs a Sticky container with position: sticky and top: 0.

      By default, the element sticks to the top of its scrolling container. Use setTop(int), setBottom(int), setLeft(int), or setRight(int) to change the sticky position.

    • Sticky

      public Sticky(Element<?> content)
      Constructs a Sticky container wrapping the specified element.
      Parameters:
      content - the element to wrap
  • Method Details

    • top

      public static Sticky top(int offset)
      Creates a Sticky container that sticks to the top of its container.
      Parameters:
      offset - the distance from the top in pixels (0 for flush with top)
      Returns:
      a new Sticky container configured for top positioning
    • top

      public static Sticky top(String offset)
      Creates a Sticky container that sticks to the top of its container.
      Parameters:
      offset - the distance from the top (e.g., "0", "80px", "5rem")
      Returns:
      a new Sticky container configured for top positioning
    • bottom

      public static Sticky bottom(int offset)
      Creates a Sticky container that sticks to the bottom of its container.
      Parameters:
      offset - the distance from the bottom in pixels
      Returns:
      a new Sticky container configured for bottom positioning
    • bottom

      public static Sticky bottom(String offset)
      Creates a Sticky container that sticks to the bottom of its container.
      Parameters:
      offset - the distance from the bottom (e.g., "0", "16px", "1rem")
      Returns:
      a new Sticky container configured for bottom positioning
    • left

      public static Sticky left(int offset)
      Creates a Sticky container that sticks to the left of its container.

      Useful for horizontal scrolling scenarios.

      Parameters:
      offset - the distance from the left in pixels
      Returns:
      a new Sticky container configured for left positioning
    • right

      public static Sticky right(int offset)
      Creates a Sticky container that sticks to the right of its container.

      Useful for horizontal scrolling scenarios.

      Parameters:
      offset - the distance from the right in pixels
      Returns:
      a new Sticky container configured for right positioning
    • setTop

      public Sticky setTop(int offset)
      Sets the top offset for sticky positioning.
      Overrides:
      setTop in class StyledElement<Sticky>
      Parameters:
      offset - the distance from the top in pixels
      Returns:
      this Sticky for method chaining
    • setTop

      public Sticky setTop(String offset)
      Sets the top offset for sticky positioning.
      Overrides:
      setTop in class StyledElement<Sticky>
      Parameters:
      offset - the distance from the top (e.g., "0", "80px", "5rem")
      Returns:
      this Sticky for method chaining
    • setBottom

      public Sticky setBottom(int offset)
      Sets the bottom offset for sticky positioning.
      Overrides:
      setBottom in class StyledElement<Sticky>
      Parameters:
      offset - the distance from the bottom in pixels
      Returns:
      this Sticky for method chaining
    • setBottom

      public Sticky setBottom(String offset)
      Sets the bottom offset for sticky positioning.
      Overrides:
      setBottom in class StyledElement<Sticky>
      Parameters:
      offset - the distance from the bottom (e.g., "0", "16px", "1rem")
      Returns:
      this Sticky for method chaining
    • setLeft

      public Sticky setLeft(int offset)
      Sets the left offset for sticky positioning.
      Overrides:
      setLeft in class StyledElement<Sticky>
      Parameters:
      offset - the distance from the left in pixels
      Returns:
      this Sticky for method chaining
    • setLeft

      public Sticky setLeft(String offset)
      Sets the left offset for sticky positioning.
      Overrides:
      setLeft in class StyledElement<Sticky>
      Parameters:
      offset - the distance from the left (e.g., "0", "16px", "1rem")
      Returns:
      this Sticky for method chaining
    • setRight

      public Sticky setRight(int offset)
      Sets the right offset for sticky positioning.
      Overrides:
      setRight in class StyledElement<Sticky>
      Parameters:
      offset - the distance from the right in pixels
      Returns:
      this Sticky for method chaining
    • setRight

      public Sticky setRight(String offset)
      Sets the right offset for sticky positioning.
      Overrides:
      setRight in class StyledElement<Sticky>
      Parameters:
      offset - the distance from the right (e.g., "0", "16px", "1rem")
      Returns:
      this Sticky for method chaining
    • setBackground

      public Sticky setBackground(String color)
      Sets a solid background color.

      Useful for ensuring content scrolling behind the sticky element doesn't show through.

      Overrides:
      setBackground in class StyledElement<Sticky>
      Parameters:
      color - the background color (e.g., "#ffffff", "white", "rgba(255,255,255,0.9)")
      Returns:
      this Sticky for method chaining
    • setZIndex

      public Sticky setZIndex(int zIndex)
      Sets the z-index for stacking control.

      Higher values ensure the sticky element appears above other content.

      Overrides:
      setZIndex in class StyledElement<Sticky>
      Parameters:
      zIndex - the z-index value
      Returns:
      this Sticky for method chaining
    • setShadow

      public Sticky setShadow(String shadow)
      Adds a box shadow for visual depth.

      Useful for indicating the sticky element is "floating" above content.

      Parameters:
      shadow - the CSS box-shadow value (e.g., "0 2px 4px rgba(0,0,0,0.1)")
      Returns:
      this Sticky for method chaining
    • withShadow

      public Sticky withShadow()
      Adds a subtle shadow for visual depth.
      Returns:
      this Sticky for method chaining
    • fullWidth

      public Sticky fullWidth()
      Makes the sticky element full width.
      Returns:
      this Sticky for method chaining