Create content rotator with knockout.js and CSS3

//Create content rotator with knockout.js and CSS3

Create content rotator with knockout.js and CSS3

There are number of ready made content rotators or content sliders on net. Here is another example how can you make one with knockout.js, few lines of JavaScript and some CSS.

Let’s say that we want to display list of discounted products on sidebar that rotates every few seconds, First, let’s make HTML with knockout template for our slides.

First, there is HTML script element inside which we are placing our slides content. Two elements are binded to view model: name and price. That script is repeated for each product in our view model, which is defined in div with class “rotator”. It is simple HTML, just for demonstration, with product name and price. Off course, you can add product picture, add to basket URL or anything else you want.

Now, add some CSS to decorate view:

Here is what our slide should look like:

product name

only 1€!

And finally, add JavaScript which handles our slide rotation.

In script we are using scripted data for products, but you can retrieve it with AJAX.

That’s all that we need to have our content rotator! Here what it looks like now:

Now, we can play around and add some effects. There are lot’s of things that you can do with CSS3, but not all browsers supports all the features. Here we are going to add flip effect when slide changes. To do that, we need to add only few lines of CSS. Inactive slides are initially rotated by 180° and hidden with backface-visibility tag, Also, slide needs to be at same position as previous one, so position is added. Active slide has 0° rotation.

Speed and effect for flip transaction is defined with tags:

And finally we need to add perspective tag in top container div.

Few more things like adding buttons for moving back and forward is also added to code, so final results is here:

Here is JsFiddle link for final version:

By |2017-05-03T12:33:49+00:00Rujan 29th, 2016|Categories: Coding|Tags: , |0 Comments

About the Author:

Leave A Comment