Guest User

Untitled

a guest
May 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. /**
  2. * Carousel object
  3. */
  4.  
  5. CP.Carousel = {
  6. liWidth: 0,
  7. ulWidth: 0,
  8. ulPos: 0,
  9. slideCount: 0,
  10. delta: 0,
  11. left: 0,
  12. right: 0,
  13. moving: null,
  14. settings: null,
  15.  
  16. /**
  17. * callback after sliding is complete. updates the ulPos variable.
  18. */
  19. updateUlPos: function() {
  20. console.log(this);
  21. console.log(this.settings.carouselId);
  22. this.ulPos = $(this.settings.carouselId+' ul').css('left');
  23. console.log('here: '+$(this.settings.carouselId+' ul').css('left')+', carouselId:'+this.settings.carouselId);
  24. this.ulPos = parseInt(this.ulPos.substring(0,this.ulPos.indexOf('px')),10);
  25. this.moving = null;
  26.  
  27. //change cursor from hand to pointer if at the end
  28. if(this.ulPos == 0) {
  29. $(this.settings.carouselId+' div.left').css('cursor','default');
  30. } else if (this.ulPos == (this.delta * this.left)) {
  31. $(this.settings.carouselId+' div.right').css('cursor','default');
  32. } else {
  33. $(this.settings.carouselId+' div.right,'+this.settings.carouselId+' div.left').css('cursor','pointer');
  34. }
  35.  
  36. console.log(this.settings.carouselId + ' end updateUlPos');
  37. },
  38.  
  39. /**
  40. * click method for left/right buttons
  41. */
  42. move: function(dir) {
  43. console.log('move called');
  44. console.log(this.ulPos);
  45. console.log(this.moving);
  46. if (this.moving !== null) {
  47. return;
  48. }
  49. var that = this;
  50.  
  51. if (dir == 'right') {
  52. if(this.ulPos != (this.delta * this.left)) {
  53. this.moving = $(this.settings.carouselId+' ul').animate({"left": this.left+this.ulPos+"px"},this.settings.speed,function(){that.updateUlPos.call(that)});
  54. }
  55. } else {
  56. if(this.ulPos != 0) {
  57. this.moving = $(this.settings.carouselId+' ul').animate({"left": this.right+this.ulPos+"px"},this.settings.speed,function(){that.updateUlPos.call(that)});
  58. }
  59. }
  60. console.log('move end');
  61. },
  62.  
  63. /**
  64. * Initialize sets up element widths and heights, inserts left/right buttons and assigns
  65. * click handlers for each
  66. *
  67. * Takes settings object with the following members:
  68. * carouselId: id of carousel div,
  69. * itemsInView: number of items viewable in carousel
  70. * speed: scrolling speed of carousel
  71. */
  72. initialize: function(settings) {
  73. if(typeof(settings) == 'undefined') return false;
  74. this.settings = settings;
  75. this.slideCount = $(this.settings.carouselId+' ul li').length;
  76. this.liWidth = $(this.settings.carouselId+' ul li').outerWidth({margin:true});
  77. this.left = this.liWidth*-1;
  78. this.right = this.liWidth;
  79. this.ulWidth = this.slideCount * this.liWidth;
  80. this.delta = this.slideCount - this.settings.itemsInView;
  81. $(this.settings.carouselId+' ul').width(this.ulWidth);
  82.  
  83. //insert left/right buttons
  84. $(this.settings.carouselId+' div.view').before('<div class="left"></div>').after('<div class="right"></div>');
  85. var that = this;
  86.  
  87. //add click handler for left right buttons
  88. $(this.settings.carouselId+' div.right,'+this.settings.carouselId+' div.left').click(function(e) {
  89. that.move($(this)[0].className);
  90. });
  91. }
  92. }
Add Comment
Please, Sign In to add comment