Mouseover & Dynamic in Mathematica

553 Views Asked by At

Could anyone point out why this is not working in Mathematica 8:

DynamicModule[{x = Pink},
 Row[
  {Style["Hello", x],
   Mouseover[
    x = Green; "World",
    x = Blue; "World"]}]]

What I expect is to see the color of "Hello" change when I mouse over "World". What I am getting is a pink "Hello" that never changes color.

5

There are 5 best solutions below

0
Mike Bailey On BEST ANSWER

A quick check shows that Mouseover evaluates all the expressions inside of it when you first launch it:

Mouseover[Print["One"]; 1, Print["Two"]; 2]

The idiomatic way of actually making the Mouseover modify the values of x is to use MouseAnnotation. Mr. Wizard's answer describes how to achieve this.

0
Sjoerd C. de Vries On

If you look at the FullForm of the result, you'll see that it only contains the last part of each compound instruction set. Apparently Mouseover evaluates its arguments and only stores the results.

enter image description here

1
Heike On

As an alternative you could do something like

DynamicModule[{col = Pink}, 
 Row[{Style["Hello ", FontColor -> Dynamic[col]], 
   Dynamic@If[CurrentValue["MouseOver"],
     col = Green; "World", 
     col = col /. Green -> Blue; "World"]}]
] 
2
Mr.Wizard On

I think I have waited long enough to be fair. Here is my proposal:

DynamicModule[{x = Pink},
 Row[{
   Dynamic@Style["Hello", If[MouseAnnotation[] === 1, x = Green; Blue, x]],
   Annotation["World", 1, "Mouse"]
 }]
]
6
Arnoud Buzing On

Try using EventHandler with "MouseEntered" and "MouseExited":

DynamicModule[{c = Pink}, Row[{
 Style["Hello", FontColor -> Dynamic[c]], 
 EventHandler[
  "World", {
   "MouseEntered" :> (c = Blue), 
   "MouseExited" :> (c = Green)
}]}]]