簡體   English   中英

褪色不起作用,JavaScript

[英]Fading doesn't work, JavaScript

這是非常簡單的任務,但是在某處是一個錯誤。.我找不到它。

function get(obj) { return document.getElementById(obj); }
var SomeObj = {
    t1:null,t2:null,
    hide: function() {
        if(parseFloat(get("wr_st").style.opacity)>0.45) {
            get("wr_st").style.opacity-=0.05;
        } else {
            clearInterval(SomeObj.t1);
        }
    },
    show: function() {
        if(parseFloat(get("wr_st").style.opacity)<1) {
            get("wr_st").style.opacity+=0.05;
        } else {
            clearInterval(SomeObj.t2);
        }
    },
    fade: function($wt) {
        if($wt==1) {
            clearInterval(menu.status.t2);
            menu.status.t1=setInterval('SomeObj.hide();',10);
        } else if($wt==2) {
            clearInterval(menu.status.t1);
            menu.status.t2=setInterval('SomeObj.show();',10);
        }
    }
}

因此,我有一個輸入(類型=文本)。 具有屬性:

onfocus="SomeObj.fade(1);" 
onblur="SomeObj.fade(2);".

Onblur不起作用。 更確切地說,這不起作用:

get("wr_st").style.opacity+=0.05;

如果要放置在這里,例如:alert('NOOOO'); 它會一直處於處理過程中,因為opacity + = 0.5無效。.您能幫我嗎:WTF是&為什么無效? 謝謝..

您正在為字符串添加數字,這會將數字轉換為字符串,而不是將字符串轉換為數字。 例如,如果當前值為"0.7" ,則最終以"0.70.05"而不是0.75

在添加之前分析字符串:

get("wr_st").style.opacity = (parseFloat(get("wr_st").style.opacity) + 0.05).toString();

(這當然是重復get調用和解析,而不是必要的,您可以將瞬時值存儲在臨時變量中,以減少代碼的重復性。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM