Performance: View transitions API
This API affects the smoothness and visual stability of page transitions, improving perceived speed and reducing layout shifts.
document.startViewTransition(() => { document.body.innerHTML = newContent; });document.body.innerHTML = newContent; // direct DOM replacement without animation
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct DOM swap without animation | High (full content replace) | Multiple reflows | High paint cost due to sudden changes | [X] Bad |
| Using View Transitions API | Moderate (batched updates) | Single reflow with animation | Lower paint cost with smooth compositing | [OK] Good |
startViewTransition method in the View Transitions API?startViewTransitionstartViewTransition method takes a callback function that performs DOM updates.startViewTransition with a function. Options A, B, and D use invalid syntax or assignment.startViewTransition(() => {
document.getElementById('content').textContent = 'New content';
});
console.log(document.getElementById('content').textContent);startViewTransition runs synchronously, so the DOM textContent changes immediately.startViewTransition(() => {
const newDiv = document.createElement('div');
newDiv.textContent = 'Hello';
});<div id='message'> only if the new text is different from the current text. Which code correctly uses the View Transitions API to do this efficiently?Pass the test and this pattern moves unknown