js随意拖动图片

<html>

<head>

<metahttp-equiv="Content-Type" content="text/html;charset=gb2312">

<title>随意拖动图片</title>

</head>

<bodyonLoad="remove()">

<div id="div1"onMouseOver="dragimages=div1;drag=1;"style="height:77px;left:10px;position:absolute;top:10px;width:90px">

<imgname="img1" alt="" border="0"src="/images/logo.gif" style="cursor:pointer ">

</div>

<p>

<scriptlanguage="javascript">

drag = 0;//没有拖动

move = 0;//没有移动

function mousedown()

{

if(drag)//鼠标划过去的时候赋值为1

{

        X1 = window.event.x - parseInt(dragimages.style.left);//差距

        Y1 = window.event.y - parseInt(dragimages.style.top);

        dragimages.style.Index += 1;

        move = 1;//让他移动

}

}

function mouseStop()

{

window.event.returnValue = false;

}

function mousemove()

{

if (move)

{

        dragimages.style.left = window.event.x - X1;//鼠标位置-差距

        dragimages.style.top = window.event.y - Y1;

}

}

function mouseup()

{

move = 0;//停止移动

}

function remove()

{

document.all.div1.onmousemove = mousemove;

document.all.div1.onmousedown = mousedown;

document.all.div1.onmouseup = mouseup;

document.all.div1.ondragstart = mouseStop;

}

</script>

</body>

</html>

关于event.returnvalue=false在ie中表示当做了某个判断,就不允许此事件继续执行。

<scriptlanguage="javascript" type="text/javascript">

  function test()

  {

         if(event.keyCode == 46)

         {

                event.returnValue = false;

         }

  }

</script>

<inputtype="text" οnkeypress="test()"/>

在火狐中是:event.preventDefault();

<scriptlanguage="javascript" type="text/javascript">

  function test(event)

  {

         if(event.charCode == 46)

         {

                event.preventDefault();

         }

  }

</script>

 

<inputtype="text" οnkeypress="test(event)"/>

此外window.event.returnValue=false放到提交表单中的onclick事件中则不会提交表单,如果放到超链接中则不执行超链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值