diff --git a/README.markdown b/README.markdown index 945a782..c41aca7 100644 --- a/README.markdown +++ b/README.markdown @@ -3,20 +3,20 @@ Javascript and jQuery based component for making swipable containers for touch devides and with mouse usage. # Examples - + Simple mouse/finger swipable textbox: [http://pulges.github.com/Content-Swipe/](http://pulges.github.com/Content-Swipe/) #Usage - + The css variant with inline-blocks and no predefined width works only with no spaces between boxes in html. It can be used also with float left boxes but then #swipable must have a width that fits all boxes. - + - +
... @@ -28,55 +28,46 @@ It can be used also with float left boxes but then #swipable must have a width t
- + #Configuration - - $('#swipeable').scroller({ - box_element: '.swipable-box', // classname of box elements - move_treshold: 0.15, // fraction of width needed to drag to trigger scroll to another page - tap_treshold: 0.05, // if less than this fraction of width moved, tap event is triggered on scroll box + + new Scroller('swipeable', { + box_element: '.swipable-box', // css selector of box elements + move_threshold: 0.15, // fraction of width needed to drag to trigger scroll to another page + tap_threshold: 0.05, // if less than this fraction of width moved, tap event is triggered on scroll box fixed_stops: true // if false autoscroll to closest slide is disabled }); #Events If movement of less than tap treshold occurs. "tap" event is triggered on ".swipable-box" - - $('.swipable-box').on('tap', function() { alert('tap'); }); - + + document.querySelector('.swipable-box').addEventListener('tap', function() { alert('tap'); }); + After scroll animation finishes "scrollstop" event is triggered on "#swipeable" with index of scroll page position as second parameter - $('#swipeable').on("scrollstop", function(event, index) { - alert("Scrolled to index: " + index); + document.getElementById('swipeable').addEventListener('scrollstop', function(event) { + alert('Scrolled to index: ' + event.detail.currentIndex); }); - + #Actions Example of accessing inner functions - $('#swipeable').scroller(); - + var scroller = new Scroller(document.getElementById('swipeable')); + // to next slide - $('#next').click(function() { - $('#swipeable').scroller().next(); - }); - + scroller.next(); + // to prev slide - $('#prev').click(function() { - $('#swipeable').scroller().previous(); - }); + scroller.previous(); // slide to third slide (index 2 as list is 0 based) - $('#to3').click(function() { - $('#swipeable').scroller().move_to(2); - }); + scroller.move_to(2); // switch to third slide without animation - $('#to3_i').click(function() { - $('#swipeable').scroller().move_to(2, true); - }); - + scroller.move_to(2, true); + NB! Will update the gallery example soon. Current one will not work with new library. - \ No newline at end of file diff --git a/example.html b/example.html index 2d28b06..6aee2a3 100644 --- a/example.html +++ b/example.html @@ -46,32 +46,31 @@

Third Box

- - - - - + + + + diff --git a/jquery.scroller.js b/jquery.scroller.js deleted file mode 100644 index 05e0a81..0000000 --- a/jquery.scroller.js +++ /dev/null @@ -1,148 +0,0 @@ -(function($) { - - function getsupportedprop(p){ - var root=document.documentElement; - for (var i=0; i moveTreshold && i + 1 < (this.$boxes.length)) { - i++; - } else if (((-1) * moveX) > moveTreshold && i - 1 >= 0){ - i--; - } - this.move_to(i); - }, - - move_to: function(index, fast) { - var newloc = this.boxW * index; - this.idx = index; - this.x = -1 * newloc; - if (cssTransform && cssTransitionD) { - this.$el.get(0).style[cssTransitionD] = (fast) ? "0s" : "0.5s"; - this.$el.get(0).style[cssTransform] = "translate3d(" + this.x + "px,0px,0px)"; - } else { - this.$el.stop(); - (fast) ? this.$el.css({'left': this.x}) : this.$el.animate({'left': this.x}, 800); - } - if (!fast) { - setTimeout($.proxy(function () { - this.$el.trigger('scrollstop', [index]); - }, this), 810); - } - }, - - next: function() { - if(this.idx + 1 < this.$boxes.length){ - this.move_to(this.idx + 1); - } - }, - - previous: function() { - if (this.idx - 1 >= 0) { - this.move_to(this.idx - 1); - } - } - }; - - $.fn.scroller = function (options) { - if ($(this).data('plugin_scroller')) { - return $(this).data('plugin_scroller'); - } else { - return this.each(function () { - if (!$.data(this, 'plugin_scroller')) { - $(this).data('plugin_scroller', new Scroller($(this), options)); - } - }); - } - }; - -})(jQuery); \ No newline at end of file diff --git a/jquery.scroller.min.js b/jquery.scroller.min.js deleted file mode 100644 index c729971..0000000 --- a/jquery.scroller.min.js +++ /dev/null @@ -1 +0,0 @@ -(function(a){function b(a){var b=document.documentElement;for(var c=0;cc&&b+1c&&b-1>=0&&b--,this.move_to(b)},move_to:function(b,c){var f=this.boxW*b;this.idx=b,this.x=-1*f,d&&e?(this.$el.get(0).style[e]=c?"0s":"0.5s",this.$el.get(0).style[d]="translate3d("+this.x+"px,0px,0px)"):(this.$el.stop(),c?this.$el.css({left:this.x}):this.$el.animate({left:this.x},800)),c||setTimeout(a.proxy(function(){this.$el.trigger("scrollstop",[b])},this),810)},next:function(){this.idx+1=0&&this.move_to(this.idx-1)}},a.fn.scroller=function(b){return a(this).data("plugin_scroller")?a(this).data("plugin_scroller"):this.each(function(){a.data(this,"plugin_scroller")||a(this).data("plugin_scroller",new g(a(this),b))})}})(jQuery) \ No newline at end of file diff --git a/scroller.js b/scroller.js new file mode 100644 index 0000000..6632273 --- /dev/null +++ b/scroller.js @@ -0,0 +1,194 @@ +(function(window) { + + function getsupportedprop(p){ + var root = document.documentElement; + + for (var i=0; i moveThreshold && i + 1 < (this.boxes.length)) { + i++; + } else if (((-1) * moveX) > moveThreshold && i - 1 >= 0){ + i--; + } + + this.move_to(i); + }, + + move_to: function(index, fast) { + var newloc = this.boxW * index; + + this.idx = index; + this.x = -1 * newloc; + + if (cssTransitionD) { + this.el.style[cssTransitionD] = (fast) ? "0s" : "0.5s"; + } + + if (cssTransform) { + this.el.style[cssTransform] = "translate3d(" + this.x + "px,0px,0px)"; + } else { + this.el.style.left = this.x + 'px'; + } + + var callback = function() { + this.triggerEvent(this.el, 'scrollstop', {currentIndex: index}); + } + + if (fast) { + callback.apply(this); + } else { + this.scrollStopCallbackTimeout = setTimeout(callback.bind(this), 500); + } + }, + + next: function() { + if(this.idx + 1 < this.boxes.length){ + this.move_to(this.idx + 1); + } + }, + + previous: function() { + if (this.idx - 1 >= 0) { + this.move_to(this.idx - 1); + } + }, + + triggerEvent: function(element, type, data) { + var event, + eventData; + + if (data) { + eventData = data + } else { + eventData = {}; + } + + if (window.CustomEvent) { + event = new CustomEvent(type, {detail: eventData}); + } else { + event = document.createEvent('CustomEvent'); + event.initCustomEvent(type, true, true, eventData); + } + + element.dispatchEvent(event); + } + }; + + window.Scroller = Scroller; + +})(window);