gdelfino
gdelfino

Reputation: 11181

Mouseover & Dynamic in Mathematica

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.

Upvotes: 14

Views: 542

Answers (5)

Mike Bailey
Mike Bailey

Reputation: 12817

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.

Upvotes: 7

Arnoud Buzing
Arnoud Buzing

Reputation: 15423

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

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

Upvotes: 9

Heike
Heike

Reputation: 24420

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"]}]
] 

Upvotes: 7

Mr.Wizard
Mr.Wizard

Reputation: 24336

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"]
 }]
]

Upvotes: 10

Sjoerd C. de Vries
Sjoerd C. de Vries

Reputation: 16232

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

Upvotes: 9

Related Questions