Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function (root) {
- function __() {
- var Animations = {
- rotateX: function (pct, fromValue, toValue) {
- var deg = fromValue + ((toValue - fromValue) * pct);
- return {
- property: 'transform',
- value: 'rotateX(' + deg + 'deg)';
- }
- },
- rotateY: function (pct, fromValue, toValue) {
- var deg = fromValue + ((toValue - fromValue) * pct);
- return {
- property: 'transform',
- value: 'rotateY(' + deg + 'deg)';
- }
- },
- scale: function (pct, fromValue, toValue) {
- var num = fromValue + ((toValue - fromValue) * pct);
- return {
- property: 'transform',
- value: 'scale(' + num + ')';
- }
- }
- };
- var StyleValuesHandler = {
- transform: function (values) {
- return values.join(' ');
- }
- };
- var Animation = function (el, action, options) {
- options = options || {};
- this.el = el;
- if ('function' === typeof action) {
- this.action = action;
- } else {
- this.action = Animations[action];
- }
- this.from = options.from || 0;
- delete options.from;
- this.fromValue = options.fromValue;
- delete options.from;
- this.to = options.to || 0;
- delete options.to;
- this.toValue = options.toValue;
- delete options.toValue;
- this.options = options;
- }
- Animation.prototype = {
- current: function (y) {
- var pct = (this.to - this.from) / y;
- return this.action(pct, this.fromValue, this.toValue, this.options);
- }
- };
- var Scroller = function () {
- this.animations = [];
- };
- Scroller.prototype = {
- animate: function (el, action, options) {
- var animation = new Animation(el, action, options);
- this.animations.push(animation);
- return this;
- },
- bind: function (win) {
- return win.onScroll = function () {
- var y = win.pageYOffset, styles = {}, animation, current, el, property, value;
- for (var i=0; i<this.animations.length; i++) {
- animation = this.animations[i];
- if (animation.from >= y && animation.to <= y) {
- el = animation.el;
- current = animation.current(y);
- property = current.property;
- value = current.value;
- if (!styles[el]) styles[el] = {};
- if (!styles[el][property]) styles[el][property] = [];
- styles[el][property].push(value);
- }
- }
- var props, $el, value;
- for (var el in styles) {
- props = styles[el];
- $el = document.querySelector(el);
- for (var key in props) {
- value = (props[key].length > 1) ? StyleValuesHandler[key](props[key]) : props[key][0];
- $el.style[key] = value;
- }
- }
- }.bind(this));
- }
- }
- return Scroller;
- };
- if ('function' === typeof define && define.amd) {
- define(__);
- } else {
- root.Scroller = __();
- };
- })(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement