当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS实现鼠标拖拽盒子移动及右键点击盒子消失效果示例

JS实现鼠标拖拽盒子移动及右键点击盒子消失效果示例

2019年03月17日  | 移动技术网IT编程  | 我要评论
本文实例讲述了js实现鼠标拖拽盒子移动及右键点击盒子消失效果。分享给大家供大家参考,具体如下: 1. 鼠标拖拽盒子移动效果 <!doctype html&

本文实例讲述了js实现鼠标拖拽盒子移动及右键点击盒子消失效果。分享给大家供大家参考,具体如下:

1. 鼠标拖拽盒子移动效果

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>title</title>
  <style>
    *{
      margin:0;
      padding:0;
    }
    div{
      width: 100px;
      height: 100px;
      background: blue;
      position: absolute;
    }
  </style>
</head>
<body>
<div>
</div>
<script>
  window.onload=function () {
    var odiv=document.getelementsbytagname("div")[0];
    odiv.onmousedown=function () {
      document.onmousemove=function (ev) {
        var event=window.event||ev;
        odiv.style.left=event.clientx+"px";
        odiv.style.top=event.clienty+"px";
      }
      document.onmouseup=function (){
        document.onmousemove=null;
        document.onmouseup=null;
      }
    }
  }
</script>
</body>
</html>

2. 鼠标右键使盒子消失

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>title</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    div{
      width: 100px;
      height: 100px;
      background: red;
      display: block;
    }
  </style>
</head>
<body>
<div>
</div>
<script>
  window.onload=function () {
    document.oncontextmenu=function () {
      var odiv=document.getelementsbytagname("div")[0];
      odiv.style.display="none"
      return false
    }
  }
</script>
</body>
</html>

感兴趣的朋友可以使用在线html/css/javascript代码运行工具:测试一下运行效果。

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript页面元素操作技巧总结》、《javascript操作dom技巧总结》、《javascript切换特效与技巧总结》、《javascript动画特效与技巧汇总》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》及《javascript数学运算用法总结

希望本文所述对大家javascript程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网