I want to use a responsive div with a scrolling background image.
In the example I showed it small and large. I want the entire scroll to be constant - so the small div should take x seconds and the large div should also take x seconds (rather than the small one taking less time to complete a whole image pan than the larger one).
I've tried using percentage values in background-position-x but it stops the animation.
.offset {
background-image: url('https://upload.wikimedia.org/wikipedia/commons/c/c4/PM5544_with_non-PAL_signals.png');
background-size: 100%;
animation: slideLeft768px 5s linear infinite;
}
.div1 {
width: 76.8px;
height: 57.6px;
}
.div2 {
width: 768px;
height: 576px;
}
@keyframes slideLeft768px {
0% {
background-position-x: 768px;
}
100% {
background-position-x: 0px;
}
}
<div class="offset div1"></div>
<div class="offset div2"></div>
====================
This is based on Temani Afif's answer:
.offset {
background-image: url('https://upload.wikimedia.org/wikipedia/commons/c/c4/PM5544_with_non-PAL_signals.png');
background-size: 100%;
animation: slideLeft 5s linear infinite;
width: var(--p);
--p: 40vw;
height: 30vw;
}
.div1 {
--p: 12vw;
height: 9vw;
}
@keyframes slideLeft {
0% {
background-position-x: var(--p);
}
100% {
background-position-x: 0px;
}
}
<div class="offset div1"></div>
<div class="offset div2"></div>
I made it only use responsive units so it adjusts when you resize the window.