DIV和Image 的一些JavaScript 操作

<html>
<head>
<title>JavaScript</title>
<style type="text/css">
#pic {
	position:absolute;
	left:0px;
	top:80px;
	width:120px;
	height:120px;
	z-index:1;
	border: thin dashed #CC00FF;
}
#select {
	position:absolute;
	left:0px;
	top:80px;
	width:40px;
	height:40px;
	z-index:2;
	border: thin dashed #CC00FF;
}
#display {
	position:absolute;
	left:120px;
	top:80px;
	width:600px;
	height:600px;
	z-index:1;
	border: thin dashed #CC00FF;
	background-repeat: no-repeat;
}
</style>
<script language="javascript" type="text/javascript" src="jquery-1.4.1.js"></script>
<script language="javascript" type="text/javascript">
var multiples = 1;
var path;

$("document").ready(function() {
    $("#select").bind("mousedown", 
    function(event) {
        mouseDown(event);
    });
    $("#file").bind("change", 
    function(event) {
        addImage(this);
    });
});
//添加图片
function addImage(obj) {
    if (obj) {
        if (window.navigator.userAgent.indexOf("MSIE") >= 1)
        {
            path = obj.value;
        }
        else if (window.navigator.userAgent.indexOf("Firefox") >= 1)
        {
            path = obj.files[0].getAsDataURL();
        }
    }
    var disImage = new Image();
    disImage.src = path;
	disImage = editImage(disImage);
	$("#pic").empty();
	$("#pic").get(0).appendChild(disImage);
	$("#display").css("background-image","url(" + path+ ")");
}
//设置图片
function editImage(image){
	for(multiples = 1;image.width>=120 || image.height>=120;multiples ++){
		image.width = image.width * 0.8;
		image.height = image.height * 0.8;
	}
	return image;
}
//移动图片
function moveImage(newX,newY){
	newX = (0- newX)*12;
	newY = (80 - newY)*12;
	$("#display").css("background-position",newX + "px " + newY + "px");
}
//移动select
function mouseDown(a) {
    var o = $("#select").get(0);
    //var o = document.getElementById("display");
    var d = document;
    var x = a.clientX + d.body.scrollLeft - o.offsetLeft;
    var y = a.clientY + d.body.scrollTop - o.offsetTop;
    d.onmousemove = function(a)
    {
        if (!a) a = window.event;
        var newX = a.clientX + document.body.scrollLeft - x;
        var newY = a.clientY + document.body.scrollTop - y;
        if (newX >= 0 && newX <= 80 && newY >= 80 && newY <= 160) {
            o.style.left = newX;
            o.style.top = newY;
			moveImage(newX,newY);
        }
    };
    d.onmouseup = function()
    {
        d.onmousemove = null;
        d.onmouseup = null;
    };
}
</script>
</head>
<body>
<input type="file" id="file"/>
<div id="pic"></div>
<div id="select"></div>
<div id="display"></div>
</body>
</html>
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值