0

我有一个很大的精灵图像,我只是将此图像设置为我的 30 个李背景的背景。设置背景后,悬停时,我只想将 y 位置减去 70px,所以它会到达上方。

因为,我正在从 yi 中检索当前的 x、y 和存储,只是减少 70px 以实现我的悬停位置,但我没有得到正确的结果。它有效,但它非常有问题,任何人都可以更正我的代码以更好地工作吗?

我的代码:

$('.logo-gal li').each(function(index){ // dynamically i am setting the bg, works.
        index % 5 == 0 ? y+=1 : y;
        $(this).css({
            background:'url(images/css/gall-logo.png) no-repeat',
            backgroundPosition: -((index%5) * 106)+'px '+ -(y*140) +'px'
        })

    })

    $('.logo-gal li').hover(function(){
        var backPos = $(this).css('backgroundPosition').split(" ");
        var xPos = parseInt(backPos[0]),yPos = parseInt(backPos[1]);//storing x y pos.
        console.log(xPos);
        $(this).css({backgroundPosition: xPos+ (yPos-70)+"px"});// applying too.. not work, and i need to setback the stored dada, while mouseout..
    })

有什么好的建议吗?

4

1 回答 1

1

你有字符串连接的问题

$('.logo-gal li').each(function(index){ // dynamically i am setting the bg, works.
        index % 5 == 0 ? y+=1 : y;
        $(this).css({
            background:'url(images/css/gall-logo.png) no-repeat',
            backgroundPosition: '-'+eval((index%5) * 106)+'px  -'+eval(y*140) +'px'
        })

    })

    $('.logo-gal li').hover(function(){
        var backPos = $(this).css('backgroundPosition').split(" ");
        var xPos = parseInt(backPos[0]),yPos = parseInt(backPos[1]);//storing x y pos.
        console.log(xPos);
        $(this).css({backgroundPosition: eval(xPos+ yPos)+"px"});// applying too.. not work, and i need to setback the stored dada, while mouseout..
    })
于 2012-05-26T10:34:41.283 回答