Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. // If JavaScript is disabled it will look more user friendly
  2. // and no matter how many images we add, the code will work properly
  3.  
  4. (function(){
  5. var container = $('div.slider').css('overflow', 'hidden').children('ul'),
  6. slider = new Slider(container, $('#slider-nav'));
  7.  
  8. slider.nav.show().find('button').on('click', function(){
  9. slider.setCoordinates($(this).data('dir'));
  10. slider.transition();
  11. });
  12.  
  13. setInterval(function() {
  14. slider.setCoordinates('next');
  15. slider.transition();
  16. }, 3000);
  17. })();
  18.  
  19. function Slider(container, nav) {
  20. this.container = container;
  21. this.nav = nav.show();
  22.  
  23. this.imgs = this.container.find('img');
  24. this.imgWidth = this.imgs[0].width;
  25. this.imgsLen = this.imgs.length;
  26.  
  27. this.current = 0;
  28. }
  29.  
  30. Slider.prototype.transition = function(coords) {
  31. this.container.animate({
  32. 'margin-left': coords || -(this.current * this.imgWidth)
  33. });
  34. };
  35.  
  36. Slider.prototype.setCoordinates = function(dir) {
  37. var pos = this.current;
  38.  
  39. pos += ( ~~(dir === 'next') || -1); // add or substract 1
  40. this.current = (pos < 0) ? this.imgsLen - 1 : pos % this.imgsLen;
  41.  
  42. return pos;
  43. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement