Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Carousel object
- */
- CP.Carousel = {
- liWidth: 0,
- ulWidth: 0,
- ulPos: 0,
- slideCount: 0,
- delta: 0,
- left: 0,
- right: 0,
- moving: null,
- settings: null,
- /**
- * callback after sliding is complete. updates the ulPos variable.
- */
- updateUlPos: function() {
- console.log(this);
- console.log(this.settings.carouselId);
- this.ulPos = $(this.settings.carouselId+' ul').css('left');
- console.log('here: '+$(this.settings.carouselId+' ul').css('left')+', carouselId:'+this.settings.carouselId);
- this.ulPos = parseInt(this.ulPos.substring(0,this.ulPos.indexOf('px')),10);
- this.moving = null;
- //change cursor from hand to pointer if at the end
- if(this.ulPos == 0) {
- $(this.settings.carouselId+' div.left').css('cursor','default');
- } else if (this.ulPos == (this.delta * this.left)) {
- $(this.settings.carouselId+' div.right').css('cursor','default');
- } else {
- $(this.settings.carouselId+' div.right,'+this.settings.carouselId+' div.left').css('cursor','pointer');
- }
- console.log(this.settings.carouselId + ' end updateUlPos');
- },
- /**
- * click method for left/right buttons
- */
- move: function(dir) {
- console.log('move called');
- console.log(this.ulPos);
- console.log(this.moving);
- if (this.moving !== null) {
- return;
- }
- var that = this;
- if (dir == 'right') {
- if(this.ulPos != (this.delta * this.left)) {
- this.moving = $(this.settings.carouselId+' ul').animate({"left": this.left+this.ulPos+"px"},this.settings.speed,function(){that.updateUlPos.call(that)});
- }
- } else {
- if(this.ulPos != 0) {
- this.moving = $(this.settings.carouselId+' ul').animate({"left": this.right+this.ulPos+"px"},this.settings.speed,function(){that.updateUlPos.call(that)});
- }
- }
- console.log('move end');
- },
- /**
- * Initialize sets up element widths and heights, inserts left/right buttons and assigns
- * click handlers for each
- *
- * Takes settings object with the following members:
- * carouselId: id of carousel div,
- * itemsInView: number of items viewable in carousel
- * speed: scrolling speed of carousel
- */
- initialize: function(settings) {
- if(typeof(settings) == 'undefined') return false;
- this.settings = settings;
- this.slideCount = $(this.settings.carouselId+' ul li').length;
- this.liWidth = $(this.settings.carouselId+' ul li').outerWidth({margin:true});
- this.left = this.liWidth*-1;
- this.right = this.liWidth;
- this.ulWidth = this.slideCount * this.liWidth;
- this.delta = this.slideCount - this.settings.itemsInView;
- $(this.settings.carouselId+' ul').width(this.ulWidth);
- //insert left/right buttons
- $(this.settings.carouselId+' div.view').before('<div class="left"></div>').after('<div class="right"></div>');
- var that = this;
- //add click handler for left right buttons
- $(this.settings.carouselId+' div.right,'+this.settings.carouselId+' div.left').click(function(e) {
- that.move($(this)[0].className);
- });
- }
- }
Add Comment
Please, Sign In to add comment