四处乱逛,总会有收获,今天介绍一个很好玩的东西。
首先声明出处,今天的代码我是从似乎有知识 | 在路上这个Blog上偷来的。
这是一段Javascript代码,它提供了一个浮动窗口,效果如图。这个窗口可以用鼠标拖动,可以单击右上角关闭。

Caffey将这个窗口用于展示“历史的今天”,开始,我以为这是某个类似Wiki网站提供的服务,后来发现不是,窗口中显示的内容和字体是静态写在脚本中的,因此,我觉得如果用作Blog的临时通知,好像更酷。
当然,首先要做的是把代码复制到公告栏中,全部代码如下:
<script language="javascript">
var IE5=(document.getElementById && document.all)? true : false;
var W3C=(document.getElementById)? true: false;
var currIDb=null, currIDs=null, xoff=0, yoff=0; zctr=0; totz=0;
function trackmouse(evt){
if((currIDb!=null) && (currIDs!=null)){
var x=(IE5)? event.clientX+document.body.scrollLeft : evt.pageX;
var y=(IE5)? event.clientY+document.body.scrollTop : evt.pageY;
currIDb.style.left=x+xoff+'px';
currIDs.style.left=x+xoff+10+'px';
currIDb.style.top=y+yoff+'px';
currIDs.style.top=y+yoff+10+'px';
return false;
}}
function stopdrag(){
currIDb=null;
currIDs=null;
NS6bugfix();
}
function grab_id(evt){
xoff=parseInt(this.IDb.style.left)-((IE5)? event.clientX+document.body.scrollLeft : evt.pageX);
yoff=parseInt(this.IDb.style.top)-((IE5)? event.clientY+document.body.scrollTop : evt.pageY);
currIDb=this.IDb;
currIDs=this.IDs;
}
function NS6bugfix(){
if(!IE5){
self.resizeBy(0,1);
self.resizeBy(0,-1);
}}
function incrzindex(){
zctr=zctr+2;
this.subb.style.zIndex=zctr;
this.subs.style.zIndex=zctr-1;
}
function createPopup(id, title, width, height, x , y , isdraggable, boxcolor, barcolor, shadowcolor, text, textcolor, textptsize, textfamily ){
if(W3C){
zctr+=2;
totz=zctr;
var txt='';
txt+='<div id="'+id+'_s" style="position:absolute; left:'+(x+10)+'px; top:'+(y+10)+'px; width:'+width+'px; height:'+height+'px; background-color:'+shadowcolor+'; filter:alpha(opacity=50); visibility:visible"> </div>';
txt+='<div id="'+id+'_b" style="border:outset '+barcolor+' 2px; position:absolute; left:'+x+'px; top:'+y+'px; width:'+width+'px; overflow:hidden; height:'+height+'px; background-color:'+boxcolor+'; visibility:visible">';
txt+='<div style="width:'+width+'px; height:16px; background-color:'+barcolor+'; padding:0px; border:1px"><table cellpadding="0" cellspacing="0" border="0" width="'+(IE5? width-4 : width)+'"><tr><td width="'+(width-20)+'"><div id="'+id+'_h" style="width:'+(width-20)+'px; height:14px; font: bold 12px sans-serif; color:'+textcolor+'"> '+title+'</div></td><td align="right"><a onmousedown="document.getElementById(\''+id+'_s\').style.display=\'none\'; document.getElementById(\''+id+'_b\').style.display=\'none\';return false"><img src="close.gif" border="0" height="15" width="15"></a></td></tr></table></div>';
txt+='<div id="'+id+'_ov" width:'+width+'px; style="margin:2px; color:'+textcolor+'; font:'+textptsize+'pt '+textfamily+';">'+text+'</div></div>';
document.write(txt);
this.IDh=document.getElementById(id+'_h');
this.IDh.IDb=document.getElementById(id+'_b');
this.IDh.IDs=document.getElementById(id+'_s');
this.IDh.IDb.subs=this.IDh.IDs;
this.IDh.IDb.subb=this.IDh.IDb;
this.IDh.IDb.IDov=document.getElementById(id+'_ov');
if(IE5){
this.IDh.IDb.IDov.style.width=width-6;
this.IDh.IDb.IDov.style.height=height-22;
this.IDh.IDb.IDov.style.scrollbarBaseColor=boxcolor;
this.IDh.IDb.IDov.style.overflow="auto";
}else{
this.IDh.IDs.style.MozOpacity=.5;
}
this.IDh.IDb.onmousedown=incrzindex;
if(isdraggable){
this.IDh.onmousedown=grab_id;
this.IDh.onmouseup=stopdrag;
}}}
if(W3C)document.onmousemove=trackmouse;
if(!IE5 && W3C)window.onload=NS6bugfix;
createPopup( 'box2', '历史上的今天 点击右上角关闭', 288, 90, 28, 200, true, '333399' , '333333' , '000000' , '1915年,五四时期著名的革命刊物《新青年》在上海创刊。' , 'c0c0c0' , 11, '宋体');
</script>
这些代码中最关键的是creatPopup()这一行。
'历史上的今天 点击右上角关闭'是浮动窗口的标题文字,你可以根据自己的需要替换。
'333399' , '333333' , '000000' 分别是,窗口的背景颜色,窗口标题的背景颜色、窗口阴影颜色,可以跟据需要替换。
'1915年,五四时期著名的革命刊物《新青年》在上海创刊。' , 'c0c0c0'分别是窗口的字体和字体颜色,可替换。
下面,给出一个我改动后的例子:

这个时候,公告栏代码中CreatePopup这一行是这样的。
createPopup( 'box2', '号外!号外! 点击右上角关闭', 288, 90, 28, 200, true, 'f0eee1' ,'d1c8b9', '000000' , '去火星出差,Blog暂停更新一年,请见谅!' , '000000' , 11, '宋体');
此主题的相关链接:
《Donews Blog的非官方使用指北》标准操作系列
《Donews Blog的小花招》系列
Trackback: http://tb.donews.net/TrackBack.aspx?PostId=595626