Below is a simple HTML + CSS + JavaScript (with no jQuery involved) document. If someone tests it he is gonna see that there's a simple text that fades and move when window is loaded.
Code Explanation :
Well its a very simple piece of code. In html there is a paragraph and with css it's opacity is set to 0. Now with JS this property of opacity is changed every 10 mili seconds by 0.01. But I used some global variables to do it (something I don't want).
What I want?
I want to make a function like fadeIn(element, changeOnEveryMiliSecond). In place of element the id of any element I want to fade in and changeOnEveryMiliSecond the change in opacity every 10 mili second. Can anyone guide me how I can do it with the code similar like mine and no global variables? Also I don't want to use any library like jQuery. I can do it that way but I am just making my JavaScript concepts better and figuring out how things are done ;)
Here's my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Fade In</title>
<style type="text/css">
html{
height: 100%;
width: 100%;
margin: 0 auto;
padding: 0;
}
#main_body{
background: black;
}
#text{
font: normal 2em ebrima;
color: white;
opacity: 0;
position: absolute;
}
</style>
<script type="text/javascript">
var op = 0;
var pos = 0;
function main(){
if(op != 1) op = op + 0.01;
if(pos != 50) pos = pos + 1;
document.getElementById('text').style.opacity = op;
document.getElementById('text').style.top = pos + 'px';
}
window.addEventListener('load', setInterval(main, 10), false);
</script>
</head>
<body id="main_body">
<p id="text">Hello World</p>
</body>
</html>