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

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);

2013年1月17日 星期四

[]jQuery] 解決 change select 在IE 不能執行.

因為Change 的event , 在IE 會不能順利執行, 所以只好改成下列方式.
$('#container').bind($.browser.msie ? 'click' : 'change', function(event) { /* test event.type and event.target * to capture only select control changes */ })

2012年9月18日 星期二

[jquery] $.ajax 不使用async: false 來接收return result

使用$.ajax 時, 若是要return result , 看來好像只能使用 async: false (同步化) 的參數設定, 但是這樣的設定會讓執行ajax 時, 若是ajax 還未返回時, 會讓整個頁面停住.
若是不是用這種設定時, 可以用以下方式達成.

function get(url, callback) { $.get(url).success(function(data) { callback(data); }); } get("...", function(result) { // use `result` which you normally got by assigning the return value // of `get` to a variable });

2012年6月18日 星期一

[jquery] 解決flash 覆蓋 div 的問題 , z-index 無效

解決flash與div 一起使用時, flash 會蓋在div 上面的問題.
因為這問題用css: z-index 也無法解決.
用下列的jquery與法可以一次解決
var objs=$('object').not('object param[name="wmode"][value="transparent"]'); var appended = $(' '); objs.prepend(appended); // 所有的 object 都塞進去 $('object').each(function(){ $(this).append($('<param name="wmode" value="transparent"></param>')); });

2012年6月11日 星期一

[jquery] 輸入欄位預設文字

以下是很簡單的輸入欄位預設文字的jquery 語法
稍微改良了一下, 以解決Browser reload 的bug :
From : if ($(this).val() == "")
To : if ($(this).val() == "" || $(this).val() == $(this)[0].title)
來源:http://www.dailycoding.com/Posts/default_text_fields_using_simple_jquery_trick.aspx <script type=text/javascript> $(document).ready(function() { $(".defaultText").focus(function(srcc) { if ($(this).val() == $(this)[0].title) { $(this).removeClass("defaultTextActive"); $(this).val(""); } }); $(".defaultText").blur(function() { if ($(this).val() == "" || $(this).val() == $(this)[0].title) { $(this).addClass("defaultTextActive"); $(this).val($(this)[0].title); } }); $(".defaultText").blur(); }); </script> <style type="text/css"> .defaultText { width: 299px; } .defaultTextActive { color: #a1a1a1; font-style: italic; } </style> <input type="text" title="enter email" size="30" name="email" id="user_email" class="defaultText login-table-form defaultTextActive">

2012年3月23日 星期五

[引用]手機網頁-文字自動縮放,等比縮放,文字百分比

引用來源: http://ucmic.blogspot.com/2012/03/blog-post.html 
手機眾多裝置的螢幕顯示大小不同,會因為排版時區塊及圖片可用百分比達到顯示等比縮放,但唯獨文字沒辦法設定依照顯示的百分比去做縮放。


就因為這樣設計排版上,主要視覺區塊內的文字,大小設計剛好在區塊內,以不同顯示比例去看,區塊及圖片會因為設定成百分比而依照螢幕大小縮放,但文字確保持在您設定的大小,這就會產生區塊有自動縮放大小,文字卻沒有自動縮放大小,使得你設計的版面跑位或不是你所要呈現的設計樣子。

國外網站在這裡提供,一套解決方法使用jquery 外掛 fittext.js,簡單的應用,就可達到文字的自動依照您的顯示大小做縮放。

以下為簡單的套用方式:關於更多請至 fittext.js

2011年12月22日 星期四

[jquery] 解決 IE 瀏覽器不能使用 change , 與 option 不能 hide()

IE瀏覽器一直以來對於Select 的 change 一直是有問題的.
試過了網路上的一些方法, 結果最後只能用這種方法來解決是最恰當的,
 就是 select 上加 onchange , 如此一來才能完全模擬出 $.change 的效果.

 另外 IE 很賤的是 select 下的 option 不能去 hide() , 只能 remove() 或是 attr('disabled',true) . 

所以看來只能以此種方式處理, 勉強差強人意. 在其他瀏覽器正常的,
我只能說IE 你真是好樣的.

Html:
<select onchange="type_7_change(this)" id="type_7" name="type_7"> <option value="">請選擇...</option> <option value="69">A-執行費用</option> <option value="71">C-委外費用</option> <option value="72">D-贈品費用</option> <option value="73">E-其他費用</option> </select> Javascript :
function type_7_change(self){ var $select_option = $(self).find('option:selected'); if ($select_option.val() != '' ) { var str = $select_option.text().substr(0,1); if (str) { $('#type_8 option:eq(0)').attr('selected',true); $('#type_8 option').each(function(i,j){ if ($(j).val() != '') { $(j).show(); if ($.browser.msie) $(j).attr('disabled',false); else $(j).show(); if ($(j).text().substr(0,1) != str) { if ($.browser.msie) $(j).attr('disabled',true); else $(j).hide(); } } }); } } }

2011年9月23日 星期五

[引用] 用Jquery 做 Ajax 上一頁下一頁.

來源: 黑暗執行緒

但很不幸地,IE7(或IE8/9開相容模式)對location.hash特性支援不夠完整,hash改變時並不會在瀏覽歷史中產生記錄,因此需要透過加入隱藏式iframe等特殊手法來克服,自己處理跨瀏覽器議題太辛苦,有個很好用的jQuery外掛BBQ(不是中秋烤肉巴比Q,是Back Button & Query)可以讓我們輕鬆寫出跨瀏覽器版的AJAX回上頁功能。

<!DOCTYPE html> <html> <head> <title>AJAX GoBack</title> <script src="Scripts/jquery-1.6.3.js" type="text/javascript"> </script> <script src="Scripts/jquery.ba-bbq.js" type="text/javascript"> </script> <script> $(function () { $("#s1").show(); $("input.nav-btn").click(function () { var $btn = $(this); $btn.parent().hide(); var nav = $btn.data("nav"); $("#" + nav).show(); //將目前的步驟加註在location.hash $.bbq.pushState({ step: nav }); }); //hash變化時觸發hashchange事件 $(window).bind('hashchange', function (e) { //由hash取出step參數,決定要顯示哪一個div var s = e.getState("step") || "s1"; if (!$("#" + s + ":visible").length) { $("#main > div").hide(); $("#" + s).show(); } }); }); </script> <style> #main div { width: 300px; height: 200px; display: none; padding: 10px; } #s1 { background-color: #ff7777; } #s2 { background-color: #77ff77; } #s3 { background-color: #7777ff; } </style> </head> <body> <div id="main"> <div id="s1"> STEP1 <input type="button" class="nav-btn" value="Next" data-nav="s2" /> </div> <div id="s2"> STEP2 <input type="button" class="nav-btn" value="Next" data-nav="s3" /> </div> <div id="s3"> FINAL <input type="button" value="Submit" /> </div> </div> </body> </html>

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

2010年12月28日 星期二

[jquery]Bind

Bind 用法:
1.綁定並且alert()出, mouse x 與 y 的值

$(document).ready(function() {
$('#foo').bind('click', function(event) {
alert('The mouse cursor is at ('
+ event.pageX + ', ' + event.pageY + ')');
});
});


2.還可以一次放入兩個event 去 toggleClass.

$('#foo').bind('mouseenter mouseleave', function() {
$(this).toggleClass('entered');
});


3. jQuery 1.4 之後可以這樣用

$('#foo').bind({
click: function() {
// do something on click
},
mouseenter: function() {
// do something on mouseenter
}
});


4. event.preventDefault() 取消原本event的動作

$("form").bind("submit", function(event) {
event.preventDefault();
});


5. event.stopPropagation() 停止原本event的動作和冒泡(bubbling)動作

$("form").bind("submit", function(event) {
event.stopPropagation();
});

2010年11月26日 星期五

[jquery] 技巧

triggerHandler-觸發handler

var refreshFrame = function() {
$('#frame').load('http://reddit.com');
};
$('.button').click(refreshFrame);
refreshFrame();

//可以改成;

$('.button').click(function() {
$('#frame').load('/page/frame.html');
}).triggerHandler('click');

// You can also use a shortcut
$('.button').click(function() {
$('#frame').load('/page/frame.html');
}).click();



Custom events - 客製化事件.

$('.button').click(function() {
$('div#frame').load('/page/frame.html', function(){
$(this).triggerHandler('frameLoaded');
});
});
// PluginA.js
$('#frame').bind('frameLoaded', function(){
$(this).show('slide', {direction: 'top'});
});
// PluginB.js
$('div').bind('frameLoaded', function(){
// do something else
});

2010年9月3日 星期五

[jquery] $.isFunction , $.getScript 運用

$.isFunction
$.getScript

if ($.isFunction(jQuery.fn.autocomplete)){/* use official jQuery UI autocomplete */

$('#aj_au').autocomplete({

source: '/ajax_user.php?rt=j',

minLength: 2

});

} else {/* use jQuery plugins autocomplete */

$.getScript('/jslib/jquery/plugins/jquery.autocomplete.js', function() {

$('#aj_au').autocomplete('/ajax_user.php?rt=t', {

width: 150,

minChars: 2

});

});

}

2010年7月30日 星期五

jquery 筆記. tooltip 圖片.

tooltip + 預載圖片 + 限制寬高比例.
另外還有 tooltip 浮動圖片 偵測 Left 與 Top ;

$('a.tooltip').mouseover(function(e){
var img = new Image(); //預載圖片
img.src = $(this).attr('href');
var div = ['<div id="tooltip_img" style="display:none;z-index:100;position:absolute;border:2px solid #666; padding:3px;background-color:#eee;"><img src='+$(this).attr('href')+' /></div>'].join('');
$('body').append(div);
var _w = img.width ;
var _h = img.height;
var maxW = 250; //預設圖片最大寬度
var maxH = 300; //預設圖片最大高度
var HeightWidth = _h / _w; //設置高寬比
var WidthHeight = _w / _h; //設置寬高比
if (_w > maxW ) {
_w = maxW ;
_h = maxW * HeightWidth ;
}
if (_h > maxH ) {
_w = maxH * WidthHeight;
_h = maxH ;
}
$('#tooltip_img img').attr('width',_w);
$('#tooltip_img img').attr('height',_h);
var left = e.pageX+20;
var top = e.pageY+20;
v = viewport();
// 偵測水平
if (v.x + v.cx < left+_w ) {
left -= _w+40 ;
}
// 偵測垂直
if (v.y + v.cy < top + _h ) {
top -= _h+40 ;
}
$('#tooltip_img').css({'top':top+'px','left':left+'px'}).show();
}).mouseout(function(e){
$('#tooltip_img').remove();
});
// 取得瀏覽器 寬高
function viewport() {
return {
x: $(window).scrollLeft(), //scroll Bar
y: $(window).scrollTop(), //Sscroll Bar
cx: $(window).width(), //螢幕寬高
cy: $(window).height() //螢幕寬高
};
}

2010年6月18日 星期五

JQuery CSS Reset Plugin

出處
$(document).ready(function(){
$("YourElement").beResetCSS();
});
可以針對Widget 做個別的 CSS Reset.


/*
* beResetCSS 0.1 - jQuery plugin
* written by Benjamin Mock
* http://benjaminmock.de/jquery-css-reset-plugin/
*
* Copyright (c) 2009 Benjamin Mock (http://benjaminmock.de)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Built for jQuery library
* http://jquery.com
*
*/

(function($) {
$.fn.beResetCSS = function(){
resetStyles(this);
return this;
};

function resetStyles( element ) {
var tagName = $(element)[0].tagName.toLowerCase();

var elements = new Array();
var styles = new Array();

elements[0] = [ 'html', 'body', 'div', 'span', 'applet', 'object', 'iframe',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'blockquote', 'pre',
'a', 'abbr', 'acronym', 'address', 'big', 'cite', 'code',
'del', 'dfn', 'em', 'font', 'img', 'ins', 'kbd', 'q', 's', 'samp',
'small', 'strike', 'strong', 'sub', 'sup', 'tt', 'var',
'b', 'u', 'i', 'center',
'dl', 'dt', 'dd', 'ol', 'ul', 'li',
'fieldset', 'form', 'label', 'legend',
'table', 'caption', 'tbody', 'tfoot', 'thead', 'tr', 'th', 'td'];

var s ={
margin: '0',
padding: '0',
border: '0',
outline: '0',
fontSize: '100%',
verticalAlign: 'baseline',
background: 'transparent'
};
styles[0] = s;

elements[1] = ['body'];
s = {
lineHeight: '1'
};
styles[1] = s;

elements[2] = ['ol', 'ul'];
s = {
listStyle: 'none'
}
styles[2] = s;

elements[3] = ['blockquote', 'q'];
s = {
quotes: 'none'
}
styles[3] = s;

elements[4] = ['ins'];
s = {
textDecoration: 'none'
}
styles[4] = s;

elements[5] = ['del'];
s = {
textDecoration: 'line-through'
}
styles[5] = s;

elements[6] = ['table'];
s = {
borderCollapse: 'collapse',
borderSpacing: '0'
}
styles[6] = s;

// resetting styles
$(elements).each(function(i){
$(this).each(function(k){
if(tagName == this){
addStyles(element, styles[i]);
}
});
});
}

function addStyles( element, styles ) {
for(key in styles){
$(element).css(key, styles[key]);
}
}

})(jQuery);

2010年6月7日 星期一

[jquery] 解決Div 內 onmoussout 的元素 觸發問題.

若是使用 onmouseout 會造成 滑鼠移到 div 內的元素也觸發 onmouseout ~
通常不符合我們的操作行為. 用 jquery 的 mouseleave 就對啦!


//html範例:
<div id="test" >
<ul>
<li>123</li>
<li>123</li>
<li>123</li>
<li>123</li>
</ul>
</div>
//jscript
$('#test').bind("mouseleave",function(e){
$(this).hide()
});

2010年3月5日 星期五

jquery 截斷超出字並加上省略號

這plugin 還不錯, 若是能加上 中英文寬度判別應該就更完美了.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cookie</title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<style>
*{ margin:0; padding:0;font-family:"宋體",Arial, Helvetica, sans-serif;}
#best{ width:300px; height:200px; border:1px solid #ccc; margin:60px auto 0; line-height:1.6; font-size:14px; padding:10px 0 0 10px}
.blank{ font-size:18px; font-weight:bold; text-align:center; padding:20px}
</style>
<script type="text/javascript">
jQuery.fn.limit=function(){
        var self = $("[@limit]");
        self.each(function(){
                var objString = $(this).text();
                var objLength = $(this).text().length;
                var num = $(this).attr("limit");
                if(objLength > num){
                                                $(this).attr("title",objString);
                        objString = $(this).text(objString.substring(0,num) + "...");
                }
        })
}
$(function(){
        $("[@limit]").limit();
})
</script>
</head>
<body>
        <div class="blank">請按F5刷新頁面。。。。</div>
<div id="best">
        <div limit="12">計算字串的長度長度長度長度</div>
        <div limit="10">這邊有優化很公開這邊</div>
        <div limit="12">這邊有優化很公開長度長度很公開長度</div>
        <div limit="12">計算字長度長度</div>
        <div limit="10">這邊有優化很邊有優化很邊有優化很邊有優化很邊有優化很</div>
</div>

</body>
</html>


加強版有過濾中英文

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cookie</title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<style>
*{ margin:0; padding:0;font-family:"宋體",Arial, Helvetica, sans-serif;}
#best{ width:300px; height:200px; border:1px solid #ccc; margin:60px auto 0; line-height:1.6; font-size:14px; padding:10px 0 0 10px}
.blank{ font-size:18px; font-weight:bold; text-align:center; padding:20px}
</style>
<script type="text/javascript">
String.prototype.mylength = function(){
var arr = this.match(/[\u00FF-\uFFFF]/gi);
if(!arr || arr==null)
return this.length;
var len = this.length + arr.length;
return len;
}
String.prototype.mysubstring = function(limit){
var len = 0;
var arr = [];
for(var i=0;i<this.length;i++){
var it = this.substring(i,i+1);
len += it.mylength();
if(len > limit)
break;
arr[arr.length] = it;
}

var result = arr.join("");
return result;
}

jQuery.fn.limit = function(){
$(this).each(function(){
var mylimit = parseInt($(this).attr("limit"));
var applied = $(this).attr("applied");
if(applied)
return;
var text = $(this).text();
var original_text = text;
if(text.mylength() > mylimit){
text = text.mysubstring(mylimit);
text += "…";
text = "<SPAN style=\"position:relative\"><SPAN class='tooltip'>" + original_text + "</SPAN>" + text + "</SPAN>";
$(this).html(text);
$(this).mouseover(function(){
$(this).find(".tooltip").css("display","");
});
$(this).mouseout(function(){
$(this).find(".tooltip").css("display","none");
});
$(this).find(".tooltip")
.css({ 'display':'block',
'position':'absolute',
'top':'2em',
'left':'2em',
'width':'15em',
'border':'1px solid #0cf',
'background-color':'#cff',
'color':'#000',
'text-align':'center',
'word-wrap':'break-word',
'word-break':'break-all'})
.css("display","none");
$(this).attr("applied",true);
}
});
}

$(function(){
$("[limit]").limit();
});
</script>
</head>
<body>
<div class="blank">請按F5刷新頁面。。。。</div>
<div id="best">
<div limit="7">abcd計算字串的長度長度長度長度</div>
<div limit="10">這邊有優化很公開這邊</div>
<div limit="12">這邊有優化很公開長度長度很公開長度</div>
<div limit="12">計算字長度長度</div>
<div limit="10">這邊有優化很邊有優化很邊有優化很邊有優化很邊有優化很</div>
</div>
</body>
</html>

2010年3月2日 星期二

[jquery]提示窗口



// JavaScript Document
(function($) {
$.fn.extend({
Show: function(widht,height) {
var TopY=0;//初始化元素距父元素的距離
$(this).css("width",widht+"px").css("height",height+"px");//設置消息框的大小
$(this).slideDown(1000);//彈出
//$("#messageTool").css("margin-top",-height);//為內容部分創建高度 溢出
$("#message_close",window.parent.frames["main"].document).click(function() {//當點擊關閉按鈕的時候
if(TopY==0)
{
$("#message",window.parent.frames["main"].document).slideUp(1000);//這裡之所以用slideUp是為了兼用Firefox瀏覽器
}
else
{
$("#message",window.parent.frames["main"].document).animate({top: TopY+height}, "slow", function() { $("#message").hide(); });//當TopY不等於0時 ie下和Firefox效果一樣
}
});
$(window).scroll(function() {
$("#message",window.parent.frames["main"].document).css("top", $(window).scrollTop() + $(window).height() - $("#message",window.parent.frames["main"].document).height());//當滾動條滾動的時候始終在屏幕的右下角
TopY=$("#message",window.parent.frames["main"].document).offset().top;//當滾動條滾動的時候隨時設置元素距父原素距離
});
}
})}
)(jQuery);

function ShowMess(MessContent,target)
{
var tit="您有未處理的任務:"; //提示標題
MessContent=MessContent.replace(/\,/g,"<br/>").replace(/white/g,"red");
CreateDiv(tit,MessContent,target);
$("#message",window.parent.frames[target].document).Show(220,150);

}

function CreateDiv(title,content,target)
{
var Div="<div id=\"message\" style=\"z-index:100;position:absolute;display:none;bottom:0; right:0; overflow:hidden;border:1px solid #6699cc\"><div id=\"backimage\"></div><div style=\"width:100%;height:25px;overflow:hidden;background:#6699cc\" id=\"messageTool\"><div style=\"padding:3px 0 0 0px;width:150px;line-height:18px;text-align:center;overflow:hidden;position:absolute;font-size:12px;color:#FFFFFF\" id=\"msgtitle\"><img src=\"/SysImage/radio.gif\" /> "+title+"</div><span id=\"message_close\" style=\"right:10px;width:16px;padding-top:5px;text-align:center;cursor:pointer;position:absolute;\">×</span><div style=\"clear:both;\"></div></div><div id=\"message_content\" style=\"height:150px;text-align:left;overflow:hidden;background:#CFD9F8\"><div style=\"padding:5px;font-size:12px\">"+content+"</div></div></div>";
var Obj = window.parent.frames[target].document.createElement("div");
Obj.innerHTML=Div;
window.parent.frames[target].document.body.appendChild(Obj);
}

//調用方法即可:

ShowMess(message,"main"); //message:需要顯示的信息,main需要顯示的窗口.

2010年2月6日 星期六

[jquery] tooltip


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple JQuery Tooltips turtorial</title>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

$(document).ready(function() {

//Select all anchor tag with rel set to tooltip
$('a[rel=tooltip]').mouseover(function(e) {

//Grab the title attribute's value and assign it to a variable
var tip = $(this).attr('title');

//Remove the title attribute's to avoid the native tooltip from the browser
$(this).attr('title','');

//Append the tooltip template and its value
$(this).append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');

//Set the X and Y axis of the tooltip
$('#tooltip').css('top', e.pageY + 10 );
$('#tooltip').css('left', e.pageX + 20 );

//Show the tooltip with faceIn effect
$('#tooltip').fadeIn('500');
$('#tooltip').fadeTo('10',0.8);

}).mousemove(function(e) {

//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
$('#tooltip').css('top', e.pageY + 10 );
$('#tooltip').css('left', e.pageX + 20 );

}).mouseout(function() {

//Put back the title attribute's value
$(this).attr('title',$('.tipBody').html());

//Remove the appended tooltip template
$(this).children('div#tooltip').remove();

});

});

</script>

<style>
body {font-family:arial;font-size:12px;text-align:center;}
div#paragraph {width:300px;margin:0 auto;text-align:left}
a {color:#aaa;text-decoration:none;cursor:pointer;cursor:hand}
a:hover {color:#000;text-decoration:none;}

/* Tooltip */

#tooltip {
position:absolute;
z-index:9999;
color:#fff;
font-size:10px;
width:180px;

}

#tooltip .tipHeader {
height:8px;
background:url(images/tipHeader.gif) no-repeat;
}

/* IE hack */
*html #tooltip .tipHeader {margin-bottom:-6px;}

#tooltip .tipBody {
background-color:#000;
padding:5px;
}

#tooltip .tipFooter {
height:8px;
background:url(images/tipFooter.gif) no-repeat;
}

</style>

</head>

<body>

<div id="paragraph">
A paragraph typically consists of a unifying main point, thought, or idea accompanied by supporting details. The non-fiction paragraph usually begins with the general and moves towards the more specific so as to advance an argument or point of view. Each <a rel="tooltip" title="A paragraph typically consists of a unifying main point, thought, or idea accompanied by <b>supporting details</b>">paragraph</a> builds on what came before and lays the ground for what comes next. Paragraphs generally range three to seven sentences all combined in a single paragraphed statement. In prose fiction successive details, for example; but it is just as common for the point of a prose paragraph to occur in the middle or the end. A paragraph can be as short as one word or run the length of multiple pages, and may consist of one or many <a rel="tooltip" title="Testing of Sentences">sentences</a>. When dialogue is being quoted in fiction, a new paragraph is used each time the person being quoted changed.
</div>

</body>
</html>