简体   繁体   English

原型点击,鼠标悬停和鼠标移动不能一起工作?

[英]Prototype click, mouseover and mouseout can't work together?

I'm trying to do a very simple button that changes color based on mouseover, mouseout and click, I'm doing this in prototype and the weird thing is if I used mouseover and mouseout, after I clicked on the button, the button wouldn't change to white, seems like it is because of the mouseout, here's my code 我正在尝试做一个非常简单的按钮,根据鼠标悬停,鼠标移动和点击改变颜色,我在原型中这样做,奇怪的是如果我使用鼠标悬停和鼠标滚动,在我点击按钮后,按钮就不会变成白色,好像是因为鼠标输出,这是我的代码

$("izzy").observe('mouseover', function() {
     $('izzy').setStyle({ color: '#FFFFFF' });
});

$("izzy").observe('mouseout', function() {
     $('izzy').setStyle({ color: '#666666' });
});

$("izzy").observe('click', function() {
     $('izzy').setStyle({ color: '#FFFFFF' });
});

how can I fix it? 我该怎么解决? Thanks. 谢谢。

Unless there's something else happening in mouse over and out, why not use css? 除非在鼠标中出现其他问题,为什么不使用css?

#izzy:hover { color: '#FFFFFF'; }

However, I'm a little confused as to what exactly you want to happen. 但是,我对你究竟想要发生什么感到困惑。 Assuming you want the button white if it has been clicked or if the mouse is over it. 假设您希望按钮为白色,如果已单击或鼠标悬停在按钮上。 I'd have the click event handler add a clicked class, like so: 我有点击事件处理程序添加一个单击的类,如下所示:

$("izzy").observe('click', function() {
    $('izzy').addClass('selected');
});

And the css as so 和css一样

#izzy { color: '#666666'; }
#izzy:hover, #izzy.selected { color: '#FFFFFF'; }

This has the advantage of separating the state - clicked/not-click and mouse over/not over - from the style - black or gray. 这样做的好处是可以将状态 - 单击/不单击和鼠标悬停在/不结束 - 与样式 - 黑色或灰色分开。 Right now they're all mixed in together, creating confusion and opening yourself to bugs. 现在,他们全都混在一起,造成混乱,并让自己打开虫子。

Do you mean that you wan the mouse click to cause a permenant change in the style that is not replaced by mouseout? 你的意思是你用鼠标点击鼠标来改变鼠标输出没有取代的风格吗? If so, try using a flag, like so: 如果是这样,请尝试使用标志,如下所示:

var wasClicked = false;

$("izzy").observe('mouseover', function() {
    if (!wasClicked) $('izzy').setStyle({ color: '#FFFFFF' });
});

$("izzy").observe('mouseout', function() {
    if (!wasClicked) $('izzy').setStyle({ color: '#666666' });
});

$("izzy").observe('click', function() {
    $('izzy').setStyle({ color: '#FFFFFF' });
    wasClicked = true;
});

If you move the cursor away from the button after clicking on it, the last event is mouseout, so it happens no matter if you click or not. 如果在单击后将光标移离按钮,则最后一个事件是mouseout,因此无论您是否单击都会发生。

If you want to avoid the mouseout effect when it is clicked, try setting a flag when clicking and abortind the mouseout event if the flag is set. 如果要在单击时避免鼠标移除效果,请尝试在单击时设置标志,并在设置标志时中止mouseout事件。

Set a status to prevent the other events: 设置状态以防止其他事件:

var clicked = false
$("izzy").observe('mouseover', function() {
     if(!clicked) {
        $('izzy').setStyle({ color: '#FFFFFF' });
     }
});

$("izzy").observe('mouseout', function() {
     if(!clicked) {
        $('izzy').setStyle({ color: '#666666' });
     }
});

$("izzy").observe('click', function() {
    clicked = true
    $('izzy').setStyle({ color: '#cccccc' });
});

Using CSS for a hover and a selected class to colour the element is probably the best choice. 使用CSS进行悬停和选定的类为元素着色可能是最佳选择。 However, if you just want a quick fix to code you already have in place you could stop observing the mouseout event after it has been clicked. 但是,如果您只想快速修复已有的代码,则可以在单击后停止观察mouseout事件。

$("izzy").observe('click', function(e) {
     e.element().setStyle({ color: '#FFFFFF' });
     e.element().stopObserving('mouseout');
});

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

相关问题 Javascript:“鼠标悬停”和“鼠标悬停”事件处理程序有效,但当“鼠标悬停”被“单击”替换时“鼠标悬停”无法正常工作 - Javascript: "Mouseover" and "mouseout" event handlers work, but "mouseout" doesn't work properly when "mouseover" is replaced by "click" 如何将鼠标悬停,鼠标悬停和单击结合起来。 没有.mouseout破坏.click - How can i combine mouseover, mouseout and click. Without having .mouseout ruin .click 将“单击”function 更改为 mouseover/mouseout - Change 'Click' function to mouseover/mouseout 如何让 mouseover/mouseout 与调整大小一起工作? - How can I get mouseover/mouseout to work with resizing? Fancybox:在鼠标悬停/鼠标悬停时使用它 - Fancybox: Getting it to work on mouseover/mouseout jQuery Mouseover / Mouseout 不适用于 Live - jQuery Mouseover / Mouseout will not work with Live 我无法在Tumblr上进行mouseover / mouseout JQuery div转换 - I can't make a mouseover/mouseout JQuery div transition on Tumblr 鼠标悬停和鼠标移出事件会不匹配吗? - Can mouseover and mouseout events be mismatched? 鼠标悬停,鼠标移开并单击谷歌地图上的相同标记 - mouseover, mouseout and click on the same marker on a google map 将 div 转换为功能 click、mouseover 和 mouseout - Turning divs into functions click, mouseover, and mouseout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM