2

我正在尝试根据鼠标位置更改元素的不透明度(在本例中为 .png)。

我发现了这个很好的颜色示例:http: //jsfiddle.net/WV8jX/

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

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

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

$(document.body).css('background','rgb('+rgb.join(',')+')'); }).resize();

但我很困惑如何使用不透明度来做到这一点。我想要同样流畅无缝的效果。

所有帮助将不胜感激!

4

2 回答 2

2

这很简单:

JSFiddle

$(function(){
    var $win = $(window),
    w = 0,h = 0,
    opacity = 1,
    getWidth = function() {
        w = $win.width();
        h = $win.height();
    };

    $win.mousemove(function(e) {
        getWidth();
        opacity = (e.pageX/w * 0.5) + (e.pageY/h * 0.5);

        $('#myElement').css('opacity',opacity);

    });
});

我编辑了整个调整大小的东西,因为它对我来说没有意义。

于 2013-04-06T16:40:37.607 回答
0
<div id="wrapper"><h1></h1></div>

http://jsfiddle.net/WV8jX/159/

var $win = $(window),
    w = 0,
    h = 0,
    opacity = 0,
    getWidth = function() {
        w = $win.width();
        h = $win.height();
    };

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

    opacity = (e.pageX/w) * (e.pageY/h);
    $('h1').text(opacity);

    $('#wrapper').css('opacity',opacity);

}).resize();
于 2013-04-06T16:47:17.937 回答