简体   繁体   English

根据鼠标位置将悬停和活动颜色更改链接到Javascript

[英]links hover and active color change depending on mouse position with Javascript

I'm using a JS script to change div background colors depending of my mouse position. 我正在使用JS脚本根据鼠标位置更改div背景颜色。

$(document).mousemove(function(e){
    var $width = ($(document).width())/(252 - 23);
    var $height = ($(document).height())/(253 - 2);
    var $pageX = 253 - parseInt(e.pageX / $width,10);
    var $pageY = 200 - parseInt(e.pageY / $height,10) + 2;
        $("body").css("background-color", "rgb("+$pageY+","+$pageY+","+$pageY+")");
}); 

it works perfectly fine. 它工作得很好。

what I'm trying to do now is to apply the same color changes to my links when hover and when active. 我现在想做的是,当鼠标悬停和活动时,将相同的颜色更改应用于链接。

when trying this code, the color changes on hover, depending of the mouse position, but when mouseout the changed color belongs : 尝试此代码时,颜色会根据鼠标的位置在悬停时发生变化,但是当鼠标移出时,更改的颜色属于:

$(document).mousemove(function(e){
var $width = ($(document).width())/(252 - 23);
var $height = ($(document).height())/(253 - 2);
var $pageX = 253 - parseInt(e.pageX / $width,10);
var $pageY = 200 - parseInt(e.pageY / $height,10) + 2;
    $("a:hover").css("color", "rgb("+$pageX+","+$pageY+","+$pageX+")");
    $("a:hover").css("border-bottom", "1px dotted rgb("+$pageX+","+$pageY+","+$pageX+")");
    $("a:active").css("color", "rgb("+$pageX+","+$pageY+","+$pageX+")");
    $("a:active").css("border-bottom", "1px dotted rgb("+$pageX+","+$pageY+","+$pageX+")");

}); });

I think I need to add a mouseover and mouseout function but I don't know how to do this... 我想我需要添加一个mouseover和mouseout函数,但是我不知道该怎么做...

anyone know how I can do this ? 有人知道我该怎么做吗?

here is a jsfiddle : http://jsfiddle.net/BrZjJ/36/ 这是一个jsfiddle: http : //jsfiddle.net/BrZjJ/36/

thanks a lot for your help 非常感谢你的帮助

You better use mouseleave instead of mouseout 您最好使用mouseleave而不是mouseout

$('a').mouseleave(function(e){
    $('a').css({'color':'#000', 'border':'none'});
});

Because of some signifcant changes in the fiddle, I'll answer here instead of leaving it as a comment. 由于小提琴中发生了一些重大变化,因此我将在此处回答而不是将其保留为注释。 If I understand you right, you want the color of the link to change depending on where you move over it and having the link keep that color when you click on it (or activate it). 如果我没看错,您希望链接的颜色根据您在链接上的移动位置而改变,并让链接在单击(或激活)时保持该颜色。

In this jsFiddle it does just that: 在这个jsFiddle中,它只是这样做的:

CSS 的CSS

a.active {
    border-bottom: 1px dotted;
}
a:visted {
    color:blue;
}
a {
    color: blue;
    text-decoration: none;
}
a:hover {
    border-bottom: 1px dotted;
}

JavaScript 的JavaScript

var pageX, pageY;

$(document).mousemove(function (e) {
    var $width = ($(document).width()) / (252 - 23);
    var $height = ($(document).height()) / (253 - 2);
    var $pageX = 253 - parseInt(e.pageX / $width, 10);
    var $pageY = 200 - parseInt(e.pageY / $height, 10) + 2;
    $("body").css("background-color", "rgb(" + $pageY + "," + $pageY + "," + $pageY + ")");
});

$(document).mousemove(function (e) {
    var $width = ($(document).width()) / (252 - 23);
    var $height = ($(document).height()) / (253 - 2);
    pageX = 253 - parseInt(e.pageX / $width, 10);
    pageY = 200 - parseInt(e.pageY / $height, 10) + 2;

    $("a").on("mousemove", function () {
        $(this).css("color", "rgb(" + pageX + "," + pageY + "," + pageX + ")");
    }).on("mouseleave", function () {
        if (!$(this).hasClass("active")) $(this).removeAttr("style");
    });

    $("a.active").css("color", "rgb(" + pageX + "," + pageY + "," + pageX + ")");
});

$("a").on("click", function () {
    $("a").removeAttr("style").removeClass("active");
    $(this).addClass("active").css("color", "rgb(" + pageX + "," + pageY + "," + pageX + ")");
});

Edit: updated to change color of active link on moving mouse. 编辑:更新以更改移动鼠标上活动链接的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM