Created
June 5, 2017 02:43
-
-
Save srph/8a6e21e5de82c01df2e8047b207e818f to your computer and use it in GitHub Desktop.
JS: Get placeholder index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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