1:源碼
function FloatCouplet(leftElement, rightElement) {<br />if (arguments.length == 0) return;</p><p>lastScrollY=0;<br />function heartBeat(leftElement, rightElement){<br />return function() {<br />var diffY;<br />if (document.documentElement && document.documentElement.scrollTop) {<br />diffY = document.documentElement.scrollTop;<br />} else if (document.body) {<br />diffY = document.body.scrollTop;<br />} else {<br />/*Netscape stuff*/<br />}</p><p>percent = .1 * (diffY - lastScrollY);<br />percent = percent > 0 ? Math.ceil(percent) : Math.floor(percent);</p><p>if (leftElement) leftElement.style.top = parseInt(leftElement.style.top) + percent + "px";<br />if (rightElement) rightElement.style.top = parseInt(rightElement.style.top) + percent + "px";<br />lastScrollY = lastScrollY + percent;<br />}<br />}<br />window.setInterval(heartBeat(leftElement, rightElement), 1);<br />}
2:使用
new FloatCouplet($("#leftFloatE")[0], $("#rightFloatE")[0]);
3:測試
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><br /><title>符合標準的正常工作的對聯廣告</title><br /><mce:style type="text/css"><!--<br />html,body{height:1000px;}<br />--></mce:style><style type="text/css" mce_bogus="1">html,body{height:1000px;}</style><br /></head><br /><body><br /><div id="leftFloatE" style="left:2px;position:absolute;top:200px;border:5px solid #ccc;width:100px;height:200px;">ad1</div><br /><div id="rightFloatE" style="right:2px;position:absolute;top:200px;border:5px solid #ccc;width:100px;height:200px;">ad1</div><br /><div id="mm"><br /></div><br /><mce:script src="jquery.js" mce_src="jquery.js" type="text/javascript"></mce:script><br /><mce:script language="JavaScript" type="text/javascript"><!--<br />function FloatCouplet(leftElement, rightElement) {<br />if (arguments.length == 0) return;</p><p>lastScrollY=0;<br />function heartBeat(leftElement, rightElement){<br />return function() {<br />var diffY;<br />if (document.documentElement && document.documentElement.scrollTop) {<br />diffY = document.documentElement.scrollTop;<br />} else if (document.body) {<br />diffY = document.body.scrollTop;<br />} else {<br />/*Netscape stuff*/<br />}</p><p>percent = .1 * (diffY - lastScrollY);<br />percent = percent > 0 ? Math.ceil(percent) : Math.floor(percent);</p><p>if (leftElement) leftElement.style.top = parseInt(leftElement.style.top) + percent + "px";<br />if (rightElement) rightElement.style.top = parseInt(rightElement.style.top) + percent + "px";<br />lastScrollY = lastScrollY + percent;<br />}<br />}<br />window.setInterval(heartBeat(leftElement, rightElement), 1);<br />}<br />new FloatCouplet($("#leftFloatE")[0], $("#rightFloatE")[0]);<br />// --></mce:script><br /></body><br /></html>