简体   繁体   English

褪色不起作用,JavaScript

[英]Fading doesn't work, JavaScript

It's very simple task, but somewhere is a mistake.. I can't find out it. 这是非常简单的任务,但是在某处是一个错误。.我找不到它。

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);
        }
    }
}

So, I have an input (type=text). 因此,我有一个输入(类型=文本)。 With attributes: 具有属性:

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

Onblur doesn't work. Onblur不起作用。 More precisely, this doesn't work: 更确切地说,这不起作用:

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

If I'll place here for ex.: alert('NOOOO'); 如果要放置在这里,例如:alert('NOOOO'); it will be always in process, because opacity+=0.5 doesn't work.. Can you help me: WTF is that & why it doesn't work? 它会一直处于处理过程中,因为opacity + = 0.5无效。.您能帮我吗:WTF是&为什么无效? Thank you.. 谢谢..

You are adding a number to a string, which will convert the number to a string rather than converting the string to a number. 您正在为字符串添加数字,这会将数字转换为字符串,而不是将字符串转换为数字。 If the current value is for example "0.7" you end up with "0.70.05" instead of 0.75 . 例如,如果当前值为"0.7" ,则最终以"0.70.05"而不是0.75

Parse the string before adding: 在添加之前分析字符串:

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

(This of course is repeating the get call and the parsing more than neccesary, you can store immediate values in tempory variables to make the code less repetetive.) (这当然是重复get调用和解析,而不是必要的,您可以将瞬时值存储在临时变量中,以减少代码的重复性。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM