2014
Mar
11
Overview
- Render TV list data, set the cursor focus on the item, and go on the next when pressing the Enter key.
- Y.TVList doesn't deal with any style with CSS. Users have to handle on their own.
Getting Started
Step 1: git clone from github
git clone
- git clone git://github.com/lingihuang/tvlist.git
Step 2: Include YUI CSS and Javascript
Include YUI
- <link rel="stylesheet" href="http://yui.yahooapis.com/3.7.2/build/cssreset/reset-min.css">
- <link rel="stylesheet" href="http://yui.yahooapis.com/3.7.2/build/cssfonts/fonts-min.css">
- <script type="text/javascript" src="http://yui.yahooapis.com/3.7.2/build/yui/yui-min.js"></script>
Step 3: Include tvlist.js
Include tvlist.js
- <script type="text/javascript" src="tvlist.js"></script>
Samples
Render HTML With JavaScript
Step 1: Place a div in your html
HTML Markup
- <div id="list" class="list"></div>
Step 2: Write some CSS
CSS
- html, body {
- background: #000;
- font-family: "Hei W5", "DFHeiStd-W5", "DFYuanStd-W7", "微軟正黑體";
- }
- .clearfix {
- zoom: 1;
- }
- .clearfix:after {
- clear: both;
- content: "";
- display: block;
- }
- .list {
- height: 360px;
- overflow: hidden;
- width: 1280px;
- }
- .list .item {
- background: #cceeff;
- border: 4px solid #33ccff;
- color: #333;
- float: left;
- font-size: 197%;
- height: 82px;
- letter-spacing: 5px;
- line-height: 3.5;
- overflow: hidden;
- position: relative;
- text-align: center;
- width: 248px;
- }
- .list .active-status {
- background: #0088ff;
- border-color: #0000e3;
- color: #fff;
- }
Step 3: Write some JavaScript
JavaScript
- YUI().use("tvlist", function (Y) {
- var tvlist = new Y.TVList({
- node : "#list",
- rows : 4,
- columns : 5,
- activeIndex : 0,
- activeClass : "active-status",
- items: [
- {html: '<a class="item">Video 1</a>', data: {vid: 1, vname: "Video 1"}},
- {html: '<a class="item">Video 2</a>', data: {vid: 2, vname: "Video 2"}},
- {html: '<a class="item">Video 3</a>', data: {vid: 3, vname: "Video 3"}},
- {html: '<a class="item">Video 4</a>', data: {vid: 4, vname: "Video 4"}},
- {html: '<a class="item">Video 5</a>', data: {vid: 5, vname: "Video 5"}},
- {html: '<a class="item">Video 6</a>', data: {vid: 6, vname: "Video 6"}},
- {html: '<a class="item">Video 7</a>', data: {vid: 7, vname: "Video 7"}},
- {html: '<a class="item">Video 8</a>', data: {vid: 8, vname: "Video 8"}},
- {html: '<a class="item">Video 9</a>', data: {vid: 9, vname: "Video 9"}},
- {html: '<a class="item">Video 10</a>', data: {vid: 10, vname: "Video 10"}},
- {html: '<a class="item">Video 11</a>', data: {vid: 11, vname: "Video 11"}},
- {html: '<a class="item">Video 12</a>', data: {vid: 12, vname: "Video 12"}},
- {html: '<a class="item">Video 13</a>', data: {vid: 13, vname: "Video 13"}},
- {html: '<a class="item">Video 14</a>', data: {vid: 14, vname: "Video 14"}},
- {html: '<a class="item">Video 15</a>', data: {vid: 15, vname: "Video 15"}},
- {html: '<a class="item">Video 16</a>', data: {vid: 16, vname: "Video 16"}},
- {html: '<a class="item">Video 17</a>', data: {vid: 17, vname: "Video 17"}},
- {html: '<a class="item">Video 18</a>', data: {vid: 18, vname: "Video 18"}},
- {html: '<a class="item">Video 19</a>', data: {vid: 19, vname: "Video 19"}},
- {html: '<a class="item">Video 20</a>', data: {vid: 20, vname: "Video 20"}},
- {html: '<a class="item">Video 21</a>', data: {vid: 21, vname: "Video 21"}},
- {html: '<a class="item">Video 22</a>', data: {vid: 22, vname: "Video 22"}},
- {html: '<a class="item">Video 23</a>', data: {vid: 23, vname: "Video 23"}},
- {html: '<a class="item">Video 24</a>', data: {vid: 24, vname: "Video 24"}},
- {html: '<a class="item">Video 25</a>', data: {vid: 25, vname: "Video 25"}},
- {html: '<a class="item">Video 26</a>', data: {vid: 26, vname: "Video 26"}}
- ]
- });
- tvlist.on("move", function (e) {
- Y.log("activeIndex = " + e.activeIndex);
- });
- tvlist.on("enter", function (e) {
- Y.log(e.data);
- Y.log("offset = " + e.offset);
- });
- });
Step 4: Output HTML
Render HTML With JavaScript
- <div id="list" class="list">
- <div class="row clearfix">
- <a class="item active-status">Video 1</a>
- <a class="item">Video 2</a>
- <a class="item">Video 3</a>
- <a class="item">Video 4</a>
- <a class="item">Video 5</a>
- </div>
- <div class="row clearfix">
- <a class="item">Video 6</a>
- <a class="item">Video 7</a>
- <a class="item">Video 8</a>
- <a class="item">Video 9</a>
- <a class="item">Video 10</a>
- </div>
- <div class="row clearfix">
- <a class="item">Video 11</a>
- <a class="item">Video 12</a>
- <a class="item">Video 13</a>
- <a class="item">Video 14</a>
- <a class="item">Video 15</a>
- </div>
- <div class="row clearfix">
- <a class="item">Video 16</a>
- <a class="item">Video 17</a>
- <a class="item">Video 18</a>
- <a class="item">Video 19</a>
- <a class="item">Video 20</a>
- </div>
- <div class="row clearfix">
- <a class="item">Video 21</a>
- <a class="item">Video 22</a>
- <a class="item">Video 23</a>
- <a class="item">Video 24</a>
- <a class="item">Video 25</a>
- </div>
- <div class="row clearfix">
- <a class="item">Video 26</a>
- </div>
- </div>
Parse Data Attributes
Step 1: Prepare data with attribute data-xxx
HTML Markup
- <div id="list" class="list">
- <div class="row clearfix">
- <a class="item" data-vid="1" data-vname="Video 1">Video 1</a>
- <a class="item" data-vid="2" data-vname="Video 2">Video 2</a>
- <a class="item" data-vid="3" data-vname="Video 3">Video 3</a>
- <a class="item" data-vid="4" data-vname="Video 4">Video 4</a>
- <a class="item" data-vid="5" data-vname="Video 5">Video 5</a>
- </div>
- <div class="row clearfix">
- <a class="item" data-vid="6" data-vname="Video 6">Video 6</a>
- <a class="item" data-vid="7" data-vname="Video 7">Video 7</a>
- <a class="item" data-vid="8" data-vname="Video 8">Video 8</a>
- <a class="item" data-vid="9" data-vname="Video 9">Video 9</a>
- <a class="item" data-vid="10" data-vname="Video 10">Video 10</a>
- </div>
- <div class="row clearfix">
- <a class="item" data-vid="11" data-vname="Video 11">Video 11</a>
- <a class="item" data-vid="12" data-vname="Video 12">Video 12</a>
- <a class="item" data-vid="13" data-vname="Video 13">Video 13</a>
- <a class="item" data-vid="14" data-vname="Video 14">Video 14</a>
- <a class="item" data-vid="15" data-vname="Video 15">Video 15</a>
- </div>
- <div class="row clearfix">
- <a class="item" data-vid="16" data-vname="Video 16">Video 16</a>
- <a class="item" data-vid="17" data-vname="Video 17">Video 17</a>
- <a class="item" data-vid="18" data-vname="Video 18">Video 18</a>
- <a class="item" data-vid="19" data-vname="Video 19">Video 19</a>
- <a class="item" data-vid="20" data-vname="Video 20">Video 20</a>
- </div>
- <div class="row clearfix">
- <a class="item" data-vid="21" data-vname="Video 21">Video 21</a>
- <a class="item" data-vid="22" data-vname="Video 22">Video 22</a>
- <a class="item" data-vid="23" data-vname="Video 23">Video 23</a>
- <a class="item" data-vid="24" data-vname="Video 24">Video 24</a>
- <a class="item" data-vid="25" data-vname="Video 25">Video 25</a>
- </div>
- <div class="row clearfix">
- <a class="item" data-vid="26" data-vname="Video 26">Video 26</a>
- </div>
- </div>
Step 2: Write some CSS
CSS
- .clearfix {
- zoom: 1;
- }
- .clearfix:after {
- clear: both;
- content: "";
- display: block;
- }
- .list {
- height: 360px;
- overflow: hidden;
- width: 1280px;
- }
- .list .item {
- background: #cceeff;
- border: 4px solid #33ccff;
- color: #333;
- float: left;
- font-size: 197%;
- height: 82px;
- letter-spacing: 5px;
- line-height: 3.5;
- overflow: hidden;
- position: relative;
- text-align: center;
- width: 248px;
- }
- .list .active-status {
- background: #0088ff;
- border-color: #0000e3;
- color: #fff;
- }
Step 3: Write some JavaScript
JavaScript
- YUI().use("tvlist", function (Y) {
- var tvlist = new Y.TVList({
- node : "#list",
- rows : 4,
- columns : 5,
- activeIndex : 0,
- activeClass : "active-status"
- });
- tvlist.on("move", function (e) {
- Y.log("activeIndex = " + e.activeIndex);
- });
- tvlist.on("enter", function (e) {
- Y.log(e.data);
- Y.log("offset = " + e.offset);
- });
- });
Sinle Row or Single Column
Horizontal Row
Vertical Column
Multiple Lists
Disable other lists when switching the list.
Documentation
Y.TVList Config
Parameter | Type | Required | Default Value | Description | Example |
---|---|---|---|---|---|
activeClass | String | No | focused | Classname of the focused item. | 5 |
activeIndex | Number | No | 0 | Index of the focused item. | 2 |
columns | Number | No | 4 | How many items per row contains. | 5 |
disabled | Boolean | No | false | Disable or enable events active. | true |
items | Array | No | [] | An array to store html and data of all items. | [{html: "", data: {key: value}}] |
node | String or Y.Node | Yes | null | "#list" or Y.one("#list") | 5 |
rows | Number | No | 4 | The amount of rows | 5 |
Y.TVList Attributes
Parameter | Description | Example |
---|---|---|
activeClass | Set index of the item active, and re-render all items. | tvlist.set("activeIndex", 1); |
disabled | Disable or enable events active. When you set disabled true, your list node will add classname tvlist-disabled. | tvlist.set("disabled", true); |
Y.TVList Events
Example
- YUI().use("tvlist", function (Y) {
- var tvlist = new Y.TVList({
- node : "#list",
- rows : 4,
- columns : 5,
- activeIndex : 0,
- activeClass : "active-status"
- });
- tvlist.on("move", function (e) {
- Y.log("activeIndex = " + e.activeIndex); // Index of the active item in data array.
- });
- tvlist.on("enter", function (e) {
- Y.log(e.data);
- Y.log("offset = " + e.offset); // Index of the active item in the viewable viewport.
- });
- });
Y.TVList Methods
Example
- var tvlist = new Y.TVList({
- node : "#list",
- rows : 4,
- columns : 5,
- activeIndex : 0,
- activeClass : "active-status"
- });
- tvlist.set("disabled", true);
回應 (Leave a comment)