Saturday, 21 July 2012

CSS3 REM unit for font size..

Remember em? Yeah!!..it has been renamed by CSS3 working group..believe that? You don't, actually, em and rem are siblings, rem being the most modern & advanced one.. I will introduce you with that but lets first start with their neighbour..px(pixel) unit.

px has been around ever since I started working and is most consistent and reliable. Most of the UI developers use it but if you are a usability concerned developer, you will not use it. Reason being, Internet explorer users can not use the browser default capabilities are increasing/decreasing the font size.



To over come the text-resizing issue in IE, em has been used for a long time. But the problem with the em based font-size is that the font-size is compound. See the example below. Using 62.5% for body so that I don't need to calculate the em value every time I need to use the unit.


body{font-size:62.5%}
p{font-size:1em} /*10px*/
h1{font-size:1.4em} */14px*/
li{font-size:1.4em} /*14px*/
li li{font-size:1.4em} /*20px*/


em is guided by the font-size of its parent and calculating it again and again can become frustrating at times.


rem has been introduced in CSS3 to overcome both the above mentioned limitations. It just behaves like em but the only difference is that rem values are based on the root element (html or body) and are not affected by the parent font-size value. So, if I use the above mentioned code for rem unit, it will be as follows..

body{font-size:62.5%}
p{font-size:1rem} /*10px*/
h1{font-size:1.4rem} */14px*/
li{font-size:1.4rem} /*14px*/
li li{font-size:1.4rem} /*14px*/

And the interesting thing is that it's supported by all modern browsers including IE9. Though Opera has not implemented rem unit yet. but you always have an option to use px as a fallback for the browsers not supporting rem unit.


p{font-size:14px; font-size:1rem} 

So the browser supporting rem will use rem otherwise px will be used for the browsers not supporting rem. 

Tuesday, 17 July 2012

jQuery CHAINING



The beauty of jquery method is that they return jquery object which can be used further to call another method. This is called COMMAND CHAINING where you can use multiple methods on a single set of elements. This is very helpful in many ways. See the example below.


Without Chaining
<script type="text/javascript">
    $(document.)ready(function(){
       $(“#div1").removeClass(“red");
       $(“#div1").addClass(“blue");
       $(“#div1").css(“border","1px solid #000")
    });
</script>

This script is traversing through the entire DOM and removing class from div ID div1. Then again it’s traversing through the DOM and adding a class blue and traversing through again to add a border. Now, this traversing could have been avoided using jquery chaining. It’s surely going to perform better, saving you from writing unnecessary code and keeping the code cleaner.

With Chaining
<script type="text/javascript">
    $(document.)ready(function(){
      $(“#div1").removeClass(“red")
       .addClass(“blue")
       .css(“border","1px solid #000")
     });
</script>




Thursday, 5 July 2012

Javascript : Displaying day, month and year

Copy this simple javascript code to get you current day, month and year.

See the working example here. http://jsfiddle.net/coolvin21/n7RNh/2/

 <script>
         window.onload = initDate;
           
            function initDate(){
                var dayName = new Array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
                var monName = new Array("January","February","March","April","May","June","July","August","September","October","Novermber","December");
                var now=  new Date();
               
                var day = dayName[now.getDay()-1];
                alert(day);
               
                var month = monName[now.getMonth()-1];
                alert(month);
               
                var year = now.getFullYear();
                alert(year);
            }
        </script>

Wednesday, 4 July 2012

Javascript adding and deleting node..

Adding and deleting node in javascript is pretty simple. You don't need to use jquery or any other library to accomplish such small tasks like this. See the example below.

I am using a textarea, submit button and an anchor tag to add and delete node through javascript.

Click here to see the working example. http://jsfiddle.net/coolvin21/QUUtk/

<html>
    <head>
        
<script type="text/javascript">
            window.onload = initAll; // CALLING FUNCTION ON WINDOW ONLOAD      
            function initAll(){
                document.getElementsByTagName("form")[0].onsubmit=submitfunc; // FUNCTION ON PRESS OF SUBMIT BUTTON
                document.getElementById("removePara").onclick=removePara; //FUNCTION ON CLICK ON ANCHOR
            };
          
            function submitfunc(){
                var tAareaVal = document.getElementById("tAreaId").value;
                var txtNode = document.createTextNode(tAareaVal); //TEXT NODE OF TEXT AREA VALUE
              
                var pElement = document.createElement("p");
                pElement.appendChild(txtNode); //INSERTING TEXTNODE IN PARAGRAPH TAG
              
                var bodyDoc = document.getElementsByTagName("body")[0];
                bodyDoc.appendChild(pElement); //WRITING INSIDE BODY TAG
              
                return false;
            };
          
            function removePara(){              
                var allGrafs = document.getElementsByTagName("p");
              
                if(allGrafs.length>=1){
                    var lastGraf = allGrafs.item(allGrafs.length-1); //LAST PARAGRAPH WOULD BE ALL PARAGRAPH-1
                    var bodyDoc = document.getElementsByTagName("body")[0];
                    var removed = bodyDoc.removeChild(lastGraf); //DELETING LAST PARA
                }
                else{
                    alert("No ParaGraph Added")
                }
            }          
        </script>
    </head>
    <body>
        <form id="formId">
            <textarea id="tAreaId" cols="30" rows="5">Please type here to add Paragraph</textarea><br />
            <input type="submit" value="CLICK HERE TO ADD PARAGRAPH">
        </form>
        <br /><br /><a href="#" id="removePara">CLICK HERE TO REMOVE PARAGRAPH</a>
    </body>
</html>​​​​​​​​​​​​​​​​​​​​​​​​​

Monday, 2 July 2012

JQuery Before & After Plugin

While working on an e-commerce site I needed a very interactive and interesting  plugin where i could show before and after effects in a single image rather than those two traditional before and after image. And here's what I used. Have a look..

http://www.catchmyfame.com/jquery/demo/7/

http://www.catchmyfame.com/jquery/demo/8/

And it's very simple to use. Remember it requires jQuery UI with JQuery.

<script type="text/javascript">
$(function(){
 $('#container').beforeAfter();
});
</script>


And wrap your images in a div ID container. That's it.

Have it downloaded from here..
http://www.catchmyfame.com/jquery/beforeafter.zip








Learning Coding through Video Tutorials

Video tutorials are a great way to enhance your existing skills or learning new skills. It's a classroom at your own convenience. You can stop, forward or backward and repeat as per your requirement unlike a regular classroom where a professor comes and throws his knowledge at his own speed which might be slower or faster depending upon students' learning capabilities. In case of video tutorials, you can move slowly, pause, rewind and repeat as many times as you want. And the best thing is that you do not even need to go to attend any classes.

But in case of learning coding through Video Tutorials, it comes with a great amount of disbelief or over confidence that we have learnt all that we have watched. You will start behaving or believing that you know all that you watched. But that it not true. There are serious threats of you being failed when you put yourself on test. Here are the steps you must follow while learning Coding through Video Tutorials.


  1. Make up your mind that you have to do the coding shown in the video tutorial yourself and independently.
  2. Watch the video very slowly and carefully. Use pause and rewind wherever not clear.
  3. Use notepad to write some important lines.
  4. After completing 1 lesson, do not start the next one. STOP.. come on, don't be greedy..
  5. Do all the coding yourself that you just watched in the video. If needed, watch the portion of the video again.
  6. Once everything is clear from the lesson, move ahead with the next chapter and follow the steps again.

Hope this helps some of learners like me..keep learning...