English 中文(简体)
为什么这样做 Java的外包功能失败吗?
原标题:Why does this fadeOut function in JavaScript fail?
  • 时间:2011-04-24 17:53:26
  •  标签:
  • javascript
var d = document.getElementById("box");

function fadeOut(r, s) {
    if (!this instanceof Element) return false;
    this.style.opacity = ".90";
    var opacity = this.style.opacity;
    var t = setInterval(function() {
        if (!r) r = 500;
        opacity = (opacity) - s;
        if (opacity == ".0") return;
    }, r);
};

fadeOut.apply(d, [100, 5]);

It does not work. What went wrong?

最佳回答

我知道这是你想要的答案,但如果你在某个项目中实际使用这一答案,那么有两点需要注意:

  • You are only animating opacity – remember that IE<9 doesn t support this property.
  • All current browsers that support opacity also support transitions (Firefox 3.X is the exception, but that s not going to hang around now 4 is out).

因此,在你所希望的所有现有浏览器中开展工作的更清洁的办法,是利用空间安全局建立这种系统。

-webkit-transition:opacity 0.6s ease-in-out;
-moz-transition:opacity 0.6s ease-in-out;
-o-transition:opacity 0.6s ease-in-out;
-ms-transition:opacity 0.6s ease-in-out;
transition:opacity 0.6s ease-in-out;

之后,在您的《联合材料》中,公正改变了不透明性。

this.style.opacity = ".90";

它将按要求进行校正,作为相当大笔的奖金,它将加速一些浏览器的硬件(当前是iOS)。

问题回答

当你去此行时:

var opacity = this.style.opacity;

从一项功能中转来,您需要clearInterval

function fadeOut(r, s) {
    var self = this;
    if (!self instanceof Element) return false;
    self.style.opacity = ".90";
    var t = setInterval(function() {
        if (!r) r = 500;
        self.style.opacity = self.style.opacity - s;
        if (self.style.opacity == ".0") {
            clearInterval(t);    
        }
    }, r);
};

Also, full opacity is 1 and none is 0. You are reducing it by s each time and s is defined as 5. Since you start by dropping to .9, you probably mean:

 fadeOut.apply(d, [100, .1]);

Your opacity starts at 0.9. Then you subtract 5 from it on every interval. What this means is that the first time the interval runs you will try to set the opacity to the string "-4.1", which will fail to parse as a valid opacity value, so the set will be ignored. And then you go around in circles. Did you mean 0.05 instead of 5? Or s/100 in the callback function?

var d = document.getElementById("box");

function fadeOut(r, s) {
    if (!this instanceof Element) return false;
    this.style.opacity = ".90";
    var opacity = this.style.opacity;
    var t = setInterval(function() {
        if (!r) r = 500;
        opacity = (opacity) - s;
        if (opacity == ".0") return;
    }, r); };

fadeOut.apply(d, [100, 5]);

There are many things wrong with this code.

首先,社会保障局的财产正在扼杀,因此,你的不透明补充线应当是:

opacity = parseInt(opacity) - 0.05 +   ;

平均率变成了星号,然后又增加了负0.05,然后用<代码>+ back。

其次,if (!r) r = 500;应当从匿名职能外界定。

应:

var d = document.getElementById("box");

function fadeOut(fadeScaler, hertz) {
    if (!this instanceof Element) return false;
    hertz = (!hertz) ? 60 : hertz; // Approx 60 hertz refresh rate

    var opacity = this.style.opacity
        opacity = ".9";

    var t = setInterval(
       function() {
         opacity = parseInt(opacity) - fadeScaler +   ;

         if (parseInt(opacity) <= 0) 
           clearInterval(t);
       },
       Math.floor(1000 / hertz)); // 1000 miliseconds / hertz = refresh rate
};

fadeOut.apply(d, [.05]);

更加注意你的法典的成文和风格。 一位立法者是智能代码。

此外,在《欧洲常规武装力量法》中,“不透明”相当于:

this.filters.alpha.opacity=100

页: 1





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

热门标签