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

用JS做的小事例,鼠标点击出现东西!!

2016-04-25 21:53 453 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  onclick="createImg(event)"xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">

<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>网页标题</title>
<meta name="keywords" content="关键字列表" />
<meta name="description" content="网页描述" />
<link rel="stylesheet" type="text/css" href="" />
<style type="text/css"></style>
<script type="text/javascript">

function createImg(e){
//创建照片节点
var img_node=document.createElement("img");
//追加到父节点中、
document.body.appendChild(img_node)
//添加照片路径
img_node.src="images/xingxing.gif";

//设置宽度高度
var img_width =getRandom(85,25);
var img_width_half =img_width/2;
var p_x=e.clientX- img_width_half;
var p_y=e.clientY- img_width_half;
//这里必须用css样式
img_node.style.width=img_width+"px";
img_node.style.position ="absolute";
img_node.style.top=p_y+"px";
img_node.style.left=p_x+"px";
//不能这个方法删除图片

// img_node.onclick=removeImg(this);
//创建删除图片的节点
img_node.setAttribute("onclick","removeImg(this)");
}
//创建一个随机函数
function getRandom(max,min){
 
return Math.floor(Math.random()*(max-min)+min);
 
 
}
//删除星星
function removeImg(obj){
document.body.removeChild(obj);
 
 
}

</script>

</head>

<body bgcolor="#000">

</body>
</html>

效果图如下:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: