mymth / vanillajs-datepicker

A vanilla JavaScript remake of bootstrap-datepicker for Bulma and other CSS frameworks
MIT License
719 stars 147 forks source link

Mousewheel support #176

Open Bilge opened 5 months ago

Bilge commented 5 months ago

Hi, I would use this library if it had mouse-wheel support for scrolling years.

mymth commented 4 months ago

It doesn't seem to me that to scroll years is a preference common to many. Some may want to scroll months, others may want to move the focused date. Some may want it to work only when a modifier key is pressed, others may think it's totally unnecessary. So, I think it's better to be done by the user on their app.

You should be able to do it by something like this:

const elem = document.querySelector('.date');
const dp = new Datepicker(elem);

elem.addEventListener('wheel', (ev) => {
  const date = dp.getFocusedDate();
  const year = date.getFullYear() + (ev.deltaY > 0 ? 1 : -1);

  dp.setFocusedDate(date.setFullYear(year));
  ev.preventDefault();
});