I wanted to create a simple static jQuery based slider. Right now I just have the design. It has small dots as pagination in the right bottom, with a heading, an image and some content. It should be able to loop infinitely and also should have the ability to play and pause. Not much features are required.
Share
Today I tried a content slider, with fixed pagination. I implemented this for something, which I would say after it is released. I could have simply used a plug-in, but due to some technical issues, and I too wanted to learn something, so I did it on my onw.
I started with the HTML Markup, starting with two
ULs. One holds the pagination and the other, content for the slides.Starting with this HTML Markup, I moved on to the CSS and positioned the layout elements.
HTML
I had somewhat a hard time fixing the issues caused by our ancient Internet Explorer 7 and slightly modern Internet Explorer 8 browsers, so I had to meddle with their proprietary form of hacking and fixed them! The final CSS came this way:
CSS
And yeah, the width and height of background images are fixed and they were
270px × 150px.Now comes the really interesting part, which took me about an hour! The JavaScript rocks in consuming time like anything! 🙂 I have to admit that I took a kind of low quality approach in my JavaScript, as I was concerned only about this 5 Content always with pagination and not scalable in future kind of a slider.
JavaScript
First of all, I had no idea how these sliders work, but I had a small idea that the
ULwill be having the width of sum of all theLIs, which it holds. So, that became the first statement:I need a counter to hold the current page been requested. So I initialized a counter
indexand assigning it a value0. Many of you people missed this part!!! Add this too!The next step is to create a function, which animates the whole slider. Yeah, I used jQuery to help me out! So, the function goes this way:
Since I declared
indexas a global variable, I can access it at any time from anywhere. Its scope is valid through the scripts and inside functions. I initialized a variable for the current page as the modulus value of the count ofindexdivided by the number of children inside slider.The next comes the awesome
animate()function, which jQuery provides. It is used to change the CSS Properties as given in a smooth transition instead of a drastic or sudden change. This function now moves (slides) the left position of the list containerULto fit the nextLIelement.Then comes the pagination stuff. It adds the current slide using another awesome CSS / jQuery Selector,
:nth-child(), which literally selects the nth child of the container. Once a slide is loaded, the correspondingLIwill be marked as active by adding a classactive.Finally the value of
indexis incremented by 1 using the traditional way!Coming to the next function, which is adding the
clickevent to the pagination buttons. I thought this would be the complicated way, but I managed to do in a simple way by just setting theindexvalue, clearing the old timer, and restarting the slider function.I added a
return false;at the end to prevent the link from following the URL of it, else it would open the URL from itshrefattribute. Now comes the final part and the important one. The initializer for the script. We just need to start the slider using the functionslideStart()and let it run for a fixed interval, say 2.5 seconds. Since it needs to be executed after the document is loaded, it is given inside the$(document).ready()function.Final JavaScript
Full Demo: http://jsbin.com/uhowak/2
Hope this helps someone! 🙂