Browse files

Refactored the code

Refactored the code a lot to remove useless functions

Signed-off-by: Sam Hough <stone.hough@gmail.com>
  • Loading branch information...
1 parent 817d5d1 commit 061337363a3a6faf7e31432c96a43fd3e0549193 @samuelhough committed Nov 13, 2011
Showing with 110 additions and 229 deletions.
  1. +110 −229 jquery.skrewler.js
View
339 jquery.skrewler.js
@@ -1,55 +1,23 @@
/*
-
- Skrewler - A Jquery plugin for scrolling floated, unordered list elements vertically and horizontally
- Alpha 1.0
- Created by Sam Hough with code inspiration from Chris Abrams
- Open Source - MIT Licesnse
- October 2011
+@name: Skrewler
+@plugin: jQuery
+@description: A jQuery plugin for scrolling list elements horizontally and vertically
+@author: Sam Hough
+@version: Alpha 1.1
+@updated: November 10th, 2011
+@contributors:
+Chris Abrams
- To use - call skrewler on your unorganized list, say whether it should scroll vertically or horizontall, and give the ids for your left/right or up/down
- buttons
- EX: $('#scroller').skrewler({ direction: "horizontal", leftUpButtonID: '#left', rightDownButtonID: '#right'});
+@license:
+Copyright (c) 2011 Samuel Hough
- Skrewler Options:
- REQUIRED:
- direction: "horizontal" || "vertical" - Direction skrewler will scroll your UL element
-
- REQUIRED (if enableKeys === false):
- leftUpButtonID: "#id" - Selector ID for the left/Up button on your page
- rightDownButtonID: "#id" - Selector ID for the right/Down button on your page
-
- Optional:
- duration: (int) - How long the animations last
- reverse: (boolean) - Reverse the direction of the button / keyboard presses
- enableKeys: (boolean) - Enable keyboard control
- keyCode_left: (int) - Specify a different key code value to cause the list to move left
- keyCode_right: (int) - Specify a different key code value to cause the list to move right
- keyCode_up: (int) - Specify a different key code value to cause the list to move up
- keyCode_down: (int) - Specify a different key code value to cause the list to move down
- postAnimFunc: function(){} - Pass a function to be performed at the end of every scroll animation
- leaveBoundsFunc: function(){}- Pass a function to be performed when the user tries to go outside a boundary (left or right/up down side)
- scrollByPageSize: (boolean) - Instead of scrolling by the average li elements width, scroll by the page width or height
-
-// ----------------------------------------------------------------------------//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files ( the "Software" ), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
-// ----------------------------------------------------------------------------//
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
*/
(function($) {
@@ -59,15 +27,15 @@
var error = {
e: "Skrewler ERROR: ",
noDirection: function(){
- console.log(this.e + "No direction was provided to the handler - $('#yourElement').skrewler({ direction: 'vertical' (or) 'horizontal' ");
+ console.error(this.e + "No direction was provided to the handler - $('#yourElement').skrewler({ direction: 'vertical' (or) 'horizontal' ");
},
leftUp: function(){
- console.log(this.e + "No id for leftUpButtonID && enableKeys === false FIX: $('#yourElement').skrewler({ leftUpButtonID: '#id'");
+ console.error(this.e + "No id for leftUpButtonID && enableKeys === false FIX: $('#yourElement').skrewler({ leftUpButtonID: '#id'");
},
rightDown: function(){
- console.log(this.e + "No id for rightDownButtonID && enableKeys === false FIX: $('#yourElement').skrewler({ rightDownButton: '#id'");
+ console.error(this.e + "No id for rightDownButtonID && enableKeys === false FIX: $('#yourElement').skrewler({ rightDownButton: '#id'");
}
};
@@ -107,8 +75,24 @@
element: {}, // Element that is being manipulated
position: 0, // Keeps track of the list's position and is multiplied by the scrollLength to set the xPos/yPos
- xPos: 0, // marginLeft size in horizontal scrolling
- yPos: 0, // marginTop size in vertical scroling
+ xORy: {
+ xPos: 0, // marginLeft size in horizontal scrolling
+ yPos: 0, // marginTop size in vertical scroling
+ setPos: function(direction, newPos){
+ if(direction === 'left' || direction == 'right'){
+ xPos = newPos;
+ } else {
+ yPos = newPos;
+ }
+ },
+ retPos: function(direction){
+ if(direction === 'left' || direction == 'right'){
+ return xPos;
+ } else {
+ return yPos;
+ }
+ }
+ },
atBoundary: false, // If (atBoundary) and the user tries to go past it, the defaults.leaveBoundsFunc() is called
init: function(obj){
this.wHeight = $(window).height();
@@ -161,24 +145,7 @@
break;
}
}
-
- switch(direction){ // Send the scroll call to the appropriate function
- case "left":
- this.scrollLeft();
- break;
- case "right":
- this.scrollRight();
- break;
- case "up":
- this.scrollUp();
- break;
- case "down":
- this.scrollDown();
- break;
- default:
- console.log(error.e + ' No valid direction was passed to skrewler.scroll(direction)');
- break;
- }
+ this.scrollDir(direction);
},
remPX: function(string){
string = ''+string+'';
@@ -188,193 +155,107 @@
return parseInt(string,10);
}
},
- scrollLeft: function(){ // Scroll the element left
- console.log('entering scroll left');
- var totalWidth = 0;
- $(this.element).find("li").each(function(){ // Total size of all elements combined
- var tmp = 0;
- if(defaults.include.border){
-
- tmp += skrewler.remPX($(this).css('borderLeftWidth'));
- tmp += skrewler.remPX($(this).css('borderRightWidth'));
- }
- if(defaults.include.margin){
- tmp += skrewler.remPX($(this).css('margin-left'));
- tmp += skrewler.remPX($(this).css('margin-right'));
- }
- if(defaults.include.padding){
- tmp += skrewler.remPX($(this).css('padding-left'));
- tmp += skrewler.remPX($(this).css('padding-right'));
- }
- console.log(tmp);
- totalWidth += tmp;
- totalWidth += skrewler.remPX($(this).width());
+ scrollDir: function(direction){
+ console.log(direction);
+ var borderOne = '',
+ borderTwo = '',
+ marginPadOne = '',
+ marginPadTwo = '',
+ totalSize = 0,
+ wSize = 0,
+ obj = {};
- });
- console.log('out of each');
-
- var elements = $(this.element).find("li").length, // Get total # of elements in list
- scrollLength = parseInt(totalWidth / elements, 10); // Determine the length each scroll click should be
-
- if(defaults.scrollByPageSize){
- scrollLength = this.wWidth;
- }
-
- if(((this.position - 1) * scrollLength) >= (-totalWidth + this.wWidth)){
- this.position -= 1;
- this.xPos = this.position * scrollLength;
+ if(direction == 'left' || direction == 'right'){
+ borderOne = 'Left';
+ borderTwo = 'Right';
+ marginPadOne = 'left';
+ marginPadTwo = 'right';
+ wSize = this.wWidth;
} else {
- if(this.atBoundary){
- defaults.leaveBoundsFunc();
- }
- this.xPos = (-totalWidth + this.wWidth);
- this.atBoundary = true;
+ borderOne = 'Top';
+ borderTwo = 'Bottom';
+ marginPadOne = 'top';
+ marginPadTwo = 'bottom';
+ wSize = this.wHeight;
}
- console.log('scrollLength: ' + scrollLength + 'totalWidth: '+totalWidth + ' atBoundary: ' + this.atBoundary + ' position: '+this.position+ ' xPos ' + this.xPos + " wWidth " + this.wWidth);
-
- $(this.element).stop().animate({
- marginLeft: this.xPos
- }, defaults.duration, function(){
- defaults.postAnimFunc();
- });
- },
- scrollRight: function(){ // Scroll the element right
-
- var totalWidth = 0;
$(this.element).find("li").each(function(){ // Total size of all elements combined
var tmp = 0;
if(defaults.include.border){
- tmp += skrewler.remPX($(this).css('borderLeftWidth'));
- tmp += skrewler.remPX($(this).css('borderRightWidth'));
+ tmp += skrewler.remPX($(this).css('border'+borderOne+'Width'));
+ tmp += skrewler.remPX($(this).css('border'+borderTwo+'Width'));
}
if(defaults.include.margin){
- tmp += skrewler.remPX($(this).css('margin-left'));
- tmp += skrewler.remPX($(this).css('margin-right'));
+ tmp += skrewler.remPX($(this).css('margin-'+marginPadOne));
+ tmp += skrewler.remPX($(this).css('margin-'+marginPadTwo));
}
if(defaults.include.padding){
- tmp += skrewler.remPX($(this).css('padding-left'));
- tmp += skrewler.remPX($(this).css('padding-right'));
+ tmp += skrewler.remPX($(this).css('padding-'+marginPadOne));
+ tmp += skrewler.remPX($(this).css('padding-'+marginPadTwo));
}
- totalWidth += tmp;
- totalWidth += skrewler.remPX($(this).width());
+ console.log(tmp);
+ totalSize += tmp;
- });
-
- var elements = $(this.element).find("li").length, // Get total # of elements in list
- scrollLength = parseInt(totalWidth / elements, 10); // Determine the length each scroll click should be
-
- if(defaults.scrollByPageSize){
- scrollLength = this.wWidth;
- }
-
- if(((this.position + 1) * scrollLength) <= 0){
- this.atBoundary = false;
- this.position += 1;
- this.xPos = this.position * scrollLength;
- } else {
- defaults.leaveBoundsFunc();
- this.xPos = 0;
- }
-
- $(this.element).stop().animate({
- marginLeft: this.xPos
- }, defaults.duration, function(){
- defaults.postAnimFunc();
- });
- console.log('scrollLength: ' + scrollLength + 'totalWidth: '+totalWidth + ' atBoundary: ' + this.atBoundary + ' position: '+this.position+ ' xPos ' + this.xPos + " wWidth " + this.wWidth);
-
- },
- scrollUp: function(){ // Scroll the element up
-
- var totalHeight = 0;
- $(this.element).find("li").each(function(){ // Total size of all elements combined
- var tmp = 0;
- if(defaults.include.border){
-
- tmp += skrewler.remPX($(this).css('borderTopWidth'));
- tmp += skrewler.remPX($(this).css('borderBottomWidth'));
+ if(direction == 'left' || 'right'){
+ totalSize += skrewler.remPX($(this).width());
+ } else {
+ totalSize += skrewler.remPX($(this).height());
}
- if(defaults.include.margin){
- tmp += skrewler.remPX($(this).css('margin-top'));
- tmp += skrewler.remPX($(this).css('margin-bottom'));
- }
- if(defaults.include.padding){
- tmp += skrewler.remPX($(this).css('padding-top'));
- tmp += skrewler.remPX($(this).css('padding-bottom'));
- }
- totalHeight += skrewler.remPX($(this).height());
});
+ console.log('out of each');
- var elements = $(this.element).find("li").length, // Get total # of elements inlistStylet
- scrollLength = parseInt(totalHeight / elements, 10); // Determine the length each scroll click should be
+ var elements = $(this.element).find("li").length, // Get total # of elements in list
+ scrollLength = parseInt(totalSize / elements, 10); // Determine the length each scroll click should be
if(defaults.scrollByPageSize){
- scrollLength = this.wHeight;
- }
-
- if(((this.position + 1) * scrollLength) <= 0){
- this.atBoundary = false;
- this.position += 1;
- this.yPos = this.position * scrollLength;
- } else {
- defaults.leaveBoundsFunc();
- this.yPos = 0;
- }
-
- $(this.element).stop().animate({
- marginTop: this.yPos
- }, defaults.duration, function(){
- defaults.postAnimFunc();
- });
- },
- scrollDown: function(){ // Scroll the element down
- var totalHeight = 0;
- $(this.element).find("li").each(function(){ // Total size of all elements combined
- var tmp = 0;
- if(defaults.include.border){
-
- tmp += skrewler.remPX($(this).css('borderTopWidth'));
- tmp += skrewler.remPX($(this).css('borderBottomWidth'));
+ if(direction == 'left' || 'right'){
+ scrollLength = this.wWidth;
+ } else {
+ scrollLength = this.wHeight;
}
- if(defaults.include.margin){
- tmp += skrewler.remPX($(this).css('margin-top'));
- tmp += skrewler.remPX($(this).css('margin-bottom'));
+ }
+
+ if(direction == 'left' || direction == 'down'){
+ console.log('left or up' + direction);
+ if(((this.position - 1) * scrollLength) >= (-totalSize + wSize)){
+ this.position -= 1;
+ this.xORy.setPos(direction, (this.position * scrollLength));
+ } else {
+ if(this.atBoundary){
+ defaults.leaveBoundsFunc();
+ }
+ this.xORy.setPos(direction, (-totalSize + wSize));
+ this.atBoundary = true;
}
- if(defaults.include.padding){
- tmp += skrewler.remPX($(this).css('padding-top'));
- tmp += skrewler.remPX($(this).css('padding-bottom'));
+ } else {
+ console.log('right or up');
+ if(((this.position + 1) * scrollLength) <= 0){
+ this.atBoundary = false;
+ this.position += 1;
+ this.xORy.setPos(direction, (this.position * scrollLength));
+ } else {
+ defaults.leaveBoundsFunc();
+ this.xORy.setPos(direction, (0));
}
- totalHeight += tmp;
- totalHeight += skrewler.remPX($(this).height());
-
- });
-
- var elements = $(this.element).find("li").length, // Get total # of elements in list
- scrollLength = parseInt(totalHeight / elements, 10); // Determine the length each scroll click should be
-
- if(defaults.scrollByPageSize){
- scrollLength = this.wHeight;
}
+ // console.log('scrollLength: ' + scrollLength + 'totalWidth: '+totalSize + ' atBoundary: ' + this.atBoundary + ' position: '+this.position+ ' xPos ' + skrewler.xORy.retPos(direction) + " wWidth " + wSize);
- if(((this.position - 1) * scrollLength) >= (-totalHeight + this.wHeight)){
- this.position -= 1;
- this.yPos = this.position * scrollLength;
+ if(direction == 'left' || direction == 'right'){
+ obj = {
+ marginLeft: skrewler.xORy.retPos(direction)
+ };
} else {
- if(this.atBoundary){
- defaults.leaveBoundsFunc();
- }
- this.yPos = (-totalHeight + this.wHeight);
- this.atBoundary = true;
+ obj = {
+ marginTop: skrewler.xORy.retPos(direction)
+ };
}
- $(this.element).stop().animate({
- marginTop: this.yPos
- }, defaults.duration, function(){
+ $(this.element).stop().animate(obj, defaults.duration, function(){
defaults.postAnimFunc();
- });
+ });
+
}
};
skrewler.init($(this));

0 comments on commit 0613373

Please sign in to comment.