Created
March 16, 2016 21:12
-
-
Save Scapal/7223021ccc6b22a121b6 to your computer and use it in GitHub Desktop.
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
import {inject, customAttribute, children} from 'aurelia-framework'; | |
function calcOuterHeight(element){ | |
var height; | |
height = element.getBoundingClientRect().height; | |
height += getStyleValue(element, 'marginTop'); | |
height += getStyleValue(element, 'marginBottom'); | |
return height; | |
} | |
function calcScrollHeight(element){ | |
var height; | |
height = element.getBoundingClientRect().height; | |
height -= getStyleValue(element, 'borderTopWidth'); | |
height -= getStyleValue(element, 'borderBottomWidth'); | |
return height; | |
} | |
function getStyleValue(element, style){ | |
var currentStyle, styleValue; | |
currentStyle = element.currentStyle || window.getComputedStyle(element); | |
styleValue = parseInt(currentStyle[style]); | |
return Number.isNaN(styleValue) ? 0 : styleValue; | |
} | |
@customAttribute('scroll-index') | |
@inject(Element) | |
export class ScrollIndex { | |
@children('*') | |
items; | |
constructor(element) { | |
this.element = element; | |
this.scrollListener = () => this._onScroll(); | |
} | |
attached() { | |
this.itemHeight = calcOuterHeight(this.items[0]); | |
console.log(`itemHeight: ${this.itemHeight}`); | |
this.scrollHeight = calcScrollHeight(this.element); | |
console.log(`scrollHeight: ${this.scrollHeight}`); | |
this.element.addEventListener('scroll', this.scrollListener); | |
} | |
detached() { | |
this.element.removeEventListener('scroll', this.scrollListener); | |
this.itemHeight = 0; | |
} | |
_onScroll() { | |
let scrollTop = this.element.scrollTop; | |
this.value = scrollTop / this.itemHeight; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage: