English 中文(简体)
Fade HTML element with raw javascript
原标题:

It s my second question of the day related to the same problem, so I apologize for that.

I was able to put together a function to "fade out" an element, and it works just fine, my problem is that when I try to reverse it, so the element "fades in" it does not work. I ve tried to change the obvious, but I can t understand what I m doing wrong.

My code so far is as follows:

Given I have a "div" like so:

<div id="test" style="width:200px; height:200px; display:block; opacity:1; background-color:red;"></div>

The JavaScript function that I m using to fade it out is:

var getElement = document.getElementById( test );
function fadeOut(elem, speed){
if(!elem.style.opacity){
    elem.style.opacity = 1;
}
var desvanecer = setInterval(function(){
    elem.style.opacity -= .02;
    if(elem.style.opacity < 0){
        clearInterval(desvanecer);
    }
}, speed / 50);
}
fadeOut(getElement, 500);

Could somebody take a look at this and let me know what I m doing wrong, all I want to do is "FADE IN" an element to an opacity equal to "1".

By the way, I can t use jQuery, however I m eager to learn this way.

Thanks

My attemp to reverse the function is as follows:

var getElement = document.getElementById( test );
function fadeIn(elem, speed){
if(elem.style.opacity){
    elem.style.opacity = 0;
}
var desvanecer = setInterval(function(){
    elem.style.opacity += .02;
    if(elem.style.opacity > 1){
        clearInterval(desvanecer);
    }
}, speed / 50); 
}
fadeIn(getElement, 500);
最佳回答

setInterval runs in a global scope, so you need to define the timer relative to the window.

You can t concatinate the string value returned from the style property and expect a number- you ll get 0.02.02.02.02

Coerce a number out of the string, then add the .02.

It will work in some browsers, but IE before 9 needs a different expression to set and read opacity.

function fadeIn(elem, speed){
    if(elem.style){
        elem.style.opacity=  0 ;
    }
    window.fadetimer= setInterval(function(){
        elem.style.opacity= +(elem.style.opacity)+.02;
        if(elem.style.opacity> 1){
            clearInterval(fadetimer);
        }
    },
    speed);
}
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签