1

我正在尝试根据光标位置更改顶部和底部颜色的渐变。下面的函数在$(document.body).css('background','rgb('+rgb.join(',')+')');用于更改背景时有效,但我似乎无法让它与渐变一起使用。下面的代码是我为在 Firefox 中测试而设置的。我将更新代码,同时我目前尝试为每个浏览器设置选项。

http://coreytegeler.com/justin/

$(window).load(function(){
var $win = $(window),
    w = 0,h = 0,
    top = [],
    bottom = [],
    getWidth = function() {
        w = $win.width();
        h = $win.height();
    };

$win.resize(getWidth).mousemove(function(e) {

    top = [
        Math.round(e.pageX/w * 255),
        Math.round(e.pageY/h * 255),
        150
    ];

     bottom = [
        Math.round(e.pageX/h * 255),
        Math.round(e.pageY/w * 255),
        150
    ];

    $(document.body).css('background: -moz-linear-gradient(top, ('+top.join(',')+'), ('+bottom.join(',')+'))');
}).resize();

});
4

1 回答 1

2

一个问题是您使用.css错误的 javascript 方法。它有两个参数,或者一个对象。所以应该是:

$(document.body).css('background', '-moz-linear-gradient(top, rgb('+top.join(',')+'), rgb('+bottom.join(',')+'))');

或者

$(document.body).css({background : '-moz-linear-gradient(top, rgb('+top.join(',')+'), rgb('+bottom.join(',')+'))'});

除此之外,您的代码看起来基本正确。

于 2013-06-20T21:36:55.603 回答