javasctipt实用代码范例

1.右侧对联广告,随滚动条居顶一定距离;

window.onscroll = function (){
	var oWatermark = document.getElementById("divWatermark");
	oWatermark.style.top = document.documentElement.scrollTop+100+"px";
	//oWatermark.style.top = document.body.scrollTop+100+"px";
}

 

<div id="divWatermark" style="position:absolute; top:100px; right:0px; color:#ccc; widows:150px; height:150px; background-color:navy;">Watermark</div>

 

2.自定义addLoadEvent函数;

function addLoadEvent(func){
 var oldonload = window.onload;
 if(typeof window.onload != 'function'){
  window.onload = func;
 }else{
  window.onload = function(){
   oldonload();
   func();
  }
 }
}

调用方法:

addLoadEvent(函数名称);

 

3.取得控件的绝对位置

function getIE(e){   
var t=e.offsetTop;   
var l=e.offsetLeft;   
while(e=e.offsetParent){   
t+=e.offsetTop;   
l+=e.offsetLeft;   
}   
alert("top="+t+"/nleft="+l);

 

你可能感兴趣的:(java)