Through the event scroll
.
let scrollable = document.querySelector('.scrollable');
scrollable.addEventListener('scroll', function (e) {
console.log('scrolling');
});
body {
overflow: hidden;
}
.scrollable {
background-color: coral;
height: 300px;
margin: 20px auto;
overflow: auto;
padding: .5rem;
width: 300px;
}
.box {
background-color: turquoise;
height: 1000px;
margin: 0 auto;
width: 70px;
}
<div class="scrollable">
<div class="box"></div>
</div>
Update
Regarding your comment:
I need to know when I pass from one to the other , that is, detect when I pass in several elements
You can implement the Observer
pattern to know when to scrolls each element. To keep things simple, I will use for this example the micro-library MicroEvent
to implement this pattern. You can do it manually with only conditional, but I like the reactive model more. It is up to you to decide
let scrollables = document.querySelectorAll('.scrollable');
let currentContent = null;
MicroEvent.mixin(Emitter);
let emitter = new Emitter();
/* Escucha por el evento 'scrollChange' */
emitter.bind('scrollChange', function(id) {
console.log('Se ha cambiado al ememento #${id}');
});
[].forEach.call(scrollables, function (scrollable) {
scrollable.addEventListener('scroll', function (e) {
let id = scrollable.id
if (!currentContent || currentContent !== id) {
currentContent = id;
emitter.emit(id);
}
});
});
/* Es ecesario crear una función para que funcione MicroEvent */
function Emitter() { };
/* Método encargado de emitir el evento 'scrollChange' */
Emitter.prototype.emit = function (id) {
this.trigger('scrollChange', id);
}
.scrollable {
background-color: coral;
height: 300px;
margin: 20px auto;
overflow: auto;
padding: .5rem;
width: 300px;
}
.box {
background-color: turquoise;
height: 1000px;
margin: 0 auto;
width: 70px;
}
<script src="https://cdn.rawgit.com/jeromeetienne/microevent.js/master/microevent.js"></script>
<div class="scrollable" id="content1">
<div class="box"></div>
</div>
<div class="scrollable" id="content2">
<div class="box"></div>
</div>
<div class="scrollable" id="content3">
<div class="box"></div>
</div>