js实现跟随鼠标移动且带关闭功能的图片广告实例
时间:2021-07-15 09:30:35|栏目:JavaScript代码|点击: 次
本文实例讲述了js实现跟随鼠标移动且带关闭功能的图片广告。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:
<!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>跟随鼠标移动且带关闭功能的图片广告</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
</head>
<body>
<!--把下面代码加到<body>与</body>之间-->
<script type="text/javascript">
//<![CDATA[
function badAD(html){
var ad=document.body.appendChild(document.createElement('div'));
ad.style.cssText="border:1px solid #000;background:#FFF;position:absolute;padding:24px 4px 4px 4px;font: 12px/1.5 verdana;";
ad.innerHTML=html||'This is bad idea!';
var c=ad.appendChild(document.createElement('span'));
c.innerHTML="×";
c.style.cssText="position:absolute;right:2px;top:2px;cursor:pointer";
c.onclick=function (){
document.onmousemove=null;
this.parentNode.style.left='-99999px'
};
document.onmousemove=function (e){
e=e||window.event;
var x=e.clientX,y=e.clientY;
setTimeout(function() {
if(ad.hover)return;
ad.style.left=x+5+'px';
ad.style.top=y+5+'px';
},120)
}
ad.onmouseover=function (){
this.hover=true
};
ad.onmouseout=function (){
this.hover=false
}
}
badAD('<img src="/images/m02.jpg">')
//]]>
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>跟随鼠标移动且带关闭功能的图片广告</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
</head>
<body>
<!--把下面代码加到<body>与</body>之间-->
<script type="text/javascript">
//<![CDATA[
function badAD(html){
var ad=document.body.appendChild(document.createElement('div'));
ad.style.cssText="border:1px solid #000;background:#FFF;position:absolute;padding:24px 4px 4px 4px;font: 12px/1.5 verdana;";
ad.innerHTML=html||'This is bad idea!';
var c=ad.appendChild(document.createElement('span'));
c.innerHTML="×";
c.style.cssText="position:absolute;right:2px;top:2px;cursor:pointer";
c.onclick=function (){
document.onmousemove=null;
this.parentNode.style.left='-99999px'
};
document.onmousemove=function (e){
e=e||window.event;
var x=e.clientX,y=e.clientY;
setTimeout(function() {
if(ad.hover)return;
ad.style.left=x+5+'px';
ad.style.top=y+5+'px';
},120)
}
ad.onmouseover=function (){
this.hover=true
};
ad.onmouseout=function (){
this.hover=false
}
}
badAD('<img src="/images/m02.jpg">')
//]]>
</script>
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。
上一篇:ajax与jsonp的区别及用法
栏 目:JavaScript代码
下一篇:JavaScript 节流函数 Throttle 详解
本文地址:http://www.codeinn.net/misctech/157495.html