在运行javascript例程之前,如何让Firefox在:hover*上更新背景色?

在运行javascript例程之前,如何让Firefox在:hover*上更新背景色?,javascript,firefox,canvas,event-handling,dom-events,Javascript,Firefox,Canvas,Event Handling,Dom Events,我有一个Firefox特有的问题,我写了一个脚本来创建3d布局 正确的行为是脚本从元素中提取背景色,然后使用该颜色在画布上绘制。当用户将鼠标悬停在链接上并且背景颜色更改为:hover规则时,画布上正在绘制的颜色也会更改。当用户退出鼠标时,颜色应恢复为非悬停颜色 这在Webkit浏览器和Opera中可以正常工作,但Firefox似乎不会在鼠标移出事件发生后立即更新CSS中的背景色,因此,如果鼠标移出事件发生,则不会绘制当前的背景色,并且后续不会出现另一个调用draw()例程的事件。它在Opera、

我有一个Firefox特有的问题,我写了一个脚本来创建3d布局

正确的行为是脚本从元素中提取背景色,然后使用该颜色在画布上绘制。当用户将鼠标悬停在链接上并且背景颜色更改为:hover规则时,画布上正在绘制的颜色也会更改。当用户退出鼠标时,颜色应恢复为非悬停颜色

这在Webkit浏览器和Opera中可以正常工作,但Firefox似乎不会在鼠标移出事件发生后立即更新CSS中的背景色,因此,如果鼠标移出事件发生,则不会绘制当前的背景色,并且后续不会出现另一个调用
draw()
例程的事件。它在Opera、Chrome和Safari中运行良好。我怎样才能让Firefox合作

我包含了我认为与我的问题最相关的代码。任何关于如何解决此问题并获得一致效果的建议都将非常有用

function drawFace(coord, mid, popColor,gs,x1,x2,side) {
    /*Gradients in our case run either up/down or left right.
    We have two algorithms depending on whether or not it's a sideways facing 
    piece. Rather than parse the "rgb(r,g,b)" string(popColor) retrieved from 
    elsewhere, it is simply offset with the gs variable to give the illusion that it 
    starts at a darker color.*/

    var canvas = document.getElementById('depth');
//This is for excanvas.js
    var G_vmlCanvasManager;
    if (G_vmlCanvasManager != undefined) { // ie IE
        G_vmlCanvasManager.initElement(canvas);
    }
    //Init canvas
    if (canvas.getContext) {
        var ctx = canvas.getContext('2d'); 
        if (side) var lineargradient=ctx.createLinearGradient(coord[x1][0]+gs,mid[1],mid[0],mid[1]);
        else var lineargradient=ctx.createLinearGradient(coord[0][0],coord[2][1]+gs,coord[0][0],mid[1]);
        lineargradient.addColorStop(0,popColor);
        lineargradient.addColorStop(1,'black');
        ctx.fillStyle=lineargradient;
        ctx.beginPath();
        //Draw from one corner to the midpoint, then to the other corner, 
        //and apply a stroke and a fill.
        ctx.moveTo(coord[x1][0],coord[x1][1]);
        ctx.lineTo(mid[0],mid[1]);
        ctx.lineTo(coord[x2][0],coord[x2][1]);
        ctx.stroke();
        ctx.fill();
    }
}

function draw(e) {
    var arr = new Array()
    var i = 0;
    var mid = new Array(2);
    $(".pop").each(function() {
        mid[0]=Math.round($(document).width()/2);
        mid[1]=Math.round($(document).height()/2);
        arr[arr.length++]=new getElemProperties(this,mid);
        i++;
    });
    arr.sort(sortByDistance);
    clearCanvas();
    for (a=0;a<i;a++) {
        /*In the following conditional statements, we're testing to 
        see which direction faces should be drawn,
        based on a 1-point perspective drawn from the midpoint. In the first 
        statement, we're testing to see
        if the lower-left hand corner coord[3] is higher on the screen than the 
        midpoint. If so, we set it's gradient
        starting position to start at a point in space 60pixels higher(-60) than 
        the actual side, and we also declare which corners make up our face, 
        in this case the lower two corners, coord[3], and coord[2].*/
        if (arr[a].bottomFace) drawFace(arr[a].coord,mid,arr[a].popColor,-60,3,2);
        if (arr[a].topFace) drawFace(arr[a].coord,mid,arr[a].popColor,60,0,1);
        if (arr[a].leftFace) drawFace(arr[a].coord,mid,arr[a].popColor,60,0,3,true);
        if (arr[a].rightFace) drawFace(arr[a].coord,mid,arr[a].popColor,-60,1,2,true);
    }
}

$("a.pop").bind("mouseenter mouseleave focusin focusout",draw);
函数绘图面(坐标、中间、popColor、gs、x1、x2、侧面){
/*在我们的例子中,渐变要么向上/向下,要么向右。
我们有两种算法,这取决于它是否是侧向的
而不是解析从中检索到的“rgb(r,g,b)”字符串(popColor)
在其他地方,它只是用gs变量进行偏移,以产生一种错觉,即
从较暗的颜色开始*/
var canvas=document.getElementById('depth');
//这是给excanvas.js的
var G_vmlCanvasManager;
如果(G_vmlcavasmanager!=未定义){//ie
G_vmlcavasmanager.initElement(画布);
}
//初始化画布
if(canvas.getContext){
var ctx=canvas.getContext('2d');
如果(侧)变量lineargradient=ctx.createLinearGradient(坐标[x1][0]+gs,中间[1],中间[0],中间[1]);
else var lineargradient=ctx.createLinearGradient(坐标[0][0],坐标[2][1]+gs,坐标[0][0],中间[1]);
lineargradient.addColorStop(0,popColor);
lineargradient.addColorStop(1,'black');
ctx.fillStyle=线性梯度;
ctx.beginPath();
//从一个角画到中点,然后再画到另一个角,
//并应用笔划和填充。
ctx.moveTo(coord[x1][0],coord[x1][1]);
ctx.lineTo(mid[0],mid[1]);
ctx.lineTo(coord[x2][0],coord[x2][1]);
ctx.stroke();
ctx.fill();
}
}
功能图(e){
var arr=新数组()
var i=0;
var mid=新阵列(2);
$(“.pop”)。每个(函数(){
mid[0]=数学圆($(document).width()/2);
mid[1]=数学圆($(document).height()/2);
arr[arr.length++]=新的getElempProperty(this,mid);
i++;
});
arr.sort(排序距离);
clearCanvas();

对于(a=0;a您可以使用回调将javascript方法延迟几毫秒。

我用setTimeout(function(){…},0)将代码包装在draw()函数中,它工作了!它的响应性不如以前,但已经足够好了。谢谢!