首页 > 其他分享 >【前端教程02】js拖拽布局demo

【前端教程02】js拖拽布局demo

时间:2023-06-30 16:13:07浏览次数:41  
标签:02 color demo 50px height width var box4 js

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
      .page {
         display: flex;
      }

      .box1 input {
         width: 260px;
         height: 50px;
         border: solid 1px blue;
         background-color: #dcdcdc;
         margin-top: 20px;

      }
      .box2{
         width: 260px;
         height: 50px;
         background-color: #dcdcdc;
         color: #333;
         text-align: center;
         line-height: 50px;
      }
      .box3 button{
         width: 260px;
         height: 50px;
         background-color: red;
         color: #fff;
         text-align: center;
         line-height: 50px;
         border: none;
         margin-top: 20px;
      }
      .box4{
         width: 300px;
         height: 600px;
         border: 1px solid blue;
     
      }
   </style>
</head>

<body>
  <div class="page">
   <!-- 可拖拽元素 -->
   <div style="width: 400px;">
      <div draggable="true" class="box1 drage">
         <input type="text" placeholder="请输入">
      </div>
      <div draggable="true" class="box2 drage">
         <h1>这里是文案区域</h1>
      </div>
      <div draggable="true" class="box3 drage">
         <button>提交</button>
      </div>
   </div>
   
   <!-- 布局区域 -->
   <div id="box4" class="box4">拖入布局</div>

  </div>

</body>

<script>
// 拖拽
//  var box1 = document.getElementById('box1')

//  box1.addEventListener('dragstart',function(e){
//  })

//  box3.addEventListener('drag',function(e){
//       box3.style.marginLeft = e.clientX + 'px'
//    box3.style.marginTop = e.clientY + 'px'
//  })

var box4 = document.getElementById('box4')

function setDom(e){
   const x = e.clientX 
   const y = e.clientY 
   // 拖动到指定区域则插入
   if(x < 400 && x > 700 ||  y > 0 && y < 600 ) {
      var dom = document.createElement('div')
      dom.innerHTML = e.toElement.outerHTML
      dom.style.marginLeft = '20px'
      box4.append( dom)
   }
}

var dragArr = document.getElementsByClassName('drage')

// 拖动每个元素箭头拖拽事件
for (let i=0; i<dragArr.length;i++ ) {
   dragArr[i].addEventListener('dragend', function(e) {
      setDom(e)
   })
}

</script>
 
</html>

 

看效果图

标签:02,color,demo,50px,height,width,var,box4,js
From: https://www.cnblogs.com/xiaohuizhang/p/17517077.html

相关文章

  • node使用jsonwebtoken生成token与验证是否过期
    场景我们可以使用cookie,session,token来做鉴权。下面我们来看一下,如何使用token来做鉴权jwt.sign的简单介绍npminstalljsonwebtoken下载使用jsonwebtoken生成token的基本语法。jwt.sign(payload,secretOrPrivateKey,[options,callback])第1个参数payload:可以......
  • 火山引擎开发者社区一周资讯精选 2023/06/30
    ......
  • 2023-06-30 reportJSException >>>> exception function:createInstanceContext, exce
    uniapp之运行到android端报错:reportJSException>>>>exceptionfunction:createInstanceContext,exception:whitescreencausecreateinstanceContextfailed,checkjsstack->UncaughtSyntaxError:Invalidorunexpectedtoken,即:reportJSException>>异......
  • 【js学习笔记十四】普通函数中的this指向问题解决方案_this
     目录前言导语 解决思路运行结果前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从头再来歌谣的意志是永恒的放弃很容易但是坚持一定很酷导语歌谣歌谣......
  • 【js学习笔记十五】普通函数中的this指向问题解决方案箭头函数
     目录前言导语 解决思路运行结果前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从头再来歌谣的意志是永恒的放弃很容易但是坚持一定很酷导语歌谣歌谣......
  • 602. 好友申请 II :谁有最多的好友
    602.好友申请II:谁有最多的好友SQL架构在Facebook或者Twitter这样的社交应用中,人们经常会发好友申请也会收到其他人的好友申请。RequestAccepted表:+----------------+---------+|ColumnName|Type|+----------------+---------+|requester_id|int......
  • js获取 当前时间 当前零时 当月零时 当年零时
     constcurrentTime=newDate().getTime()//当前时间戳 constcurrentZero=newDate().setHours(0,0,0)//当天零时时间戳 constcurrentMonthFirstDayZero=newDate(newDate().setDate(1)).setHours(0,0,0)//当月第一天时间戳 constcurrentYearFirs......
  • redisson对单个json的单个字段进行数值操作 - lua脚本
    @OverridepublicStringupdateJson(Stringkey,Stringfield,longnum,longlimit){Stringscript="localt=redis.call('get',KEYS[1]);\n"+"localj;\n"+"iftthen......
  • 贪吃蛇制作js
    <!DOCTYPEhtml><html><head><title>贪吃蛇游戏</title><style>#game-board{width:400px;height:400px;border:1pxsolid#000;position:relative;}.snake{width:20px;height:20......
  • Vue3 vite:is a JavaScript file. Did you mean to enable the 'allowJs' option?
    描述今天在vue3+vite下进行打包时,突然vscode报了一个error。 大概的意识是询问是否启用“allowJS”选项,因为该文件在程序内是指定用于编译的根文件。提示信息已经很明确了,下面从网上摘抄了下什么是allowJS选项。allowJs是1.8中新提供的选项。TypeScrip......