您的位置:首页 > Web前端 > JQuery

关于jQuery插件jTip的修改,代码基本上没变,只进行了简单的修改,我个人认为这样更合适!

2008-08-21 14:51 656 查看
这个jTip插件,我个人认为,有点不太好用,于是个人进行了简单的修改
修改内容如下:
1.由于jtip是将提示信息,用异步html代码方式返回的,所有的内容都写在了href上,我个人认为这样herf很臃肿。
同时,如果href里有?会出错。【也就是说,我有时候真是要写成这个样子<a href="**.aspx?id=**"></a>】报错是不允许的啊,所以就干脆把href里面的异步功能都放在了ref里。
[注意这里用的是rel,起初是想用title的,但是title也是提示,两个提示很难看,于是改成了rel]
2.样式问题,jTip的鼠标提示,把<a>的cursor改成了help,就是指针后面跟着一个?,这个有时候就根本不需要,所以我自己加了一个样式,只有使用
<a href="http://www.yahoo.com" rel="yahoo.htm?width=175" name="Before You Click..." id="yahooCopy" class="jTip">Go To Yahoo</a>使用这种,就是普通的链接变手型。
<a href="#" rel="thanks.htm?width=175" name="感谢JTip的开发者" id="kkai" class="jTip kkai"><font color="#CC6633" style="font-weight:bold;">not a link</font></a>使用这种,就是cuosor;help形状。
/*
* JTip
* By Cody Lindley (http://www.codylindley.com)
* 修改:kkai 2008-8-20
* 建议你有机会去研究下原来的jtip
* Under an Attribution, Share Alike License
* JTip is built on top of the very light weight jquery library.
*/
/*修改内容:
由于jtip是将提示信息,用异步html代码方式返回的,所有的内容都写在了href上,我个人认为这样herf很臃肿。
同时,如果href里有?会出错。
但是,我有时候真是要写成这个样子<a href="**.aspx?id=**"></a>,报错是不允许的啊,所以就干脆把href里面的异步功能都放在了ref里。
[注意这里用的是rel,起初是想用title的,但是title也是提示,两个提示很难看,于是改成了rel]
*/
//页面加载完成时候,调用JT_inint
$(document).ready(JT_init);

function JT_init(){
$("a.jTip")
.hover(function(){JT_show(this.rel,this.id,this.name)},function(){$('#JT').remove()})
//.click(function(){return false}); /*这里被kkai注释掉,因为我需要href连接提交*/
}

function JT_show(rel,linkId,title){
if(title == false)title=" ";
var de = document.documentElement;
var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
var hasArea = w - getAbsoluteLeft(linkId);
var clickElementy = getAbsoluteTop(linkId) - 3; //set y position

var queryString = rel.replace(/^[^/?]+/??/,'');
var params = parseQuery( queryString );
if(params['width'] === undefined){params['width'] = 250};
//if(params['link'] !== undefined){
/*这里被kkai注释掉,我不需要在rel里添加,我不需要onclick window.location*/
//$('#' + linkId).bind('click',function(){window.location = params['link']});
//$('#' + linkId).css('cursor','pointer');
//}

if(hasArea>((params['width']*1)+75)){
$("body").append("<div id='JT' style='width:"+params['width']*1+"px'><div id='JT_arrow_left'></div><div id='JT_close_left'>"+title+"</div><div id='JT_copy'><div class='JT_loader'><div></div></div>");//right side
var arrowOffset = getElementWidth(linkId) + 11;
var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
}else{
$("body").append("<div id='JT' style='width:"+params['width']*1+"px'><div id='JT_arrow_right' style='left:"+((params['width']*1)+1)+"px'></div><div id='JT_close_right'>"+title+"</div><div id='JT_copy'><div class='JT_loader'><div></div></div>");//left side
var clickElementx = getAbsoluteLeft(linkId) - ((params['width']*1) + 15); //set x position
}

$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
$('#JT').show();
$('#JT_copy').load(rel);

}

function getElementWidth(objectId) {
x = document.getElementById(objectId);
return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
// Get an object left position from the upper left viewport corner
o = document.getElementById(objectId)
oLeft = o.offsetLeft // Get left position from the parent object
while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
oParent = o.offsetParent // Get parent object reference
oLeft += oParent.offsetLeft // Add parent left position
o = oParent
}
return oLeft
}

function getAbsoluteTop(objectId) {
// Get an object top position from the upper left viewport corner
o = document.getElementById(objectId)
oTop = o.offsetTop // Get top position from the parent object
while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
oParent = o.offsetParent // Get parent object reference
oTop += oParent.offsetTop // Add parent top position
o = oParent
}
return oTop
}

function parseQuery ( query ) {
var Params = new Object ();
if ( ! query ) return Params; // return empty object
var Pairs = query.split(/[;&]/);
for ( var i = 0; i < Pairs.length; i++ ) {
var KeyVal = Pairs[i].split('=');
if ( ! KeyVal || KeyVal.length != 2 ) continue;
var key = unescape( KeyVal[0] );
var val = unescape( KeyVal[1] );
val = val.replace(//+/g, ' ');
Params[key] = val;
}
return Params;
}

function blockEvents(evt) {
if(evt.target){
evt.preventDefault();
}else{
evt.returnValue = false;
}
}

代码下载地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐