参考YiiBootstrap先

大部分示例来自上游项目Yii-Bootstrap, 只是做了部分问题的修复并给库增加了新特性。

如何使用你的插件

把你的js插件放到js目录下 然后用Yii::app()->bootstrap->registerAssetJs("yourjsfile.js", CClientScript::POS_HEAD); 注册到你的应用中即可。

注意事项

重要! 一些文档完全来自Twitter's Bootstrap 我们认为一些组件值得在这里重复一遍,主要是transitions, tooltips 和 affix 插件。 你最好再读一下Twitter's Bootstrap的超牛逼文档 。 这会有助于理解YiiBooster + Boostrap的工作原理。

当然,中文文档在这里http://bootcss.com/

About transitions good info even if its not part of any component

如果使用压缩后的 bootstrap.js你不就用再包含其他js文件了。 For simple transition effects, include bootstrap-transition.js once alongside the other JS files. If you're using the compiled (or minified) bootstrap.js, there is no need to include this—it's already there.

Use cases

A few examples of the transition plugin:

  • Sliding or fading in modals模态窗口显隐效果
  • Fading out tabs标签页淡出
  • Fading out alerts弹出提醒框淡出
  • Sliding carousel panes幻灯片切换效果

Examples

With TbModal

YiiBootstrap came with a great widget to render modals.

Click me
<?php $this->beginWidget('bootstrap.widgets.TbModal', array('id'=>'myModal')); ?>

    <div class="modal-header">
        <a class="close" data-dismiss="modal">×</a>
    <h4>Modal header</h4>
    </div>

<div class="modal-body">
    <p>One fine body...</p>
</div>

<div class="modal-footer">
	<?php $this->widget('bootstrap.widgets.TbButton', array(
	'type'=>'primary',
	'label'=>'Save changes',
	'url'=>'#',
	'htmlOptions'=>array('data-dismiss'=>'modal'),
)); ?>
	<?php $this->widget('bootstrap.widgets.TbButton', array(
	'label'=>'Close',
	'url'=>'#',
	'htmlOptions'=>array('data-dismiss'=>'modal'),
)); ?>
</div>
<?php $this->endWidget(); ?>
	<?php $this->widget('bootstrap.widgets.TbButton', array(
	'label'=>'Click me',
	'type'=>'primary',
	'htmlOptions'=>array(
		'data-toggle'=>'modal',
		'data-target'=>'#myModal',
	),
)); ?>
                

New Approach

Nevertheless, there is a new kid on the block, so we have include it as standard on our library. Check the following examples to see how easy is now:

$this->widget('bootstrap.widgets.TbButton', array(
  'label'=>'Click me',
  'type'=>'primary',
  'htmlOptions'=>array(
	  'onclick'=>'js:bootbox.alert("hello world")'
  ),
));
                

Note: We could obviously attach the javascript calls on document load. We attach the events to the onclick for the sake of the examples

<?php
	$this->widget('bootstrap.widgets.TbButton', array(
		'label'=>'Confirm Modal',
		'type'=>'warning',
		'htmlOptions'=>array(
			'onclick'=>'js:bootbox.confirm("Are you sure?",
			function(confirmed){console.log("Confirmed: "+confirmed);})'
		),
	));
?>>
<?php
$this->widget('bootstrap.widgets.TbButton', array(
	'label'=>'Prompt Modal',
	'type'=>'success',
	'htmlOptions'=>array(
		'style'=>'margin-left:3px',
		'onclick'=>'js:bootbox.prompt("What is your name?",
			function(result){console.log("Result: "+result);})'
	),
));
?>
<?php
$this->widget('bootstrap.widgets.TbButton', array(
	'label'=>'Override Alert & Confirm Icons Modal',
	'type'=>'primary',
	'htmlOptions'=>array(
		'style'=>'margin-left:3px',
		'onclick'=>'js:(function(){
		    bootbox.setIcons({
		        "OK"      : "icon-ok icon-white",
		        "CANCEL"  : "icon-ban-circle",
		        "CONFIRM" : "icon-ok-sign icon-white"
		    });

		    bootbox.confirm("This dialog invokes <b>bootbox.setIcons()</b> to set icons for the standard three labels of OK, CANCEL and CONFIRM, before calling a normal <b>bootbox.confirm</b>", function(result) {
		        bootbox.alert("This dialog is just a standard <b>bootbox.alert()</b>. <b>bootbox.setIcons()</b> only needs to be set once to affect all subsequent calls", function() {
		            bootbox.setIcons(null);
		        });
		    });
  })();'
	),
));
?>
<?php
$this->widget('bootstrap.widgets.TbButton', array(
	'label'=>'Prompt Modal',
	'htmlOptions'=>array(
		'style'=>'margin-left:3px',
		'onclick'=>'js:bootbox.modal("<img src=\"http://placehold.it/600x400\"/>", "Modal popup!");'
	),
));
?>
                

Example tabs

We didn't play with the placement property . Let's do it now.

Top Placement

Home Content
Profile Content
Messages Content

Bottom Placement

Home Content
Profile Content
Messages Content

Left Placement

Home Content
Profile Content
Messages Content

Right Placement

Home Content
Profile Content
Messages Content

Examples from Twitter Booster docs -no need to rewrite same info

Inspired by the excellent jQuery.tipsy plugin written by Jason Frame; Tooltips are an updated version, which don't rely on images, use CSS3 for animations, and data-attributes for local title storage.

Hover over the links below to see tooltips:

Tight pants next level keffiyeh you probably haven't heard of them. Photo booth beard raw denim letterpress vegan messenger bag stumptown. Farm-to-table seitan, mcsweeney's fixie sustainable quinoa 8-bit american apparel have a terry richardson vinyl chambray. Beard stumptown, cardigans banh mi lomo thundercats. Tofu biodiesel williamsburg marfa, four loko mcsweeney's cleanse vegan chambray. A really ironic artisan whatever keytar, scenester farm-to-table banksy Austin twitter handle freegan cred raw denim single-origin coffee viral.

Four directions


Usage

Trigger the tooltip via JavaScript:

$('#example').tooltip(options)

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-animation="".

Name type default description
animation boolean true apply a css fade transition to the tooltip
html boolean true Insert html into the tooltip. If false, jquery's text method will be used to insert content into the dom. Use text if you're worried about XSS attacks.
placement string|function 'top' how to position the tooltip - top | bottom | left | right
selector string false If a selector is provided, tooltip objects will be delegated to the specified targets.
title string | function '' default title value if `title` tag isn't present
trigger string 'hover' how tooltip is triggered - click | hover | focus | manual
delay number | object 0

delay showing and hiding the tooltip (ms) - does not apply to manual trigger type

If a number is supplied, delay is applied to both hide/show

Object structure is: delay: { show: 500, hide: 100 }

Heads up! Options for individual tooltips can alternatively be specified through the use of data attributes.

Markup

For performance reasons, the Tooltip and Popover data-apis are opt in. If you would like to use them just specify a selector option.

<a href="#" rel="tooltip" title="first tooltip">hover over me</a>

Methods

$().tooltip(options)

Attaches a tooltip handler to an element collection.

.tooltip('show')

Reveals an element's tooltip.

$('#element').tooltip('show')

.tooltip('hide')

Hides an element's tooltip.

$('#element').tooltip('hide')

.tooltip('toggle')

Toggles an element's tooltip.

$('#element').tooltip('toggle')

.tooltip('destroy')

Hides and destroys an element's tooltip.

$('#element').tooltip('destroy')

The extended tooltip is when you wish to provide your IDE with some helper icons to display at your form fields or label headers, which information tooltip is saved at the database. The extended tooltip provides also an edit in place tooltip mechanism so you could edit the help tooltips whilst on development and not allow its edition on production. It is as simple as to set its editable atttribute.

Important the database table where tooltips are stored is automatically created by the widget. No need for actions.

Examples

Hover over the links below to see tooltips:

Hey, I have an help icon on a side, and you can edit it!
Hey, I have a help icon and you cannot edit it!

Hey, I have an help icon on a side, and you can edit it! <?php $this->widget('bootstrap.widgets.TbExtendedTooltip', array('key' => 'test.tooltip', 'url'=>url('site/tooltip')));?>
Hey, I have a help icon and you cannot edit it! <?php $this->widget('bootstrap.widgets.TbExtendedTooltip', array('key' => 'test.tooltip2', 'editable'=>false));?>
                

Examples

Static popover

Four options are available: top, right, bottom, and left aligned.

Popover bottom

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

Popover right

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

Popover top

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

Popover left

Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.

No markup shown as popovers are generated from JavaScript and content within a data attribute.

Live demo

 <?php $this->widget('bootstrap.widgets.TbButton', array(
	'label'=>'Top popover',
	'type'=>'primary',
	'htmlOptions'=>array('data-title'=>'A Title','data-placement'=>'top', 'data-content'=>'And here\'s some amazing content. It\'s very engaging. right?', 'rel'=>'popover'),
)); ?> 
<?php $this->widget('bootstrap.widgets.TbButton', array(
	'label'=>'Left popover',
	'type'=>'danger',
	'htmlOptions'=>array('data-title'=>'A Title','data-placement'=>'left', 'data-content'=>'And here\'s some amazing content. It\'s very engaging. right?', 'rel'=>'popover'),
)); ?> 
<?php $this->widget('bootstrap.widgets.TbButton', array(
	'label'=>'Right popover',
	'type'=>'warning',
	'htmlOptions'=>array('data-title'=>'A Title','data-placement'=>'right', 'data-content'=>'And here\'s some amazing content. It\'s very engaging. right?', 'rel'=>'popover'),
)); ?> 
<?php $this->widget('bootstrap.widgets.TbButton', array(
	'label'=>'Bottom popover',
	'type'=>'success',
	'htmlOptions'=>array('data-title'=>'A Title','data-placement'=>'bottom', 'data-content'=>'And here\'s some amazing content. It\'s very engaging. right?', 'rel'=>'popover'),
)); ?>
                
Note The above as you can see is just a Bootstrap button with a meta-tag configuration. Please, check Twitter popover documentation for more information about its options.

Example accordion

Using the TbCollapse widget, we built a simple accordion style widget:

Anim pariatur cliche...
Anim pariatur cliche...
<div class="accordion" id="accordion2">
<?php $collapse = $this->beginWidget('bootstrap.widgets.TbCollapse');?>
<div class="accordion-group">
    <div class="accordion-heading">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
            Collapsible Group Item #1
        </a>
    </div>
    <div id="collapseOne" class="accordion-body collapse in">
        <div class="accordion-inner">
            Anim pariatur cliche...
        </div>
    </div>
</div>
<div class="accordion-group">
  <div class="accordion-heading">
      <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
          Collapsible Group Item #2
      </a>
  </div>
  <div id="collapseTwo" class="accordion-body collapse">
      <div class="accordion-inner">
          Anim pariatur cliche...
      </div>
  </div>
</div>
<?php $this->endWidget();?>
...
                

We came across an interesting plugin created by Mattias Larentis: Bootstrap toggle buttons. As it is a plugin that obviously comes into the bootstrap family, we have create a widget to include it in our library.

Note The widget can be also used as a form element (see form section).

Example

$this->widget('bootstrap.widgets.TbToggleButton', array(
	'onChange' => 'js:function($el, status, e){console.log($el, status, e);}'
));

$this->widget('bootstrap.widgets.TbToggleButton', array(
    'name'=>'testToggleButtonB',
    'enabledLabel' => 'LABEL-ON',
    'disabledLabel' => 'LABEL-OFF',
    'value'=>true,
    'width'=>200,
    'enabledStyle'=>null,
    'customEnabledStyle'=>array(
        'background'=>'#FF00FF',
        'gradient'=>'#D300D3',
        'color'=>'#FFFFFF'
    ),
	'customDisabledStyle'=>array(
        'background'=> "#FFAA00",
        'gradient'=> "#DD9900",
        'color'=> "#333333"
    )
));
                

Example

A basic, easily extended plugin for quickly creating elegant typeaheads with any form text input.

$this->widget('bootstrap.widgets.TbTypeahead', array(
'options'=>array(
	'name'=>'demo-typeahead',
	'source'=>array(
		'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
		'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia',
		'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa',
		'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland',
		'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri',
		'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
		'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio',
		'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina',
		'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
		'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'),
	'items'=>4,
	'matcher'=>"js:function(item) {
        return ~item.toLowerCase().indexOf(this.query.toLowerCase());
    }",
)));
                

Example extracted from Twitter Documentation

The subnavigation on the left is a live demo of the affix plugin.


Usage

Via data attributes

To easily add affix behavior to any element, just add data-spy="affix" to the element you want to spy on. Then use offsets to define when to toggle the pinning of an element on and off.

<div data-spy="affix" data-offset-top="200">...</div>
Heads up! You must manage the position of a pinned element and the behavior of its immediate parent. Position is controlled by affix, affix-top, and affix-bottom. Remember to check for a potentially collapsed parent when the affix kicks in as it's removing content from the normal flow of the page.

Via JavaScript

Call the affix plugin via JavaScript:

$('#navbar').affix()

Methods

.affix('refresh')

When using affix in conjunction with adding or removing of elements from the DOM, you'll want to call the refresh method:

$('[data-spy="affix"]').each(function () {
  $(this).affix('refresh')
});
                

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-offset-top="200".

Name type default description
offset number | function | object 10 Pixels to offset from screen when calculating position of scroll. If a single number is provide, the offset will be applied in both top and left directions. To listen for a single direction, or multiple unique offsets, just provided an object offset: { x: 10 }. Use a function when you need to dynamically provide an offset (useful for some responsive designs).