在鼠标悬停之前,Marque不会启动

时间:2013-01-22 21:14:00

标签: onload marquee

我正在尝试将文字选框放入我的网站。一切正常,除了文本在鼠标滚动之前不会开始滚动。我需要它在页面加载时开始滚动。

相同的代码在这里开始onload:http://javascript.about.com/library/blctmarquee1.htm

我的控制台说:

Uncaught TypeError: Cannot call method 'getElementsByTagName' of null 

mq                 Marquee_Testing.html:61

start              Marquee_Testing.html:41

这是javascript:

function start() {
        new mq('m1');
        new mq('m2');
        mqRotate(mqr); // must come last
    }
    window.onload = start;

    function objWidth(obj) {
        if (obj.offsetWidth) return obj.offsetWidth;
        if (obj.clip) return obj.clip.width;
        return 0;
    }
    var mqr = [];

    function mq(id) {
        this.mqo = document.getElementById(id);
        var wid = objWidth(this.mqo.getElementsByTagName('span')[0]) + 5;
        var fulwid = objWidth(this.mqo);
        var txt = this.mqo.getElementsByTagName('span')[0].innerHTML;
        this.mqo.innerHTML = '';
        var heit = this.mqo.style.height;
        this.mqo.onmouseout = function () {
            mqRotate(mqr);
        };
        this.mqo.onmouseover = function () {
            clearTimeout(mqr[0].TO);
        };
        this.mqo.ary = [];
        var maxw = Math.ceil(fulwid / wid) + 1;
        for (var i = 0; i < maxw; i++) {
            this.mqo.ary[i] = document.createElement('div');
            this.mqo.ary[i].innerHTML = txt;
            this.mqo.ary[i].style.position = 'absolute';
            this.mqo.ary[i].style.left = (wid * i) + 'px';
            this.mqo.ary[i].style.width = wid + 'px';
            this.mqo.ary[i].style.height = heit;
            this.mqo.appendChild(this.mqo.ary[i]);
        }
        mqr.push(this.mqo);
    }
    function mqRotate(mqr) {
        if (!mqr) return;
        for (var j = mqr.length - 1; j > -1; j--) {
            maxa = mqr[j].ary.length;
            for (var i = 0; i < maxa; i++) {
                var x = mqr[j].ary[i].style;
                x.left = (parseInt(x.left, 10) - 1) + 'px';
            }
            var y = mqr[j].ary[0].style;
            if (parseInt(y.left, 10) + parseInt(y.width, 10) < 0) {
                var z = mqr[j].ary.shift();
                z.style.left = (parseInt(z.style.left) + parseInt(z.style.width) * maxa) + 'px';
                mqr[j].ary.push(z);
            }
        }
        mqr[0].TO = setTimeout('mqRotate(mqr)', 10);
    } 

1 个答案:

答案 0 :(得分:0)

我通过删除

来修复此问题
  

new mq('m2');

我仍然不知道为什么这会影响marquee'm1'滚动onload的能力,但它在任何情况下都已解决。