The scroll-snap-type property for CSS specifies whether a scroll container is a scroll snap container, how strictly it snaps, and which axes are considered.
If no strictness value is specified, proximity is assumed.
object.style.scrollSnapType = "value"; <'scroll-snap-type'> = none | [ x | y | block | inline | both ] [ mandatory | proximity ]? none The scroll container must not snap.
x The scroll container snaps to snap positions in its horizontal axis only.
y The scroll container snaps to snap positions in its vertical axis only.
block The scroll container snaps to snap positions in its block axis only.
inline The scroll container snaps to snap positions in its inline axis only.
both The scroll container snaps to snap positions in both of its axes independently.
mandatory The scroll container is required to be snapped to a snap position when there are no active scrolling operations.
proximity The scroll container may snap to a snap position at the termination of a scroll, at the discretion of the user agent given the parameters of the scroll.
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: grid;
grid-template-columns: repeat(3, 100%);
grid-template-rows: repeat(3, 100%);
height: 50vh;
overflow: scroll;
scroll-snap-type: x;
width: 50%;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
scroll-snap-align: start;
}
</style>
</head>
<body>
<div class="myclass">
<div>x</div>
<div>x</div>
<div>x</div>
<div>x</div>
<div>x</div>
<div>x</div>
<div>x</div>
<div>x</div>
<div>x</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: grid;
grid-template-columns: repeat(3, 100%);
grid-template-rows: repeat(3, 100%);
height: 50vh;
overflow: scroll;
scroll-snap-type: y;
width: 50%;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
scroll-snap-align: start;
}
</style>
</head>
<body>
<div class="myclass">
<div>y</div>
<div>y</div>
<div>y</div>
<div>y</div>
<div>y</div>
<div>y</div>
<div>y</div>
<div>y</div>
<div>y</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: grid;
grid-template-columns: repeat(3, 100%);
grid-template-rows: repeat(3, 100%);
height: 50vh;
overflow: scroll;
scroll-snap-type: block;
width: 50%;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
scroll-snap-align: start;
}
</style>
</head>
<body>
<div class="myclass">
<div>block</div>
<div>block</div>
<div>block</div>
<div>block</div>
<div>block</div>
<div>block</div>
<div>block</div>
<div>block</div>
<div>block</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: grid;
grid-template-columns: repeat(3, 100%);
grid-template-rows: repeat(3, 100%);
height: 50vh;
overflow: scroll;
scroll-snap-type: inline;
width: 50%;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
scroll-snap-align: start;
}
</style>
</head>
<body>
<div class="myclass">
<div>inline</div>
<div>inline</div>
<div>inline</div>
<div>inline</div>
<div>inline</div>
<div>inline</div>
<div>inline</div>
<div>inline</div>
<div>inline</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: grid;
grid-template-columns: repeat(3, 100%);
grid-template-rows: repeat(3, 100%);
height: 50vh;
overflow: scroll;
scroll-snap-type: both;
width: 50%;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
scroll-snap-align: start;
}
</style>
</head>
<body>
<div class="myclass">
<div>both</div>
<div>both</div>
<div>both</div>
<div>both</div>
<div>both</div>
<div>both</div>
<div>both</div>
<div>both</div>
<div>both</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: grid;
grid-template-columns: repeat(3, 100%);
grid-template-rows: repeat(3, 100%);
height: 50vh;
overflow: scroll;
scroll-snap-type: both mandatory;
width: 50%;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
scroll-snap-align: start;
}
</style>
</head>
<body>
<div class="myclass">
<div>mandatory</div>
<div>mandatory</div>
<div>mandatory</div>
<div>mandatory</div>
<div>mandatory</div>
<div>mandatory</div>
<div>mandatory</div>
<div>mandatory</div>
<div>mandatory</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: grid;
grid-template-columns: repeat(3, 100%);
grid-template-rows: repeat(3, 100%);
height: 50vh;
overflow: scroll;
scroll-snap-type: both proximity;
width: 50%;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
scroll-snap-align: start;
}
</style>
</head>
<body>
<div class="myclass">
<div>proximity</div>
<div>proximity</div>
<div>proximity</div>
<div>proximity</div>
<div>proximity</div>
<div>proximity</div>
<div>proximity</div>
<div>proximity</div>
<div>proximity</div>
</div>
</body>
</html>
<!doctype html>
<html>
<head>
<style>
.myclass
{
display: grid;
grid-template-columns: repeat(3, 100%);
grid-template-rows: repeat(3, 100%);
height: 50vh;
overflow: scroll;
width: 50%;
}
.myclass > div
{
background-image: linear-gradient(135deg, white, lightgray);
scroll-snap-align: start;
}
</style>
</head>
<body>
<button>initial</button>
<button>none</button>
<button>x</button>
<button>y</button>
<button>block</button>
<button>inline</button>
<button>both</button>
<button value="both mandatory">mandatory</button>
<button value="both proximity">proximity</button>
<div class="myclass">
<div>scroll-snap-type</div>
<div>scroll-snap-type</div>
<div>scroll-snap-type</div>
<div>scroll-snap-type</div>
<div>scroll-snap-type</div>
<div>scroll-snap-type</div>
<div>scroll-snap-type</div>
<div>scroll-snap-type</div>
<div>scroll-snap-type</div>
</div>
<script>
function myfunction(myparameter)
{
const mytarget = myparameter.target;
const myproperty = mytarget.value || mytarget.innerHTML;
document.querySelector(".myclass").style.scrollSnapType = myproperty;
}
for(const mybutton of document.querySelectorAll("button"))
{
mybutton.addEventListener("mouseover", myfunction);
}
</script>
</body>
</html>