履歴: プログラミング/Riot.js/bootstrap-carouselタグを実装
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 7 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2016/10/16 20:38:33 | プログラミング/Riot.js/bootstrap-carouselタグを実装 最新版 | 7,260 | +1 | -10 | 表示 | 表示中 | — |
| 2016/10/16 20:38:33 | プログラミング/Riot.js/bootstrap-carouselタグを実装 | 7,540 | +1 | -1 | 表示 | 差分 | 選択 |
| 2016/10/15 4:42:06 | プログラミング/Riot.js/bootstrap-carouselタグを実装 | 7,412 | +9 | -0 | 表示 | 差分 | 選択 |
| 2016/5/13 9:15:50 | プログラミング/Riot.js/bootstrap-carouselタグを実装 | 7,266 | +4 | -3 | 表示 | 差分 | 選択 |
| 2016/5/12 15:09:33 | プログラミング/Riot.js/bootstrap-carouselタグを実装 | 7,293 | +14 | -0 | 表示 | 差分 | 選択 |
| 2016/5/12 8:57:57 | プログラミング/Riot.js/bootstrap-carouselタグを実装 | 6,908 | +238 | -1 | 表示 | 差分 | 選択 |
| 2016/5/12 7:20:36 | プログラミング/Riot.js/bootstrap-carouselタグを実装 | 272 | — | — | 表示 | — | 基準 |
基準: プログラミング/Riot.js/bootstrap-carouselタグを実装 (2016/5/12 7:20:36) → 比較: プログラミング/Riot.js/bootstrap-carouselタグを実装 (2016/10/16 20:38:33) 最新版
@@ -1,5 +1,9 @@
[[公開メモ]]
+* 目次 [#ce63a3b6]
+
+#contents
+
* 概要 [#c7be18af]
Riot.js というライブラリがあって最近話題です。
@@ -7,8 +11,256 @@
- Riot.js http://riotjs.com/ja/
- Qiita の記事 http://qiita.com/search?q=riot
-これを使って bootstrap の carousel を実装するカスタムタグを作りました。
+これを使って bootstrap の carousel を簡単に実装するためのカスタムタグを作ってみました。
+https://gist.github.com/osamutake/f800cb0a7c638f058bded6702bc31356
+
+確かにすごく簡単で、便利に使えそうです。
+
* Bootstrap の carousel [#v03edf07]
Bootstrap を使うと、次のように画像が次々にスクロールする carousel を簡単に実装できます。
+
+&attachref(bootstrap-carousel.gif);
+
+実働サンプルはこちらです。
+
+http://jsbin.com/peqipi/2/edit?html,output
+
+* ただ簡単とはいえ [#f605dba1]
+
+普通にやると、この程度のコードを書く必要があります。
+
+ LANG:html(linenumber)
+ <div id="carousel-example-generic" class="carousel slide" data-ride="carousel" data-interval="1500">
+ <!-- Indicators -->
+ <ol class="carousel-indicators">
+ <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
+ <li data-target="#carousel-example-generic" data-slide-to="1"></li>
+ <li data-target="#carousel-example-generic" data-slide-to="2"></li>
+ </ol>
+
+ <!-- Wrapper for slides -->
+ <div class="carousel-inner">
+ <div class="item active">
+ <img src="http://placehold.it/800x400" alt="Caption #1">
+ <div class="carousel-caption">
+ <h2>Caption #1</h2>
+ </div>
+ </div>
+ <div class="item">
+ <img src="http://placehold.it/800x400" alt="Caption #2">
+ <div class="carousel-caption">
+ <h2>Caption #2</h2>
+ </div>
+ </div>
+ <div class="item">
+ <img src="http://placehold.it/800x400" alt="Caption #3">
+ <div class="carousel-caption">
+ <h2>Caption #3</h2>
+ </div>
+ </div>
+ </div>
+
+ <!-- Controls -->
+ <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
+ <span class="fa fa-angle-left"></span>
+ </a>
+ <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
+ <span class="fa fa-angle-right"></span>
+ </a>
+ </div>
+
+- 11-28 行目に画像とキャプションが羅列されています
+- 4-6 行目は中央の白丸にあたるもので、画像の数だけ並べます
+- 32-37 行目は手動でスクロールするための左右のコントロールにあたります
+
+画像を1枚追加したければ、11-28 行目と、4-6 行目を両方変えなければならず面倒ですし、
+何をやっているか見通しも悪いです。
+
+* Riot.js を使うと [#p50c77b1]
+
+上と同じ carousel を
+
+ LANG:html(linenumber)
+ <bootstrap-carousel interval="1500">
+ <img src="http://placehold.it/800x400" alt="Caption #1">
+ <img src="http://placehold.it/800x400" alt="Caption #2">
+ <img src="http://placehold.it/800x400" alt="Caption #3">
+ </bootstrap-carousel>
+
+と書けるようになります!
+
+画像の url と caption を列挙するだけ、
+これ以上シンプルにはならない形です。
+* Riot.js のカスタムタグ [#sdc5a684]
+
+ LANG:html
+ <bootstrap-carousel interval="1500"> ... </bootstrap-carousel>
+
+の部分がいわゆる「カスタムタグ」です。
+
+Riot.js は html ソースに埋め込まれたこのようなカスタムタグを、
+別に与えた変換規則を用いて html に変換してくれるライブラリです。
+
+** _innerHTML の利用 [#s46c6cec]
+
+よくある Riot.js のカスタムタグの利用例では、タグの中身を空にして、
+
+ LANG:html
+ <bootstrap-carousel interval="1500" />
+
+とか、
+
+ LANG:html
+ <bootstrap-carousel interval="1500"></bootstrap-carousel>
+
+のような形で使っています。
+
+しかし、ドキュメントにもあるとおり
+Riot.js は通常の html をカスタムタグで囲って使うことも許しています。
+
+ LANG:html
+ <bootstrap-carousel interval="1500">
+ <!-- 任意の html -->
+ </bootstrap-carousel>
+
+カスタムタグに囲われた innerHTML は通常 <yield /> を呼び出して利用します。
+
+ただその方法では、出力する html のどこかに、innerHTML をそのままの形で取り込むことしかできず、
+利用目的が限られてしまいます。
+
+実はこの内容には javascript からも this.root._innerHTML としてアクセス可能なため、
+下記コードではこれを利用しています。(innerHTML ではなく _innerHTML であることに注意して下さい)
+
+* カスタムタグの実装 [#rdb9cbb7]
+
+以下のコードを bootstrap-carousel.tag として用意し、
+riot.js で javascript へコンパイルして使います。
+
+実動サンプルはこちら: http://jsbin.com/qoduteguli/edit?html,output
+
+&attachref(bootstrap-carousel2.gif);
+
+ LANG:html(linenumber)
+ <bootstrap-carousel>
+
+ <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"
+ data-interval={ this.interval } data-pause={ this.pause }
+ data-wrap={ this.wrap } data-keyboard={ this.keyboard }>
+ <!-- Indicators -->
+ <ol class="carousel-indicators">
+ <li each={ this.items } data-target="#carousel-example-generic" data-slide-to={ index } class={ active: index==0 }></li>
+ </ol>
+
+ <!-- Wrapper for slides -->
+ <div class="carousel-inner">
+ <div each={ this.items } class={ item: true, active: index==0 }>
+ <img src={ src } alt={ caption }>
+ <div class="carousel-caption">
+ <h2>{ caption }</h2>
+ </div>
+ </div>
+ </div>
+
+ <!-- Controls -->
+ <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
+ <span class="fa fa-angle-left"></span>
+ </a>
+ <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
+ <span class="fa fa-angle-right"></span>
+ </a>
+ </div>
+
+ <style scoped>
+ .carousel-inner > .item {
+ text-align: center;
+ }
+ .carousel-inner > .item > img {
+ display: inline;
+ }
+ .carousel-inner .carousel-caption h2 {
+ color: rgba(0,0,0,0.6);
+ font-size: 40px;
+ text-shadow: 0px 0px 5px rgba(255, 255, 255, 0.6),
+ 0px 0px 10px rgba(255, 255, 255, 0.6);
+ }
+ .carousel-indicators li {
+ background-color: rgba(0,0,0,0.4);
+ box-shadow: 0 0 5px rgba(0,0,0,0.6);
+ border-color: rgba(255,255,255,0.8);
+ margin-left: 6px;
+ margin-right: 6px;
+ }
+ .carousel-indicators li.active {
+ border-color: rgba(255,255,255,0.8);
+ background-color: rgba(255,255,255,0.8);
+ margin-left: 6px;
+ margin-right: 6px;
+ }
+ img {
+ opacity: 0.8;
+ }
+ </style>
+
+ <script type="coffee">
+ @on 'update', ->
+ @interval = opts.interval ? 5000
+ @pause = opts.pause ? "hover"
+ @wrap = opts.wrap ? true
+ @keyboard = opts.keyboard ? true
+
+ @items = for img, i in $(@root._innerHTML) when img.tagName == 'IMG'
+ caption: img.alt
+ src: img.src
+ index: i
+ </script>
+ </bootstrap-carousel>
+
+- 3-28 行目が html ソース
+-- { } で囲まれた部分が javascript として解釈されて、結果に置き換えられる
+-- { value: expr } の部分は、expr の評価結果が true なら value に、さもなければ空になる
+-- each={ } のついたタグは与えられた配列と同じ数だけ繰り返される
+- 30-59 行目が css ソース
+-- scoped としておくと、カスタムタグの下の要素にのみ適用される
+-- type="scss" などを指定して、scss などで書くこともできる
+- 61-73 行目が javascript ソース
+-- 変換前に一回呼ばれる
+-- on('update', function(){ ... }); を呼ぶことで、値の更新時に処理を行える
+-- 基本は this.variable に値を設定して、javascript で { this.variable } を参照する形
+-- this.root._innerHTML でカスタムタグに囲まれた html ソースが得られる
+
+* コンパイルとマウント [#ff697abb]
+
+上記のようなタグの定義は Riot.js で javascript に変換して呼び出すか、
+riot.compile にソースを渡してやることでタグライブラリに取り込み、
+riot.mount でカスタムタグを含んだ html へ適用します。
+
+ LANG:javascript
+ riot.compile(tag_source);
+ riot.mount('*');
+
+* イベントやオブザーバブル、ルートなど [#a72f29a9]
+
+ここでは使いませんでしたが、Riot には他にもいろいろ機能があるようです。
+
+今後も徐々に使いながら慣れていこうと思います。
+
+* カスタムタグを TinyMCE editor で入力したい [#q89f512d]
+
+こちらで試し中:
+
+http://codepen.io/anon/pen/dMLXBy?editors=1010
+
++ custom_elements でタグを許可する
++ extended_valid_elements でプロパティを許可する
++ カスタムタグ入力用のメニューを作成する
+-- toolbar1 か toolbar2 に custom_tags というツールグループを作成して、
+-- setup で editor.addButton を読んでメニューを追加
+-- メニューの onclick で editor.iinsertContent する
++ 編集中にタグが見えるようにスタイルシートを追加する
+
+
+* 質問・コメント [#saad47c4]
+
+#article();