Hover and pressed in javafx
Asked Answered
H

1

8

I wrote this css code for button pressed

  .button:pressed {
        -fx-scale-y: 0.9;
        -fx-scale-x: 0.9;
}

and this for button hover

 .button:hover {
        -fx-background-color: #383838;
        -fx-scale-y: 1.1;
}

and it doesn't scale down because for the button to be clicked it obviously has to be hovered. Is there any way to cancel the hover code when the button is being pressed?

Hardtop answered 20/4, 2017 at 1:5 Comment(0)
L
19

Either:

reverse the order of the rules, so that the rule for pressed comes after the rule for hover (and thus takes precedence):

.button:hover {
    -fx-background-color: #383838;
    -fx-scale-y: 1.1;
}

.button:pressed {
    -fx-scale-y: 0.9;
    -fx-scale-x: 0.9;
}

or, make the rule for pressed more specific than the rule for hover by explicitly applying it to a button that is both pressed and hovered:

.button:pressed:hover {
    -fx-scale-y: 0.9;
    -fx-scale-x: 0.9;
}

.button:hover {
    -fx-background-color: #383838;
    -fx-scale-y: 1.1;
}
Lavinalavine answered 20/4, 2017 at 2:9 Comment(2)
If the answer helped you solve your problem, you should select it as correct.Nickles
@FernandoGaggero (Previous comment)Lavinalavine

© 2022 - 2024 — McMap. All rights reserved.