Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Monday, May 16, 2011

JavaScript Rolling Animation Using marginTop

In My previous Post I wrote about how You can get a rolling animtaion using javascript scrollTop property. As the "scrollTop" is not a W3C recommendation we will use 'marginTop' of the style object to make a better solution.

****
*****
Javacript Rolling Animation Example Using margin

*******
*********
*******
*******
*********
*******
***
Free Javascript scripts



We will set a negative value to the css margin-top property using a recursive function. This will move the div upwards gradually.Here is the sample Code.

d = document.getElementById('cat');

h = 0;

ab = function(){
 if(h == -150){
  h = 0;
  }
 h--;
 d.style.marginTop = h+"px";
 setTimeout('ab();', 10);
 }
 
ab();

Easy and pretty simple huh? This is just the basic, You can make awesome animations/sliding effects. This is compatible in all modern browsers. If you need more info, post a comment and I'll try to help.

Sunday, May 15, 2011

JavaScript Scrolling Animation Using scrollTop

Using only the scrollTop and scrollHeight property it is possible to create a nice scrolling effect.scrollTop and scrollHeight aren't W3C recommendation but these properties exist in all the modern browsers. I have checked it in Chromium,Firefox,IE and Midori the following script works perfectly. Let's see the effect.

Javacript Scrolling Animation Using scrollTop property
****
*****
*******
*********
*******
***
****
*****
*******
*********
*******
***
Javacript Scrolling Animation Using scrollTop property




The code is pretty simple. For achieving the animation effect out our div should be overflown.

a = document.getElementById('scroll');
c = 0;
b = function()
{
 if(200+a.scrollTop == a.scrollHeight)
 {
  a.scrollTop = 0;
  c = 0;
 };
 c++;
 a.scrollTop = c;
 setTimeout('b()',10)
}
b();

Need Explanation?

scrollTop(integer) property shows that how far you have scrolled from the top. You can also set a value to scrollTop which will scroll automatically to a value specified.

we have a recursive function that sets the scrollTop property. If we reach at the bottom of our element that's when if(200+a.scrollTop==a.scrollHeight) we reset the scrollTop property.

Here is a tutorial about How you can achieve the same animation using margin.

If You need more explanation, Let me Know.

Tuesday, May 3, 2011

JavaScript Find all Elements of an object

To find All functions and variables of a Javascript class or object you can use the following function. It's a silly little Function

function getObj(obj){ 

  var html = '';  

  for (var key in obj){

  html +=   (key + '  -- > '+ typeof obj[key]); 

  
}        

 return html;  

  }

You Can test the function here. Select a default Object and you will see the object elements. If You are using firefox you may seem nothing when you select the document object.






About Me

Web Developer From Dhaka, Bangladesh.