Today I will tell you about animations in web using jQuery. As we know – jQuery offer us several default methods of effects: fade(In/Out/To), hide, show, slide(Down/Toggle/Up), toggle. This is ok, but what if we need more? So, animation will help us! It will help to slide between 2 states using really good effects. By default, ‘easing’ is optional param, so to use extra effects we will use additional jquery.easing.js library.
Here are samples and downloadable package:
[sociallocker]
[/sociallocker]
Ok, download the example files and lets start coding !
Step 1. HTML
As usual, we start with the HTML.
This is our main page code.
index.html
01 | <script src="js/jquery.min.js"></script> |
02 | <script src="js/jquery.easing.1.3.js"></script> |
03 | <script src="js/main.js"></script> |
04 | <link rel="stylesheet" href="templates/css/main.css" type="text/css" /> |
05 | <div class="animation_example"> |
06 | <h3><a href="#">Animation in jQuery</a></h3> |
08 | <div style="margin-bottom:10px;"> |
09 | <h4>Sample object:</h4> |
10 | <div class="example_keeper"> |
11 | <img id="example" src="files/image.jpg"/> |
14 | <input type="radio" value="width" checked name="action" class="action"> Changing width |
15 | <input type="radio" value="position" name="action" class="action"> Changing position |
16 | <input type="radio" value="opacity" name="action" class="action"> Changing opacity |
17 | <select class="selector"></select> |
21 | <b>An example of animations.</b> I made 3 different types of animations: changing size (width), position and opacity. Also I pull all possible effects of easing into Select element. Just choose necessary type and select effect to get demonstration. Try it! |
Step 2. CSS
Here are used CSS styles.
templates/css/main.css
1 | body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0} |
2 | .animation_example{background:#FFF;width:865px;font-size:80%;border:1px #000 solid;margin:0.5em 10% 0.5em;padding:1em 2em 2em;-moz-border-radius: 3px;-webkit-border-radius: 3px} |
3 | .actions{margin:10px 0} |
4 | .example_keeper{overflow: hidden;width:100%;height:520px;border:1px solid #DDD;background-color:#eee} |
Step 3. JS
Here are necessary JS files to our project.
js/main.js
01 | $(document).ready(function() { |
02 | var sel1 = $(".selector"); |
03 | for (x in jQuery.easing) { |
04 | sel1.append($('<option>').attr('value', x).text(x)); |
06 | sel1.change(function(){ |
07 | var method = $(this).val(); |
08 | var effect = $('input[name=action]:checked').val(); |
11 | $('#example').animate({width:512}, {duration: 1000, easing: method}) |
12 | .animate({width:256}, {duration: 1000, easing: method}); |
15 | $('#example').animate({marginLeft:256}, {duration: 1000, easing: method}) |
16 | .animate({marginLeft:0}, {duration: 1000, easing: method}); |
19 | $('#example').animate({opacity:0}, {duration: 1000, easing: method}) |
20 | .animate({opacity:1}, {duration: 1000, easing: method}); |
js/jquery.easing.1.3.js and js/jquery.min.js
This is common files – jQuery library with addon. No need to give full code of that file here. It always available as a download package
Step 4. Images
Also we need source image for our project:
Conclusion
Today I told you how using animations in jQuery. Sure that you will happy to use it in your projects. Good luck!