Greetings,
I am looking to dynamically change the background-color for different states (normal, hover, pressed) of a button. Here is what I have come up with so far: http://jsfiddle.net/suamikim/c3eHh/
Ext.onReady(function() {
function updateBackground() {
var theWin = win || this,
btn = theWin.down('#btnTest'),
bckgr = theWin.down('#btnBckgr').getValue(),
bckgrHover = theWin.down('#btnBckgrHover').getValue(),
bckgrPressed = theWin.down('#btnBckgrPressed').getValue();
$('#' + btn.id).css('background', bckgr);
$('#' + btn.id).hover(function() {
$('#' + btn.id).css('background', bckgrHover);
}, function() {
$('#' + btn.id).css('background', bckgr);
}
);
$('<style type="text/css"> #' + btn.id + '.x-btn-pressed { background: ' + bckgrPressed + ' !important } </style>').appendTo('head');
};
var win = Ext.create('Ext.window.Window', {
width: 800,
height: 200,
layout: 'fit',
// Additional code omitted for brevity
listeners: {
'afterrender': updateBackground
}
}).show();
});
This approach works but raises some questions:
1)
Is it necessary or safe to call the hover function in jQuery every time updateBackground is called?
According to the jQuery documentation (http://api.jquery.com/hover/), this function binds functions to mouse events. Does it create new bindings each time, update existing ones, or handle disposal automatically?
2)
Setting styles as document-level for the pressed state can clutter the document header if updateBackground is called frequently. Is there a better approach or a way to remove previously set styles before adding new ones?
3)
The hover state's background does not show when the button is pressed due to the !important flag. Removing it causes issues with displaying the pressed background correctly. Any solutions?
Please provide suggestions that are generic and adaptable to other Ext controls beyond buttons like panels, checkboxes, etc. Thank you.