顯示具有 jquery plugin 標籤的文章。 顯示所有文章
顯示具有 jquery plugin 標籤的文章。 顯示所有文章

2014年12月22日 星期一

[jquery] stupidtable.js

來欣賞一下別人寫的 jquery plugin : Stupidtable

蠻簡單的, 很容易理解. 

(function($) { $.fn.stupidtable = function(sortFns) { return this.each(function() { var $table = $(this); sortFns = sortFns || {}; // Merge sort functions with some default sort functions. sortFns = $.extend({}, $.fn.stupidtable.default_sort_fns, sortFns); // ==================================================== // // Begin execution! // // ==================================================== // // Do sorting when THs are clicked $table.on("click.stupidtable", "thead th", function() { var $this = $(this); var th_index = 0; var dir = $.fn.stupidtable.dir; // Account for colspans $this.parents("tr").find("th").slice(0, $this.index()).each(function() { var cols = $(this).attr("colspan") || 1; th_index += parseInt(cols,10); }); // Determine (and/or reverse) sorting direction, default `asc` var sort_dir = $this.data("sort-default") || dir.ASC; if ($this.data("sort-dir")) sort_dir = $this.data("sort-dir") === dir.ASC ? dir.DESC : dir.ASC; // Choose appropriate sorting function. var type = $this.data("sort") || null; // Prevent sorting if no type defined if (type === null) { return; } // Trigger `beforetablesort` event that calling scripts can hook into; // pass parameters for sorted column index and sorting direction $table.trigger("beforetablesort", {column: th_index, direction: sort_dir}); // More reliable method of forcing a redraw $table.css("display"); // Run sorting asynchronously on a timout to force browser redraw after // `beforetablesort` callback. Also avoids locking up the browser too much. setTimeout(function() { // Gather the elements for this column var column = []; var sortMethod = sortFns[type]; var trs = $table.children("tbody").children("tr"); // Extract the data for the column that needs to be sorted and pair it up // with the TR itself into a tuple trs.each(function(index,tr) { var $e = $(tr).children().eq(th_index); var sort_val = $e.data("sort-value"); var order_by = typeof(sort_val) !== "undefined" ? sort_val : $e.text(); column.push([order_by, tr]); }); // Sort by the data-order-by value column.sort(function(a, b) { return sortMethod(a[0], b[0]); }); if (sort_dir != dir.ASC) column.reverse(); // Replace the content of tbody with the sorted rows. Strangely (and // conveniently!) enough, .append accomplishes this for us. trs = $.map(column, function(kv) { return kv[1]; }); $table.children("tbody").append(trs); // Reset siblings $table.find("th").data("sort-dir", null).removeClass("sorting-desc sorting-asc"); $this.data("sort-dir", sort_dir).addClass("sorting-"+sort_dir); // Trigger `aftertablesort` event. Similar to `beforetablesort` $table.trigger("aftertablesort", {column: th_index, direction: sort_dir}); // More reliable method of forcing a redraw $table.css("display"); }, 10); }); }); }; // Enum containing sorting directions $.fn.stupidtable.dir = {ASC: "asc", DESC: "desc"}; $.fn.stupidtable.default_sort_fns = { "int": function(a, b) { return parseInt(a, 10) - parseInt(b, 10); }, "float": function(a, b) { return parseFloat(a) - parseFloat(b); }, "string": function(a, b) { return a.localeCompare(b); }, "string-ins": function(a, b) { a = a.toLocaleLowerCase(); b = b.toLocaleLowerCase(); return a.localeCompare(b); } }; })(jQuery);

2011年5月24日 星期二

jQuery Plugin insertContent-在文本框游標位置插入內容並選中

在輸入框中插入一些符號或是元件的plugin.
值得收藏.


使用方式:
$(文本域選擇器).insertContent("插入的內容");
$(文本域選擇器).insertContent("插入的內容",數值);
//根據數值選中插入文本內容兩邊的邊界

來源網址
Demo:http://www.css88.com/demo/insertContent/

/**
* Created by 愚人碼頭 .
* User: 愚人碼頭
* Date: 11-5-19
* Time: 上午10:24
* 更多查看http://www.css88.com/archives/3627
*/
//在光標位置插入內容
(function($) {
$.fn.extend({
insertContent: function(myValue, t) {
var $t = $(this)[0];
if (document.selection) { //ie
this.focus();
var sel = document.selection.createRange();
sel.text = myValue;
this.focus();
sel.moveStart('character', -l);
var wee = sel.text.length;
if (arguments.length == 2) {
var l = $t.value.length;
sel.moveEnd("character", wee + t);
t < = 0 ? sel.moveStart("character", wee - 2 * t - myValue.length) : sel.moveStart("character", wee - t - myValue.length);

sel.select();
}
} else if ($t.selectionStart || $t.selectionStart == '0') {
var startPos = $t.selectionStart;
var endPos = $t.selectionEnd;
var scrollTop = $t.scrollTop;
$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
this.focus();
$t.selectionStart = startPos + myValue.length;
$t.selectionEnd = startPos + myValue.length;
$t.scrollTop = scrollTop;
if (arguments.length == 2) {
$t.setSelectionRange(startPos - t, $t.selectionEnd + t);
this.focus();
}
}
else {
this.value += myValue;
this.focus();
}
}
})
})(jQuery);

jQuery Masonry Plugin - 版型工具

應用網站: http://pinterest.com/

Masonry 官網

由下圖即可看出, 使用 Masonry 的差異了.


jQuery Masonry參數:來源


$('#wrapper').masonry({

singleMode: false,
// 禁用測量每個浮動元素的寬度。
// 如果浮動元素具有相同的寬度,設置為true。
// 默認: false

columnWidth: 240,
// 1列網格的寬度,單位為像素(px)。
// 默認: 第一個浮動元素外寬度。

itemSelector: '.box:visible',
// 附加選擇器,用來指定哪些元素包裹的元素會重新排列。

resizeable: true,
// 綁定一個 Masonry 訪問 用來 窗口 resize時佈局平滑流動.
// 默認:true

animate: true,
// 佈局重排動畫。
// 默認:false

animationOptions: {},
// 一對動畫選項,具體參數可以參考jquery .animate()中的options選項,見:http://www.css88.com/jqapi/#p=animate

appendedContent: $('.new_content'),
// 附加選擇器元素,用來尾部追加內容。
// 在集成infinitescroll插件的情況下使用。

saveOptions: true,
// 默認情況下,Masonry 將使用以前Masonry使用過的參數選項,所以你只需要輸入一次選項
// 默認:true

}, function() {}
// 可選擇的回調函數
// 'this'將指向重排的Masonry適用元素
);

2011年4月28日 星期四

[jquery] $.temp 遞歸用法

$.temp 是一個很好用的 template plugin
不過通常做 template 會需要用到遞歸方式, 所以只好用下列函數來傳回html code.


function get_template (type , data ){
    $.template( "template", $('#tmp_'+type).html());   
    return $("<div />").append($.tmpl( "template",data)).html();
}


Plugin : http://api.jquery.com/jquery.tmpl