From 584d1499034c6b3edd74827ce914a152a3dabef6 Mon Sep 17 00:00:00 2001 From: blackout314 Date: Fri, 26 Jun 2015 21:13:54 +0200 Subject: [PATCH] scrollTo --- package.json | 2 +- src/pi.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 7dc5922..3a09de2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pi.js", - "version": "0.3.0", + "version": "0.3.1", "devDependencies": { "grunt": "~0.4.2", "grunt-contrib-jshint": "~0.6.3", diff --git a/src/pi.js b/src/pi.js index 8ae0f30..21da39d 100644 --- a/src/pi.js +++ b/src/pi.js @@ -39,6 +39,35 @@ Element.prototype.on = Element.prototype.addEventListener; * @example pi('#id').rm('click', callback) */ Element.prototype.rm = Element.prototype.removeEventListener; +/** + * + */ +Element.prototype.getPos = function () { + "use strict"; + var elem = this, + box = elem.getBoundingClientRect(), + body = document.body, + docElem = document.documentElement, + scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop, + scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft, + clientTop = docElem.clientTop || body.clientTop || 0, + clientLeft = docElem.clientLeft || body.clientLeft || 0, + top = box.top + scrollTop - clientTop, + left = box.left + scrollLeft - clientLeft; + + return { + top: Math.round(top), + left: Math.round(left) + }; +}; +/** + * + */ +Element.prototype.scrollTo = function () { + "use strict"; + var coords = this.getPos(); + window.scrollTo(coords.left, coords.top); +}; NodeList.prototype.cycle = function (event, fn, action) { "use strict";