简体   繁体   English

单击JavaScript中的自定义CSS按钮后是否恢复悬停状态?

[英]Restoring hover state after click for custom CSS buttons in JavaScript?

I have a CSS button that has normal, pressed and hover states. 我有一个具有正常,按下和悬停状态的CSS按钮。 Everything works fine except that when the click has happened, I need to somehow know whether the style should be set to normal or hover. 一切正常,除了单击发生后,我需要以某种方式知道应将样式设置为正常还是悬停。 That is, I need a way of knowing if the mouse cursor is still hovering the element. 也就是说,我需要一种方法来了解鼠标光标是否仍在悬停该元素。 How would one achieve this with JavaScript? 用JavaScript如何做到这一点?

If you're concerned about the user doing a mousedown , then moving the pointer off (and perhaps on again) the button, you could do something like this: 如果您担心用户执行mousedown ,然后将指针移开(或再次移开)按钮,则可以执行以下操作:

EXAMPLE: http://jsfiddle.net/7zUaj/1/ 示例: http //jsfiddle.net/7zUaj/1/

var mouseIsDown = false; // Track/remember mouse up/down state for the button

    // Handle mouseenter and mouseleave
$('div').hover(function() {
    $(this).addClass('hover');
    if (mouseIsDown) 
        $(this).addClass('pressed'); // If mouse button was down, and user exited
                                     //   and reentered the button, add "pressed"
}, function() {
    $(this).removeClass('hover pressed');  // Remove both hover and pressed when
                                           //    the pointer leaves the button
})
  // Handle the mousedown, track that it is down, and add "pressed" class
.mousedown(function() {
    mouseIsDown = true;
    $(this).addClass('pressed');
})
 // Handle the mouseup, track that it is now up, and remove the "pressed" class
.mouseup(function() {
    mouseIsDown = false;
    $(this).removeClass('pressed');
});

 // If user does mousedown, leaves the button, and does mouseup,
 //     track that it is now up
$(document).mouseup(function() {
    mouseIsDown = false;
});​

The state of the mouse is tracked in a variable and set in the button's handlers for mousedown and mouseup . 鼠标的状态在一个变量中进行跟踪,并在button的处理程序中设置mousedownmouseup The mouseup is also tracked at the document level. document级别还可以跟踪mouseup This will help the mouseenter part of the .hover() to know if it should set the pressed class or not. 这将帮助mouseenter的一部分.hover()知道它是否应该设置pressed类与否。

(Note that because the mouseup is also tracked on the document , if there are any other elements on the page that stop the event from bubbling, the mouseup would not be detected by the document .) (请注意,因为还会在document上跟踪mouseup,所以如果页面上还有其他任何阻止事件冒泡的元素,则document不会检测到mouseup 。)

EDIT: Made it so the document only tracks mouseup , and the button tracks both. 编辑:做到这一点,因此document仅跟踪mouseup ,并且按钮同时跟踪两个。

You don't have a :visited state in your CSS? 您的CSS中没有:visited状态? As for Javascript, OnMouseOver or JQuery mouseover , hover or mouseenter (depending on what you want to do) will tell you when the hover is happening. 至于Javascript, OnMouseOver或JQuery mouseoverhovermouseenter (取决于您要执行的操作)将告诉您何时进行悬停。

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

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