简体   繁体   English

jQuery悬停和单击功能

[英]jQuery hover and click function

I have 4 links that change the position of 4 divs inside a web page. 我有4个链接,可更改网页内4个div的位置。 I am using the following jQuery script to change the color of the links when I hover them. 悬停链接时,我使用以下jQuery脚本更改链接的颜色。

    $('a.menua').hover(function(){
    $(this).css({'color':'#2EC7C7'});
},
function(){
    $(this).css({'color':'white'});
});

how can i modify this script so that when i click a link it keeps the color from hover and not change when i take my mouse off of it? 我如何修改此脚本,以便在单击链接时它可以使颜色保持悬停状态,并且当我将鼠标从其上取下时,颜色也不会更改?

$('a.menua').click(function(){
     //content
});

I would use CSS for all of the styling, like this: 我将CSS用于所有样式,如下所示:

a.menua { color: white; }
a.menua:hover, a.menua.clicked { color: #2EC7C7; }

Then only use jQuery just toggle that class using .toggleClass() like this: 然后仅使用jQuery,只需使用.toggleClass()切换该类,如下所示:

$('a.menua').click(function(){
   $(this).toggleClass('clicked');
});

Or, if you want only one active at a time: 或者,如果您一次只希望一个活动:

$('a.menua').click(function(){
   $('a.menua').removeClass('clicked');
   $(this).addClass('clicked');
});

This makes your code simpler, lighter, and easier to maintain. 这使您的代码更简单,更轻便且更易于维护。 Also it keeps your styling information where (if at all possible) it belongs, in the CSS. 此外,它还将样式信息保留在CSS所属的位置(如果可能的话)。

The only part that should require JS is for the link to keep the same color after you take your mouse off of it. 唯一需要使用JS的部分是使链接从鼠标移开后保持相同的颜色。 CSS alone will let you control what color it has when you're hovering (with a:hover ) and during the mouse click (with a:active ). 单独使用CSS可以让您控制a:hover悬停时(使用a:hover )和单击鼠标时(使用a:active )的颜色。

Craver's suggestion to add a class with jQuery should take care of keeping the color after you move away, and as he said, it's nice to keep the style info in your CSS. Craver建议使用jQuery添加类时应注意在离开后保持颜色不变,正如他所说,将样式信息保留在CSS中很好。

If you use all four possible link styles, make sure you put them in this order: 如果使用所有四种可能的链接样式,请确保按以下顺序放置它们:

a:link { }
a:visited { }
a:hover { }
a:active { }

You can remember it with LoVe HAte - Link, Visited, Hover, Active. 您可以通过LoVe HAte记住它-链接,已访问,悬停,活动。

One other thought - you're trying to make the link color identical during hover and click. 另一种想法-您正在尝试使鼠标悬停并单击时链接颜色相同。 I'd suggest it may be better to let them be a little different. 我建议最好让它们有所不同。 Having the color change during the click gives the user visual feedback that they hit the target. 在单击期间进行颜色更改可为用户提供视觉上的反馈,表明他们击中了目标。

use element.Data: 使用element.Data:

$('a.menua').hover(function(){
    $(this).css({'color':'#2EC7C7'});
}, function(){
    if($(this).data("is-clicked") === false){
        $(this).css({'color':'white'});
    }
}).live("click", function(){
    $(this).data("is-clicked", !$(this).data("is-clicked"));
});;

But Nicks css version is probably the better way to go. 但是Nicks CSS版本 可能 是更好的选择。

I think you want this: live solution 我认为您想要这样: 实时解决方案

So, I use jQuery to add classes to links. 因此,我使用jQuery向链接添加类。 I also set the script to let only one item be active at a time (that's the main difference between this solution and Nick's). 我还将脚本设置为一次仅激活一项(这是此解决方案与Nick的主要区别)。

update: 更新:

The hovering-effect is now also CSS based (That is what the :hover pseudo class is for). 悬停效果现在也是基于CSS的(这是:hover伪类的作用)。 So you only use jQuery to set the "active" state of the link. 因此,您仅使用jQuery设置链接的“活动”状态。

HTML: HTML:

<a class="menulink" href="#">Link 1</a>
<a class="menulink" href="#">Link 2</a>
<a class="menulink" href="#">Link 3</a>

CSS: CSS:

a { color: #00f; }
a:hover, a.active { color: #f00; }

JS: JS:

$('.menulink').click(function(){
$(this).addClass("active").siblings().removeClass("active");
});

Add a class after click. 单击后添加课程。

css 的CSS

.somecolor {color:#2EC7C7}

js js

$('a.menua').click(function(){
    var $this = $(this);
    if($this.hasClass("somecolor")){
        $(this).removeClass("somecolor");
    }else{
        $(this).addClass("somecolor");
    }
});

$('a.menua').hover(function(){
    $(this).css({'color':'#2EC7C7'});
},
function(){
    $(this).css({'color':'inherit'});
});

I haven't tested it, but this might work: 我还没有测试过,但这可能有用:

$('a.menua').click(function(){
    $('a.menua').unbind('mouseleave');
});

js: js:

var link='null';
$('a.menua').click(function(){
    $(link).removeClass('clicked');
        $(this).addClass('clicked');
    link=$(this);

css: CSS:

a.menua {
    color: white;
    text-decoration:none;
}
a.menua:hover, a.menua.clicked {
    color: #2EC7C7;
}

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

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