Skip to main content

Swaps

Swaps define how new content replaces or is inserted into existing DOM elements. WSX provides flexible swap strategies with timing and animation controls.

Basic Swap Types

innerHTML (Default)

Replace the content inside an element:

outerHTML

Replace the entire element:

Text Content

Replace only the text content:

Positional Swaps

beforebegin

Insert before the element:

afterbegin

Insert at the beginning of the element:

beforeend

Insert at the end of the element:

afterend

Insert after the element:

Swap Specifications

Client-Side Specification

Use wx-swap attribute to specify swap behavior:

Server-Side Specification

Specify swap in the response:

Timing Modifiers

Swap Timing

Control when the swap occurs:

Settle Timing

Control when animations settle:

Server-Side Timing

Animation and Effects

Fade Effects

Slide Effects

Custom Animations

Scroll Behavior

Auto-Scroll

Control scrolling after swaps:

Server-Side Scroll Control

Best Practices

  1. Choose Appropriate Swap Types: Use the right swap for your use case
  2. Add Smooth Transitions: Use timing modifiers for better UX
  3. Handle Scroll Behavior: Control scrolling for long content
  4. Clean Up Resources: Handle cleanup before swaps
  5. Test Animations: Ensure animations work across browsers
  6. Consider Performance: Avoid expensive animations on slow devices

Next Steps