0

We have a generic slider functionality implemented in our product. We need to read the item index value from URL and scroll the slider to show the active item.

Following code logic is used to show the active thumbnail and for that i need to animate the DIV to negative left.

First, we get the total thumbnail items and then index of active item from URL Hash Value (i.e. #slide=7). One set contains minimum of 5 items. Need to multiply the slider width to the page set value where ActiveItemIndex lies.

Javascript Code -

showActiveThumbnailOnPageLoad : function() {
    var _this = this,
        totalThumbnails = $('.slidetabs a').length,
        activeItem = window.location.href.split('=')[1],
        scrollAmount;

    if(activeItem > 5 && activeItem <= 10) {
        scrollAmount = '-=' + 772
    } else if(activeItem > 10 && activeItem <= 15) {
        scrollAmount = '-=' + 772 * 2
    } else if(activeItem > 15 && activeItem <= 20) {
        scrollAmount = '-=' + 772 * 3
    }

    $('.slidetabs').stop().animate({
        left : scrollAmount
    });
}

As of now, hard code condition is used which support upto 20 items. Any help to make this code to support n number of items. I mean to say a generic code without hard code values.

Thanks in advance.

Lokesh Yadav
  • 1,574
  • 5
  • 23
  • 50

3 Answers3

2

You can try :

scrollAmount = '-=' + 772 * ~~((activeItem - 1) / 5);
Samuel Caillerie
  • 8,259
  • 1
  • 27
  • 33
1

Use this:

scrollAmount = '-=' + 772 * Math.floor((((activeItem > 0) ? activeItem : 1)  - 1) / 5);
Mohit Pandey
  • 3,679
  • 7
  • 26
  • 38
  • It does not work if `activeItem === 0` (`Math.floor(-0.2)` gives `-1`). – Samuel Caillerie Apr 24 '13 at 10:35
  • yes, you are right, but i think, there must be atleast 5(+ve no.) activeItem in page according to his first if condtition. But i modify it as per your condition. – Mohit Pandey Apr 24 '13 at 10:38
  • since there is this condition, there is the possibility that `activeItem` is less than 5 and then possibly equals to 0... But if you want to avoid this, you can readd OP's first condition or else use double bit complement... – Samuel Caillerie Apr 24 '13 at 10:42
  • you are right mohit, active item will never be 0. Thanks for the help Sir. – Lokesh Yadav Apr 24 '13 at 10:47
0

Try this:

scrollAmount = '-=' + 772 * ((activeItem  - 1) / 5);

EDIT (Based on this comment and this answer)

And to round the division:

scrollAmount = '-=' + 772 * (((activeItem  - 1) / 5) >> 0);

Or:

scrollAmount = '-=' + 772 * (~~((activeItem - 1) / 5));
Community
  • 1
  • 1
Maria Ioannidou
  • 1,544
  • 1
  • 15
  • 36
  • 2
    Be careful : you have to round your division! – Samuel Caillerie Apr 24 '13 at 10:28
  • @SamuelCaillerie: You are right. I thought that activeItem was regarded as integer and division result would be an integer too. So based on this answer too: http://stackoverflow.com/a/4228528/504368 I should use one of the two options: var num = ~~(a / b); var num = (a / b) >> 0; I will update my answer appropriately. – Maria Ioannidou Apr 24 '13 at 10:41