Javascript不透明度变化

时间:2013-09-23 18:35:01

标签: javascript html random opacity

好吧,我做了一些修改,解决了一些问题,但现在它没有通过所有字母,一些字母可见,然后脚本停止。另外,我怎么让这些字母淡入?我以为我有这个,但剧本不会因为某种原因暂停,你看不到褪色。

<script type="text/javascript" >

        //alert("Script Loaded");

        var $TotalLetters = 16,
        $SwappedLets = 0,
        $L1 = false,
        $L2 = false,
        $L3 = false,
        $L4 = false,
        $L5 = false,
        $L6 = false,
        $L7 = false,
        $L8 = false,
        $L9 = false,
        $L10 = false,
        $L11 = false,
        $L12 = false,
        $L13 = false,
        $L14 = false,
        $L15 = false,
        $L16 = false,
        $RanNum = 0,
        $Holderval = 0;

        window.onload = function DOL() {
            DoOnLoad()
        }

        function DoOnLoad() { 
            //alert("DoOnLoad");
            /* Change the opacity of every letter randomly,
            CAN CHANGE IN SMALL TAG */

            if ($SwappedLets <= $TotalLetters + 1) {
                $RanNum = Math.floor(Math.random()*17); 
                //alert(window["$L" + $RanNum]);
                 if (window["$L" + $RanNum] == false) {
                    window["$L" + $RanNum] = true;
                    OPIncre($RanNum);
                }
            }

        } 

        function OPIncre(RN) {
            if (document.getElementById("tL" + RN).style.opacity <= 1) {
                document.getElementById("tL" + RN).style.opacity = $Holderval + 0.001;
                $Holderval = $Holderval + 0.001;
                window.setTimeout(OPIncre(RN),600);
            }
            else {
                $SwappedLets = $SwappedLets + 1;
                $Holderval = 0;
                window.setTimeout(DoOnLoad,500);    
            }
        } 

    </script>

---- OLD:

我是Javascript的新手,我无法让脚本随机更改html元素的不透明度。默认情况下,元素的不透明度在css中设置为0。我试过环顾四周,还没有找到解决方案。我在Firefox的Javascript错误控制台中没有收到任何错误。有什么明显错误的吗?

<script type="text/javascript" >
        /*alert("Script Loaded");*/
        var $TotalLetters = 16,
        $SwappedLets = 0,
        $L1 = false,
        $L2 = false,
        $L3 = false,
        $L4 = false,
        $L5 = false,
        $L6 = false,
        $L7 = false,
        $L8 = false,
        $L9 = false,
        $L10 = false,
        $L11 = false,
        $L12 = false,
        $L13 = false,
        $L14 = false,
        $L15 = false,
        $L16 = false,
        $RanNum = 0;

        window.onload = function DOL() {
            DoOnLoad()
        }

        function DoOnLoad() {
            /*alert("DoOnLoad");*/
            /* Change the opacity of every letter randomly,
            CAN CHANGE IN SMALL TAG */

            if ($SwappedLets <= $TotalLetters + 1) {
                $RanNum = Math.floor(Math.random()*17);
                /*alert(window["$L" + $RanNum]);*/
                if (window["$L" + $RanNum] == false) {
                    window["$L" + $RanNum] = true;
                    OPIncre($RanNum);
                }
            }

        }

        function OPIncre(RN) {
            while (document.getElementById("tL" + RN).style.Opacity <= 1) {
                document.getElementById("tL" + RN).style.Opacity = document.getElementById("tL" + RN).style.Opacity + 0.1;
            }
            $SwappedLets = $SwappedLets + 1;
            window.setTimeout(DoOnLoad,500);
        }
    </script>

1 个答案:

答案 0 :(得分:0)

不确定“tL”是什么类型的元素,但假设它们属于<p>类型,以下脚本应该可以解决这个问题:

var $elements;
window.onload = function DOL() {
    // Get all elements
    $elements = $("p[id^='tL']");

    // Loop through all of them
    $elements.each(function(i, e) {
        e.style.opacity = Math.random() + 0.17;
    });
}

请注意,我在这里使用jQuery。由于您对变量使用了dollor符号,我假设您也在使用它。