Parallax Scroll Effect in Jetpack Compose: Multi-layer Headers
Parallax scrolling creates depth by moving background and foreground elements at different speeds.
Detecting Scroll Offset
val scrollState = rememberScrollState()
LazyColumn(state = scrollState) {
item {
val offset = (scrollState.value / 3).toFloat()
ParallaxHeader(offset = offset)
}
items(100) { index ->
Text("Item $index", modifier = Modifier.padding(16.dp))
}
}
Parallax Header Implementation
@Composable
fun ParallaxHeader(offset: Float) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(250.dp)
.graphicsLayer {
translationY = -offset
}
.background(
brush = Brush.verticalGradient(
colors = listOf(Color.Blue, Color.Cyan)
)
)
)
}
Scroll Aware Transformation
val collapseFraction = (scrollState.value / 500f).coerceIn(0f, 1f)
Box(
modifier = Modifier
.height(250.dp * (1 - collapseFraction))
.graphicsLayer {
alpha = 1f - collapseFraction
}
)
Parallax effects enhance visual depth and create engaging scrolling experiences that delight users.
8 Android app templates available on Gumroad
Top comments (0)