拖拽上传

本文详细介绍如何使用HTML5的拖放API实现DOM元素的拖拽功能,包括dragstart、dragover、drop等事件的使用,以及jQuery在事件处理中的应用。
  • draggable=“true” 页面设置后才可以拖拽
  • dragstart 事件 开始拖动元素时触发
  • drop 事件 拖动过程中,释放鼠标键时触发此事件
  • dragover 事件 被拖动的对象在另一对象容器范围内拖动时触发此事件
  • dragenter 事件 完拖动的对象进入其容器范围内时触发此事件
  • dragleave 事件 拖动的对象离开其容器范围内时触发此事件

书写就按照上面的顺序

实例

<style>
  .page {
    width: 1000px;
    margin: 0px auto;
  }

  .left {
    width: 500px;
    float: left;
  }

  .left .container {
    padding: 10px;
    min-height: 500px;
    box-sizing: border-box;
  }

  .left .containerDrop {
    background: #eeeeee;
  }

  .left .container h1 {
    text-align: center;
  }

  .right {
    width: 500px;
    float: left;
    box-sizing: border-box;
    border-left: 1px solid red;
  }

  .right .ctrlList {
    padding: 10px;
  }

  .right .ctrlList .box {
    border: 1px dashed gray;
    margin-top: 10px;
    margin-bottom: 10px;
  }

  .line {
    margin-top: 10px;
    margin-bottom: 10px;
    padding: 5px;
  }
</style>
<div class="page">
  <div class="left">
    <div id="container" class="container">
      <h1>问卷</h1>
    </div>
  </div>
  <div class="right">
    <div class="ctrlList">
      <div class="box" draggable="true">
        <div class="line">
          <span>姓名:</span>
          <input type="text" name="name" placeholder="请输入真实姓名"></input>
        </div>
      </div>
      <div class="box" draggable="true">
        <div class="line">
          <span>年龄:</span>
          <input type="number" name="age" placeholder="请输入年龄"></input>
        </div>
      </div>
      <div class="box" draggable="true">
        <div class="line">
          <span>性别:</span>
          <input type="radio" name="gender" value="male">男</input>
          <input type="radio" name="gender" value="female">女</input>
        </div>
      </div>
      <div class="box" draggable="true">
        <div class="line">
          <span>兴趣爱好:</span>
          <input type="checkbox" name="hobby" value="basketball">篮球</input>
          <input type="checkbox" name="hobby" value="football">足球</input>
          <input type="checkbox" name="hobby" value="tennis">网球</input>
          <input type="checkbox" name="hobby" value="	badminton">羽毛球</input>
          <input type="checkbox" name="hobby" value="tabletennis">乒乓球</input>
        </div>
      </div>
    </div>
  </div>
</div>
<script>
  $(function () {
    var dragSrc;

    $(".box").each(function (index, ele) {
      $(ele).on("dragstart", function (event) {
        dragSrc = this;
      })
    });

    $("#container").on("drop", function (e) {
      e.preventDefault();
      $(this).append($(dragSrc).find('.line').clone());
      $(this).removeClass("containerDrop");
    }).on("dragover", function (e) {
      e.preventDefault();
    }).on("dragenter", function (e) {
      e.preventDefault();
      $(this).addClass("containerDrop");
    }).on("dragleave", function (e) {
      e.preventDefault();
      $(this).removeClass("containerDrop");
    })
  });
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值