繁体   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