やあ、
ボタンのさまざまな状態 (通常、ホバー、押された状態) の背景色を動的に変更する機能が必要です。
これまでに思いついたのは次のとおりです。 http://jsfiddle.net/suamikim/c3eHh/
Ext.onReady(function() {
function updateBackground() {
var theWin = win || this, // neccessary because of the call from the afterrender-event
btn = theWin.down('#btnTest'),
bckgr = theWin.down('#btnBckgr').getValue(),
bckgrHover = theWin.down('#btnBckgrHover').getValue(),
bckgrPressed = theWin.down('#btnBckgrPressed').getValue();
// Set normal background as button-style
// Should be ok
$('#' + btn.id).css('background', bckgr);
// Toggle background when hover-state changes
// Are there any downsides if this function gets called everytime the updateBackground-method is called? Do i have to dispose anything before binding the functions again?
$('#' + btn.id).hover(function() {
$('#' + btn.id).css('background', bckgrHover);
}, function() {
$('#' + btn.id).css('background', bckgr);
}
);
// Set the background for pressed button as document style
// Problems:
// - Pollutes the document-header if called multiple times...
// - Background does not change anymore on hover for pressed button because of the !important, but without important it wouldn't show the pressed background at all...
$('<style type="text/css"> #' + btn.id + '.x-btn-pressed { background: ' + bckgrPressed + ' !important } </style>').appendTo('head');
};
// Create a window with the propertygrid
var win = Ext.create('Ext.window.Window', {
width: 800,
height: 200,
layout: 'fit',
tbar: {
defaultType: 'textfield',
defaults: {
listeners: {
blur: updateBackground
}
},
items: [
'Background (CSS):', { itemId: 'btnBckgr', value: '#77ABD8' },
'Background hover (CSS):', { itemId: 'btnBckgrHover', value: '#CC1C1C' },
'Background pressed (CSS):', { itemId: 'btnBckgrPressed', value: '#7D4FC6' }
]
},
items: [{
xtype: 'button',
itemId: 'btnTest',
text: 'test button',
enableToggle: true
}],
listeners: {
'afterrender': updateBackground
}
}).show();
});
これは基本的に機能しますが、私にはいくつかの質問が残っています:
1)
updateBackground メソッドが呼び出されるたびに jquery のホバー関数を呼び出しても問題ありませんか?
jquery-doc ( http://api.jquery.com/hover/ ) によると、この関数は、指定された 2 つの関数を、指定された要素の mouse-enter- および mouse-leave-events にバインドします。
これは、呼び出すたびに新しい関数をバインドするか、既にバインドされている関数を更新するか、新しい関数をバインドする前に既にバインドされている関数を自動的に破棄することを意味しますか?つまり、新しい関数をバインドする
前に何かを破棄する必要がありますか? $.hover で機能しますか?
2)
押された状態のスタイルを document-style として設定しました。つまり、updateBackground 関数が頻繁に呼び出されると (テキスト フィールドの 1 つがテスト ケースでぼかしイベントを発生させるたびに)、ドキュメント ヘッダーが汚染されます。
同じ目標を達成するためのより良い方法はありますか、または新しいスタイルを追加する前に既に設定されているスタイルを削除できますか?
3)
押されたスタイルの !important-flag により、ボタンが押された場合、ホバー状態の背景は表示されません。押された状態の背景が正しく表示されないため、このフラグを削除することはできません...解決策はありますか?
一般に、この問題をまったく異なる方法で解決する方法についてのあらゆる提案に対して、私はオープンです。
ありがとう、
マイク
編集:
上記のコードは単なる例です。実際には、ボタン以外の ext-controls (パネル、チェックボックスなど) の background-attribute を変更できる必要がありますが、ボタンの実用的なソリューションを自分で他のコントロールに採用できると思います。
これを念頭に置いて、ボタンで答えを指定しすぎないようにしてください。できるだけ一般的なものにする必要があります...