Skip to content

Instantly share code, notes, and snippets.

@sdesai
Created July 25, 2011 00:08
Show Gist options
  • Save sdesai/1103272 to your computer and use it in GitHub Desktop.
Save sdesai/1103272 to your computer and use it in GitHub Desktop.
Normalizing scrollWidth/scrollHeight
/**
* Utility method to obtain scrollWidth, scrollHeight,
* accounting for the impact of translate on scrollWidth, scrollHeight
* @method _getScrollDims
* @returns {Array} The scrollWidth and scrollHeight as an array: [scrollWidth, scrollHeight]
* @private
*/
_getScrollDims: function() {
var dims,
matrix,
origX,
origY,
cb = this.get("contentBox"),
bb = this.get("boundingBox");
if (NATIVE_TRANSITIONS) {
// TOD: Is this OK? Just in case it's called 'during' a transition.
cb.setStyle(TRANS.DURATION, ZERO);
cb.setStyle(TRANS.PROPERTY, EMPTY);
// Ideally using CSSMatrix - don't think we have it normalized yet though.
// origX = (new WebKitCSSMatrix(cb.getComputedStyle("transform"))).e;
// origY = (new WebKitCSSMatrix(cb.getComputedStyle("transform"))).f;
origX = this.get(SCROLL_X);
origY = this.get(SCROLL_Y);
cb.setStyle('transform', this._transform(0, 0));
}
// Use bb instead of cb. cb doesn't gives us the right results in FF (due to overflow:hidden)
dims = [bb.get('scrollWidth'), bb.get('scrollHeight')];
if (NATIVE_TRANSITIONS) {
cb.setStyle('transform', this._transform(origX, origY));
}
return dims;
}
/**
* This method gets invoked whenever the height or width attributes change,
* allowing us to determine which scrolling axes need to be enabled.
*
* @method _uiDimensionsChange
* @protected
*/
_uiDimensionsChange: function() {
var sv = this,
bb = sv._bb,
CLASS_NAMES = ScrollView.CLASS_NAMES,
width = bb.get('offsetWidth'),
height = bb.get('offsetHeight'),
scrollDims = this._getScrollDims(),
scrollWidth = scrollDims[0],
scrollHeight = scrollDims[1];
if (height && scrollHeight > height) {
sv._scrollsVertical = true;
sv._maxScrollY = scrollHeight - height;
sv._minScrollY = 0;
sv._scrollHeight = scrollHeight;
bb.addClass(CLASS_NAMES.vertical);
}
/* else {
sv._scrollsVertical = false;
sv._minScrollY = 0,
sv._maxScrollY = height;
}*/
if (width && scrollWidth > width) {
sv._scrollsHorizontal = true;
sv._maxScrollX = scrollWidth - width;
sv._minScrollX = 0;
sv._scrollWidth = scrollWidth;
bb.addClass(CLASS_NAMES.horizontal);
}
/* else {
sv._scrollsHorizontal = false;
sv._minScrollX = 0,
sv._maxScrollX = width;
}
*/
/**
* Internal state, defines whether or not the scrollview can scroll vertically
*
* @property _scrollsVertical
* @type boolean
* @protected
*/
/**
* Internal state, defines the maximum amount that the scrollview can be scrolled along the Y axis
*
* @property _maxScrollY
* @type number
* @protected
*/
/**
* Internal state, defines the minimum amount that the scrollview can be scrolled along the Y axis
*
* @property _minScrollY
* @type number
* @protected
*/
/**
* Internal state, cached scrollHeight, for performance
*
* @property _scrollHeight
* @type number
* @protected
*/
/**
* Internal state, defines whether or not the scrollview can scroll horizontally
*
* @property _scrollsHorizontal
* @type boolean
* @protected
*/
/**
* Internal state, defines the maximum amount that the scrollview can be scrolled along the X axis
*
* @property _maxScrollX
* @type number
* @protected
*/
/**
* Internal state, defines the minimum amount that the scrollview can be scrolled along the X axis
*
* @property _minScrollX
* @type number
* @protected
*/
/**
* Internal state, cached scrollWidth, for performance
*
* @property _scrollWidth
* @type number
* @protected
*/
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment