Wednesday 16 July 2014

Auto flipping text with css3 and javascript

We know since Html5 and Css3 arrived we can do lots of fun effect in our webpage. In css3 we have different animations and transition effects that's gives us an easiest ways to do webpage more interactive.

There is a property in css3 that is called as transform. There are two kind of transform 2d transform and 3d transform. In this post I am going to use 2d transform for Flip effect.

In this flip effect I want it to flip text panel automatically, which will have two different content on both sides front and back.

First of all we are going to build Html structure for it.



   <div id="flipPanel">
                              <h3 id="header1" class="face">Front text </h3>
                         
                              <h3 id="header2" class="face back" style="display:none;">Back text</h3>
                        </div>

I am using h3 for my text. You may try this with div also and do deterrent design.

We are making two sides of the panel here. So here am  using one main div which content two h3. Let's add css.


#flipPanel {
   -moz-transform-style: preserve-3d;
   -moz-transition: all 1s linear 0s;
    transform-style: preserve-3d;
    transition: all 1s linear 0s;
    -webkit-transform-style: preserve-3d;
   -webkit-transition: all 1s linear 0s;
   
    float: left;
   height: 18px;
    margin-top: 31px;
   width: 100%;
   position:relative;
   
}

.face {
   -moz-backface-visibility: hidden;
   -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    margin:0;
    position:absolute;
  
}
.face.back {
   -moz-box-sizing: border-box;
   -moz-transform: rotateX(180deg);
   -webkit-box-sizing: border-box;
   -webkit-transform: rotateX(180deg);
   box-sizing: border-box;
   transform: rotateX(180deg);
   
}


This will build your basic structure. You can see only one text instead of both. Because we have flipped panel already and back side is hidden. But this will not play automatically.

For auto play we have to use Jquery part for it.

 <script src="../js/jquery-1.7.1.min.js" type="text/javascript"></script>

  <script type="text/javascript">
 Triggerh1 = 0;
          rotatAmnt = 180;
          var myVar = setInterval(function () { showH1() }, 5000);
          function showH1() {
              if (Triggerh1 == 0) {
                  Triggerh1 = 1;
                  $('#flipPanel h3').slideToggle(1000);
                  $('#flipPanel').attr('style', '-moz-transform: rotatex(' + rotatAmnt + 'deg);-webkit-transform: rotatex(' + rotatAmnt + 'deg)');
                  rotatAmnt = rotatAmnt + 180;
              } else {
                  Triggerh1 = 0;
                  $('#flipPanel h3').slideToggle(1000);
                  $('#flipPanel').attr('style', '-moz-transform: rotatex(' + rotatAmnt + 'deg);-webkit-transform: rotatex(' + rotatAmnt + 'deg)');
                  rotatAmnt = rotatAmnt + 180;

              };
          }

          </script>

You are done. Now run this page in browser.

 


     



Thursday 10 July 2014

Star Rating with simple css and less javascript

I was searching for Star Rating for one of my client. But when I did search for it, I found many plugins with heavy js files. So I tried to build my own plugin with less javascript and more css used in it. And more compatible with asp or other coding languages.

So first of all get star image which is transparent through middle, so we can see background through star shape. like this:


We will use this image in our css.



Then add below html structure in your html page. I am using radio inputrs here. you can use asp radio buttons also as per your requirement.


<div class="StarBtns" title="Rate this Recipe">
<div class="defltRate" id="defltRate2" style="width:0%">

</div>

<div class="Starcon" title="Rate this Recipe">

<div class="Rateradiodiv">

<span class="grRate"><input id="ctl00_ContentPlaceHolder1_rdoRate1" type="radio" name="ctl00$ContentPlaceHolder1$grRate" value="rdoRate1" onclick="getRateshow(1);" /></span>

</div>

<div class="colordiv" style="width: 20%">

</div>

</div>

<div class="Starcon" title="Rate this Recipe">

<div class="Rateradiodiv">

<span class="grRate"><input id="ctl00_ContentPlaceHolder1_rdoRate2" type="radio" name="ctl00$ContentPlaceHolder1$grRate" value="rdoRate2" onclick="getRateshow(2);" /></span>

</div>

<div class="colordiv" style="width: 40%">

</div>

</div>

<div class="Starcon" title="Rate this Recipe">

<div class="Rateradiodiv">

<span class="grRate"><input id="ctl00_ContentPlaceHolder1_rdoRate3" type="radio" name="ctl00$ContentPlaceHolder1$grRate" value="rdoRate3" onclick="getRateshow(3);" /></span>

</div>

<div class="colordiv" style="width: 60%">

</div>

</div>

<div class="Starcon" title="Rate this Recipe">

<div class="Rateradiodiv">

<span class="grRate"><input id="ctl00_ContentPlaceHolder1_rdoRate4" type="radio" name="ctl00$ContentPlaceHolder1$grRate" value="rdoRate4" onclick="getRateshow(4);" /></span>

</div>

<div class="colordiv" style="width: 80%">

</div>

</div>

<div class="Starcon" title="Rate this Recipe">

<div class="Rateradiodiv">

<span class="grRate"><input id="ctl00_ContentPlaceHolder1_rdoRate5" type="radio" name="ctl00$ContentPlaceHolder1$grRate" value="rdoRate5" onclick="getRateshow(5);" /></span>

</div>

<div class="colordiv" style="width: 100%">

</div>

</div>

</div>




Now copy and add this css in your page.
/*****************rating css*******************/


.StarBtns
       {
           /*background-color: #333333;*/
           float: left;
           padding: 0px;
           width: 115px;
           position: relative;
           padding: 0;
       }
       
       
       .Ratebtn1, .Ratebtn2, .Ratebtn3, .Ratebtn4, .Ratebtn5
       {
           position: relative;
           z-index: 1000;
       }
       
       .Starcon
       {
           float: left;
           padding: 0px;
       }
       .defltRate
       {
           background-color: #DAA705;
           float: left;
           height: 20px;
           position: absolute;
           top: 0px;
           width: 89%;
           z-index: 500;
           left: 0px;
           padding: 0;
       }
       
       
       .colordiv
       {
           background-color: #B70606;
           float: left;
           height: 20px;
           position: absolute;
           top: 0px;
           width: 89%;
           z-index: 500;
           display: none;
           left: 0px;
           padding: 0;
       }
       
       .Starcon:hover .colordiv
       {
           /*display: block;*/
       }
       
       
       .Rateradiodiv
{
   background-image: url('../../images/stars.png');
   height: 20px;
   position: relative;
   z-index: 1000;
   width: 23px;
   background-repeat: no-repeat;
   padding: 0;
}
.Rateradiodiv  input {
   height: 100%;
   opacity: 0;
   width: 100%;
}
/*************End of css***************/



You can now check this in browser and you will find that, on hover star rating is increasing. It will give you feel like star rating that you see on other websites and it is without any javascript.



Selecting star:

In webpage we have to select the rating. This can not be possible without javascript.
So we are going to add small javascript code in our page.

Copy below script and add it in your page. 


<script type="text/javascript">

            var f = 3;



            var radiobtn1 = document.getElementById('ctl00_ContentPlaceHolder1_rdoRate1');

            var radiobtn2 = document.getElementById('ctl00_ContentPlaceHolder1_rdoRate2');

            var radiobtn3 = document.getElementById('ctl00_ContentPlaceHolder1_rdoRate3');

            var radiobtn4 = document.getElementById('ctl00_ContentPlaceHolder1_rdoRate4');

            var radiobtn5 = document.getElementById('ctl00_ContentPlaceHolder1_rdoRate5');



            if (radiobtn1.checked) {



                document.getElementById("defltRate2").style.width = "20%";


            }



            if (radiobtn2.checked) {



                document.getElementById("defltRate2").style.width = "40%";



            }

            if (radiobtn3.checked) {



                document.getElementById("defltRate2").style.width = "60%";


            }

            if (radiobtn4.checked) {



                document.getElementById("defltRate2").style.width = "80%";



            }

            if (radiobtn5.checked) {



                document.getElementById("defltRate2").style.width = "100%";



            }


        function getRateshow(g) {
            document.getElementById("defltRate2").style.width = g * 20 + "%";

        }

    </script>


Thats it, We don't need any javascript library for this.Make sure you'r id's are same as in javascript.

Do commenting on this post if you find it useful or not.