HOME > > > > >

jQuery.cssHooks

独自のCSSプロパティと効果を設定する。設定したCSSは.css()で簡単に取得、設定できる。 全容は不明w

サンプル

myCubeという独自のCSSプロパティを設定。CSSの設定だけだと.addClassでいいじゃねーかってなるので、クリックイベントも備える。

実行結果


test
test
test

test

test

test


Javascript

  1. jQuery.cssHooks.myCube = {
  2.     get: function(elem, computed, extra) {
  3.         var bg = jQuery(elem).css('backgroundColor');
  4.         var height = jQuery(elem).css('height');
  5.         var width = jQuery(elem).css('width');
  6.         return '[bg]' + bg + '[height]' + height + '[width]' + width + '[computed]' + computed + '[extra]' + extra;
  7.     },
  8.  
  9.     set: function(elem, value) {
  10.         jQuery(elem).css({
  11.             display: 'inline-block',
  12.             width: 100,
  13.             height: 100,
  14.             margin: 5,
  15.             background: value
  16.         }).toggle(function(){
  17.             jQuery(this).width(200).height(200);
  18.         }, function(){
  19.             jQuery(this).width(100).height(100);
  20.         });
  21.     }
  22. };
  23.  
  24. $('.jqTest div').css('myCube', '#CCFFFF');
  25. $('.jqTest p').css('myCube', '#009900');
  26.  
  27. $('.jqTest .execute').click(function(){
  28.     $('.jqTest').append(function(){
  29.         var str = '';
  30.  
  31.         $('.jqTest p').each(function(){
  32.             str += $(this).css('myCube') + '<br />';
  33.         });
  34.         return str;
  35.     });
  36. });

HTML

  1. <div class="jqTest">
  2.     <input type="button" value="execute" class="execute cq-btn m-primary" /><br />
  3.  
  4.     <div>test</div>
  5.     <div>test</div>
  6.     <div>test</div>
  7.  
  8.     <p>test</p>
  9.     <p>test</p>
  10.     <p>test</p>
  11.     <hr />
  12. </div>

.cssHooksを利用すると、1つのCSSプロパティで複数のことが設定できるから便利な気がするけど、同じことするのは全然大変じゃない。

たぶん.cssHooksを使いたくなる場面ってのがあるんだと思う。