Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using some more specific css selectors you can take advantage of Ext's pressedCls and overCls configs. <a href="http://docs.sencha.com/ext-js/4-1/#!/api/Ext.button.Button-cfg-pressedCls">http://docs.sencha.com/ext-js/4-1/#!/api/Ext.button.Button-cfg-pressedCls</a> <a href="http://docs.sencha.com/ext-js/4-1/source/Button2.html#Ext-button-Button-cfg-overCls">http://docs.sencha.com/ext-js/4-1/source/Button2.html#Ext-button-Button-cfg-overCls</a></p> <pre><code>.x-btn.my-over { background: blue; } /*Ext is not consistent */ .x-btn.x-btn-my-pressed { background: red; } new Ext.button.Button({ overCls : 'my-over', pressedCls : 'my-pressed', //needs to be true to have the pressed cls show enableToggle : true, text : 'My button', renderTo : Ext.getBody(), }) </code></pre> <hr> <p>Edit for a more dynamic, generic solution</p> <pre><code> Ext.define('DynamicStyleState', { alias : 'plugin.dynamicStyleState', extend : Ext.AbstractPlugin, /** * @cfg */ elementProperty : 'el', /** * @property */ pressed : false, init : function(component) { this.component = component; this.component.on('afterrender', this._onAfterrender, this); }, /** * @protected */ clearStyle : function() { this.el.setStyle({ background : '' }); }, /** * @protected * meant to be overriden */ getHoverStyle : function() { return { background : 'blue' }; }, /** * @protected * meant to be overriden */ getPressedStyle : function() { return { background : 'red' }; }, _onAfterrender : function() { this.el = this.component[this.elementProperty]; this.el.hover(this._onElementMouseIn, this._onElementMouseOut, this); this.el.on('click', this._onElementClick, this); }, _onElementMouseIn : function() { if(!this.pressed) { this.el.setStyle(this.getHoverStyle()); } }, _onElementMouseOut : function() { if(!this.pressed) { this.clearStyle(); } }, _onElementClick : function(e) { this.pressed = !this.pressed; if(this.pressed) { this.el.setStyle(this.getPressedStyle()); } else { //mimic mouse in if(e.within(this.el)) { this.el.setStyle(this.getHoverStyle()); } } } }); </code></pre> <p>Since you want something that works on more than just button this should work on any component.</p> <p>Here is an example usage:</p> <pre><code> var headerHoverColor = new Ext.form.field.Text({ fieldLabel : 'Header hover color', value : 'orange', renderTo : Ext.getBody() }); var headerPressedColor = new Ext.form.field.Text({ fieldLabel : 'Header pressed color', value : 'black', renderTo : Ext.getBody() }) new Ext.panel.Panel({ plugins : { ptype : 'dynamicStyleState', elementProperty : 'body' }, header : { plugins : { ptype : 'dynamicStyleState', getHoverStyle : function() { return { background : headerHoverColor.getValue() } }, getPressedStyle : function() { return { background : headerPressedColor.getValue() } } } }, height : 300, width : 300, renderTo : Ext.getBody(), title : 'Panel' }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload