English 中文(简体)
Fx on JavaScript: How to fade stuff, etc without frameworks?
原标题:

I would like to learn how to do fade, and similar effects on JavaScript. I often get answers, like why not use jQuery, Mootools, etc ? Well, I want to learn how stuff works, then I won t mind using any of these frameworks.

I m currently learning about making changes on the DOM, so, I ve read a lot of stuff on this theme. Also, I ve read about Reflow, still, I didn t find any cool stuff on Repaint, but, I ll keep searching.

From seeing source files etc, I see a few methods, that I don t know if they ve created or are Core methods of JS.

My question is, is there any resource where I can learn all this neat stuff like smooth position change, fading elements trough opacity or whatever, etc?

最佳回答

here s an example that works in firefox and chrome. ie doesn t respect the opacity style.

    var ELEMENT;
    var STEPS;
    var INTERVAL;
    var COUNT;
    var TIMERID;

    // 5 * 200ms = 1 second
    STEPS = 5;
    INTERVAL = 200;

    function Button1_onclick() {

        ELEMENT = document.getElementById("foo");
        COUNT = STEPS - 1;
        TIMERID = setInterval(Fade, INTERVAL);
    }

    function Fade() {

        ELEMENT.style.opacity = String(COUNT / STEPS);
        COUNT--;

        if (COUNT < 0) {
            clearInterval(TIMERID);
            TIMERID = 0;
        }
    }

setInterval and clearInterval are standard js functions. they will execute the given function every x milliseconds. in our case we kill it when we ve hit 0 opacity.

sliding a window is a similar process. you d set the left/right/top/bottom style instead of opacity.

问题回答

Take a look at emile.js. It s brand-spanking new. Great way to learn how to do your own.

Introduced at the recent jsconf conference. Written by Thomas Fuchs (script.aculo.us).

http://github.com/madrobby/emile

Émile Stand-alone CSS animation

JavaScript mini-framework

Doesn t need a JavaScript framework

Full set of CSS properties for animation (length-based and colors)

Easing and callbacks

Less than 50 lines of code

Get updates on Twitter: http://twitter.com/emilejs

Fading using javascript is basically modifying opacity of an element programmatically. The "smoothness" is done by using timers. Same for position/size changes. You need to read up on css to understand what style properties of an element you have to control using javascript for the effect you want.

If you are really curious, you can dive into the source of yui in github: http://github.com/yui





相关问题
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.

热门标签