Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a toggling Mouseover button?

I have to make a toggler-like control, which performs some action, when the mouse goes over it, and performs another action when it leaves the button, but does nothing while the mouse is over and moving.

To make it clear, it is not enough to simply find out whether the mouse is over a region or not, but it must run some code only when the mouse enters and leaves the region. (clarification requested by Szabolcs)

Sadly, the AutoAction option of Button behaves differently: it constantly tries to perform the action while the mouse is moving inside the object:

switch = False;
Button["Hover", switch = ! switch, AutoAction -> True]
Dynamic[switch]

On the other hand, a simple Mouseover command cannot perform code that is executed each time the mouse leaves the object:

in = out = 0;
Mouseover[
 Dynamic["out", (in++) &],
 Dynamic["in", (out++) &]
 ]
Dynamic[Column[{in, out}]]

I thought that the second argument of Dynamic could be used to define starting and ending actions for a Mouseover object, but had no success with this approach either.

start = end = False;
Dynamic[Mouseover["out", "in"], {(start = True) &, Null, (end = True) &}]
Dynamic[Column[{start, end}]]

Any ideas?

like image 437
István Zachar Avatar asked Jan 09 '12 15:01

István Zachar


People also ask

How do you make a mouseover in HTML?

Using <A> Tag Basically, what you're going to want to do is to create a link with an empty reference attribute (so clicking it doesn't take you anywhere), and use the title attribute to create whatever mouseover text you would like.

How do I toggle a button in JavaScript?

We can toggle a button using conditional statements like if-else statement in JavaScript. We can toggle almost all the properties of an element like its value, class, id, and color in JavaScript. To change any property of an element, we need to get the element using its id or class.

How do you make a button transparent?

The transparent button can be easily created by using HTML and CSS. In this article, we will use background-color: transparent; property to design the transparent background button. HTML Code: In this section, we will create a basic structure of button using the button Tag.


1 Answers

You could do something like this:

in = 0; out = 0;
Dynamic[Column[{in, out}]]
Module[{over = False},
 EventHandler[
  Pane[EventHandler[Dynamic[Framed[Pane[If[over, "In", "Out"], 40]]],
     {"MouseMoved" :> If[Not[over], over = True; in++]},
     PassEventsUp -> False], ImageMargins -> 4],
  {"MouseMoved" :> If[over, over = False; out++]}]]

What this does is to wrap the region for which you want to register the entries and exits into a slightly larger region using Pane. Both the inner region and the outer region have an EvenHandler which registers movements of the mouse. By setting PassEventsUp->False for the inner EventHandler, the "MouseMoved" events are passed to the outer EventHandler only if the mouse pointer is over the outer region but not over the inner region.

like image 51
Heike Avatar answered Sep 20 '22 07:09

Heike