Grow your CSS skills. Land your dream job.

Javascript question

  • # November 16, 2012 at 9:55 am

    Hello Everybody,
    I’m a web designer from chicago.
    I’m working on some javascript code. It’s jquery for a custom photo rotater that I created. It works right now but I’m looking to optimize it.

    What do you guys think? Anything I can do to clean it up?

    // JavaScript Document

    //global parameters for controlling the sliding.

    function slider(numpics, imgwidth, mydiv){
    this.mypos = mydiv;
    this.newpos = 0;
    this.imgwidth = 0;
    this.myarray = new Array;
    this.mycounter = 0;
    this.total = 0;
    this.curpos = 0;
    this.times;

    this.intialize = intialize;
    this.moveNext = moveNext;
    this.movePrev = movePrev;
    }

    /*this function intializes the postions for sliding. It has to be called on the page to make the sliding work.
    numpics = number of divisons to slide through,
    imgwidth = the width of the container,
    mydiv = name of div that slides
    */
    function intialize(numpics, imgwidth, mydiv){
    while(this.mycounter < numpics){
    this.myarray[this.mycounter] = (imgwidth*this.mycounter)*-1;
    this.mycounter ++;
    }
    this.total = numpics – 1;
    this.mypos = mydiv;

    }

    /*this function controls forward sliding*/
    function moveNext(){
    if(this.newpos < this.total)
    {this.newpos += 1;}
    else
    {this.newpos =0; }
    this.curpos = this.myarray[this.newpos];
    $(this.mypos).stop().animate({
    left:this.curpos
    }, 800, function (){});

    }

    /*this function controls backward sliding*/
    function movePrev(){
    if(this.newpos > 0)
    {this.newpos -= 1;}
    else
    {this.newpos = this.total; }
    this.curpos = this.myarray[this.newpos];
    $(this.mypos).stop().animate({
    left:this.curpos
    }, 800, function (){});
    }

    # November 16, 2012 at 6:19 pm

    @inkblot,
    Only thing i would add, have you ran timing tests for your functions?

    this.myarray = [];
    # November 19, 2012 at 10:43 am

    @JohnMotylJr no I had not done that.
    I’ll give it a shot.

    Thanks for the suggestion.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".