Skip to content

Instantly share code, notes, and snippets.

@srph
Created June 5, 2017 02:43
Show Gist options
  • Select an option

  • Save srph/8a6e21e5de82c01df2e8047b207e818f to your computer and use it in GitHub Desktop.

Select an option

Save srph/8a6e21e5de82c01df2e8047b207e818f to your computer and use it in GitHub Desktop.
JS: Get placeholder index
/**
* Get placeholder index, like for example in a list, through node top offset.
* Used for drag-n-drops.
*
* @param {number} y The actual top offset
* @param {number} scroll Scroll count
* @return {number}
*/
function getPlaceholderIndex(y, scroll) {
const height = 70;
const margin = 20;
if (y < height / 2) {
return 0;
}
// Just a little explanation why:
// In general, you use the formula `y / height = offset`.
// However, `y` always starts from where the card was dragged,
// thus `(y / (height / 2)) / height`.
return Math.floor((y - (height / 2)) / (height + margin));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment