2019-01-17 00:44:51 liuchuo 阅读数 283
  • 《C语言/C++学习指南》Qt界面开发篇视频精讲

    本篇讲解如何使用Qt库进行界面开发,是一个从入门到精通的教程。主要的演示平台为VS2008 + Qt4.7.3,从简单的例子入手,覆盖Qt界面开发的近乎全部技术点。(自定义Widget、对话框、事件处理Singal & Slot、界面线程与工作线程、数据视图ItemView、拖放操作Drag & Drop、界面美化Qt Style等等) 提供全部示例代码的下载。 (前四章免费)

    69477 人正在学习 去看看 邵发

Touch Drag Inside:手指在控件窗口内拖动

Touch Drag Outside:手指在控件窗口外拖动

Touch Drag Enter:手指从控件窗口外部拖动到内部

Touch Drag Exit:手指从控件窗口内部拖动到外部


[依据] 来自Apple官方文档 UIControlEvents

UIControlEventTouchDragInside:An event where a finger is dragged inside the bounds of the control.

UIControlEventTouchDragOutside:An event where a finger is dragged just outside the bounds of the control.

UIControlEventTouchDragEnter:An event where a finger is dragged into the bounds of the control.

UIControlEventTouchDragExit:An event where a finger is dragged from within a control to outside its bounds.

2015-11-22 17:48:28 xinyuan_java 阅读数 702
  • 《C语言/C++学习指南》Qt界面开发篇视频精讲

    本篇讲解如何使用Qt库进行界面开发,是一个从入门到精通的教程。主要的演示平台为VS2008 + Qt4.7.3,从简单的例子入手,覆盖Qt界面开发的近乎全部技术点。(自定义Widget、对话框、事件处理Singal & Slot、界面线程与工作线程、数据视图ItemView、拖放操作Drag & Drop、界面美化Qt Style等等) 提供全部示例代码的下载。 (前四章免费)

    69477 人正在学习 去看看 邵发

常用drag控件

可drag的div实例: http://dragsort.codeplex.com/


可调的windows弹窗: http://www.5icool.org/demo/2012/a00576/


easyDialog: http://demo.sc.chinaz.com/Files/DownLoad/webjs1/201406/jiaoben2413/


layer: http://layer.layui.com/



简单的弹窗实例1

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>弹出层并可拖拽</title> 
<style> 
html,body{height:100%;overflow:hidden;} 
body,div,h2{margin:0;padding:0;} 
body{font:12px/1.5 Tahoma;} 
center{padding-top:10px;} 
button{cursor:pointer;} 
#overlay{position:absolute;top:0;left:0;width:100%;height:100%;background:#000;opacity:0.5;filter:alpha(opacity=50);display:none;} 
#win{position:absolute;top:50%;left:50%;width:400px;height:200px;background:#fff;border:4px solid #f90;margin:-102px 0 0 -202px;display:none;} 
h2{font-size:12px;height:18px;text-align:right;background:#FC0;border-bottom:3px solid #f90;padding:5px;cursor:move;} 
h2 span{color:#f90;cursor:pointer;background:#fff;border:1px solid #f90;padding:0 2px;} 
</style> 
<script> 
window.onload = function () 
{ 
var oWin = document.getElementById("win"); 
var oLay = document.getElementById("overlay"); 
var oBtn = document.getElementsByTagName("button")[0]; 
var oClose = document.getElementById("close"); 
var oH2 = oWin.getElementsByTagName("h2")[0]; 
var bDrag = false; 
var disX = disY = 0; 
oBtn.onclick = function () 
{ 
oLay.style.display = "block"; 
oWin.style.display = "block" 
}; 
oClose.onclick = function () 
{ 
oLay.style.display = "none"; 
oWin.style.display = "none" 

}; 
oClose.onmousedown = function (event) 
{ 
(event || window.event).cancelBubble = true; 
}; 
oH2.onmousedown = function (event) 
{ 
var event = event || window.event; 
bDrag = true; 
disX = event.clientX - oWin.offsetLeft; 
disY = event.clientY - oWin.offsetTop; 
this.setCapture && this.setCapture(); 
return false 
}; 
document.onmousemove = function (event) 
{ 
if (!bDrag) return; 
var event = event || window.event; 
var iL = event.clientX - disX; 
var iT = event.clientY - disY; 
var maxL = document.documentElement.clientWidth - oWin.offsetWidth; 
var maxT = document.documentElement.clientHeight - oWin.offsetHeight; 
iL = iL < 0 ? 0 : iL; 
iL = iL > maxL ? maxL : iL; 
iT = iT < 0 ? 0 : iT; 
iT = iT > maxT ? maxT : iT; 

oWin.style.marginTop = oWin.style.marginLeft = 0; 
oWin.style.left = iL + "px"; 
oWin.style.top = iT + "px"; 
return false 
}; 
document.onmouseup = window.onblur = oH2.onlosecapture = function () 
{ 
bDrag = false; 
oH2.releaseCapture && oH2.releaseCapture(); 
}; 
}; 
</script> 
</head> 
<body> 
<div id="overlay"></div> 
<div id="win"><h2><span id="close">×</span></h2></div> 
<center><button>弹出层</button></center> 
</body> 
</html> 

简单弹窗2


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>JAVASCRIPT弹出层</title> 
<META http-equiv=Content-Type content="text/html; charset=utf-8"> 
<style> 
#popDiv { 
position: absolute; 
visibility: hidden; 
overflow: hidden; 
border: 2px solid #AEBBCA; 
background-color: #EEF1F8; 
cursor: move; 
padding: 1px; 
} 

#popTitle { 
background: #9DACBF; 
height: 20px; 
line-height: 20px; 
padding: 1px; 
} 

#popForm { 
padding: 2px; 
} 

.title_left { 
font-weight: bold; 
padding-left: 5px; 
float: left; 
} 

.title_right { 
float: right; 
} 

#popTitle .title_right a { 
color: #000; 
text-decoration: none; 
} 

#popTitle .title_right a:hover { 
text-decoration: underline; 
color: #FF0000; 
} 
</style> 
<script> 
function showPopup() {//弹出层 
var objDiv = document.getElementById("popDiv"); 
objDiv.style.top = "50px";//设置弹出层距离上边界的距离 
objDiv.style.left = "200px";//设置弹出层距离左边界的距离 
objDiv.style.width = "300px";//设置弹出层的宽度 
objDiv.style.height = "200px";//设置弹出层的高度 
//objDiv.style.display = "block"; 
objDiv.style.visibility = "visible"; 
} 
function hidePopup() {//关闭层 
var objDiv = document.getElementById("popDiv"); 
objDiv.style.visibility = "hidden"; 
} 
</script> 
</head> 
<body> 
<div id="popDiv"> 
<div id="popTitle"> <!-- 标题div --> 
<span class="title_left">修改操作</span> <span class="title_right"><a 
href="#" onclick="hidePopup();">关闭</a> </span> 
</div> 
<div id="popForm"> <!-- 表单div --> 
<form action="insert_map.jsp" method="post"> 
<p> 
      ID :<input type="text" name="id" value="0" /> </br> 
名    称 :<input type="text" name="name" value="aaa" /> </br> 
电压等级 :<input type="text" name="voltage_level" value="110kv" /> </br> 
经    度 :<input type="text" name="lon" value="121." /> </br> 
纬    度 :<input type="text" name="lat" value="28." /> </br> 
</p> 
   <input type="submit" value="提交" />   
   <input type="reset" value="重置" />   
   <input type="reset" value="取消" onclick="hidePopup()" /> 
</form> 
</div> 
</div> 
<p> 
<input name="" type="button" onclick="showPopup()" value="操作" /> 
</p> 

<script type="text/javascript"> 
/*-------------------------鼠标左键拖动---------------------*/ 
/*--------当不需要实现此功能时,可以将这一部分代码删除------------*/ 
var objDiv = document.getElementById("popDiv"); 
var isIE = document.all ? true : false;//判断浏览器类型 
document.onmousedown = function(evnt) {//当鼠标左键按下后执行此函数 
var evnt = evnt ? evnt : event; 
if (evnt.button == (document.all ? 1 : 0)) { 
mouseD = true;//mouseD为鼠标左键状态标志,为true时表示左键被按下 
} 
} 

objDiv.onmousedown = function(evnt) { 
objDrag = this;//objDrag为拖动的对象 
var evnt = evnt ? evnt : event; 
if (evnt.button == (document.all ? 1 : 0)) { 
mx = evnt.clientX; 
my = evnt.clientY; 
objDiv.style.left = objDiv.offsetLeft + "px"; 
objDiv.style.top = objDiv.offsetTop + "px"; 
if (isIE) { 
objDiv.setCapture(); 
//objDiv.filters.alpha.opacity = 50;//当鼠标按下后透明度改变 
} else { 
window.captureEvents(Event.MOUSEMOVE);//捕获鼠标拖动事件 
//objDiv.style.opacity = 0.5;//当鼠标按下后透明度改变 
} 
} 
} 
document.onmouseup = function() { 
mouseD = false;//左键松开 
objDrag = ""; 
if (isIE) { 
objDiv.releaseCapture(); 
//objDiv.filters.alpha.opacity = 100;//当鼠标左键松开后透明度改变 
} else { 
window.releaseEvents(objDiv.MOUSEMOVE);//释放鼠标拖动事件 
//objDiv.style.opacity = 1;//当鼠标左键松开后透明度改变 
} 
} 

document.onmousemove = function(evnt) { 
var evnt = evnt ? evnt : event; 
if (mouseD == true && objDrag) { 
var mrx = evnt.clientX - mx; 
var mry = evnt.clientY - my; 
objDiv.style.left = parseInt(objDiv.style.left) + mrx + "px"; 
objDiv.style.top = parseInt(objDiv.style.top) + mry + "px"; 
mx = evnt.clientX; 
my = evnt.clientY; 
} 
} 
</script> 

</body> 
</html> 



2019-05-21 10:00:58 m936956 阅读数 58
  • 《C语言/C++学习指南》Qt界面开发篇视频精讲

    本篇讲解如何使用Qt库进行界面开发,是一个从入门到精通的教程。主要的演示平台为VS2008 + Qt4.7.3,从简单的例子入手,覆盖Qt界面开发的近乎全部技术点。(自定义Widget、对话框、事件处理Singal & Slot、界面线程与工作线程、数据视图ItemView、拖放操作Drag & Drop、界面美化Qt Style等等) 提供全部示例代码的下载。 (前四章免费)

    69477 人正在学习 去看看 邵发

Drag and drop single element

Actions action = new Actions (driver);
action.dragAndDrop(sourceElement, targetElement).perform();

Drag and Drop multiple elements 

 Drag source element, and move Element1 and Element2 to target place.

 Actions action = new Actions (driver);
        action.keyDown(Keys.SHIFT)
            .click(Element1)
            .click(Element2)
            .keyUp(Keys.SHIFT)
            .clickAndHold(source)
            .moveToElement(target)
            .release()
            .build().perform();

 

2019-12-24 14:49:49 qq_24724109 阅读数 18
  • 《C语言/C++学习指南》Qt界面开发篇视频精讲

    本篇讲解如何使用Qt库进行界面开发,是一个从入门到精通的教程。主要的演示平台为VS2008 + Qt4.7.3,从简单的例子入手,覆盖Qt界面开发的近乎全部技术点。(自定义Widget、对话框、事件处理Singal & Slot、界面线程与工作线程、数据视图ItemView、拖放操作Drag & Drop、界面美化Qt Style等等) 提供全部示例代码的下载。 (前四章免费)

    69477 人正在学习 去看看 邵发

Drag,Drop踩坑笔记

1. 几个概念

1. 拖拽事件

摘自MDN:

HTML drag-and-drop uses the DOM event model and drag events inherited from mouse events. A typical drag operation begins when a user selects a draggable element, drags the element to a droppable element, and then releases the dragged element.

HTML拖拽事件使用DOM事件模型,drag事件继承于鼠标事件

一个典型的拖拽事件从用户选择一个draggable元素开始,将该元素托到一个droppable元素下,然后释放该被拖拽的元素

上述的描述中有几个点:

  • 源对象(draggable元素): 正在被拖动的对象, 该元素的draggable属性应被设置为true
  • 目标对象: 拖拽到的目标对象,需要对元素的drag事件进行监听,并做相应操作才能转化为droppable元素

2. JS拖拽事件

事件 事件处理函数 作用对象 描述
drag ondrag 源对象 源文件被拖动触发
dragstart ondragstart 源对象 用户开始拖拽源对象
dragend ondragend 源对象 用户结束拖拽操作(例如释放鼠标按键和点击ESC按键)
dragenter ondragenter 目标对象 拖拽源对象进入目标对象
dragover ondragover 目标对象 源对象处于目标对象上方(每几百毫秒触发一次)
dragleave ondragleave 目标对象 源对象离开目标对象区域
dragexit ondragexit 目标对象 元素不再为可被选择的目标对象
drop ondrop 目标对象 源对象落在目标对象上

1. 几个注意点

  • 关于dragenter和dragover的注意点

摘自mdn:

A listener for the dragenter and dragover events are used to indicate valid drop targets, that is, places where dragged items may be dropped. Most areas of a web page or application are not valid places to drop data. Thus, the default handling of these events is not to allow a drop.

If you want to allow a drop, you must prevent the default handling by cancelling the event. You can do this either by returning false from an attribute-defined event listener, or by calling the event’s preventDefault() method. The latter may be more feasible in a function defined in a separate script.

根据以上描述归纳三点:

  • 通过监听一个元素的dragenter和dragover事件可以表明一个元素是有效的目标对象
  • 如果执行上述两个事件的默认处理函数,源对象还是不能被drop的
  • 如果想要被允许drop的话,需要通过preventDefault来该事件的默认行为

2. 拖动案例

图片拖动案例参考: 原生JS快速实现拖放(drag and drop)效果

  • DOM结构
<body>
    <div class="droppable">
      <div class="box" draggable="true"></div>
    </div>
    <div class="droppable"></div>
    <div class="droppable"></div>
    <div class="droppable"></div>
    <div class="droppable"></div>
    <div class="droppable"></div>
</body>
  • JS实现图片的拖拽

主要思路:

  1. draggale元素本身在拖拽的时候,文件不会消失,因此需要手动添加一个display:none使原来的元素不显示
  2. 对droppable的落点框进行dragenter, dragover事件的监听,这里要阻止默认的事件的处理函数
  3. 对drop事件的回调进行操作,当事件落下的时候进行操作
const dragElem = document.querySelector('.box');
const droppables = document.querySelectorAll('.droppable');

dragElem.addEventListener('drag', function(event) {
    console.log('-------------Drag Event--------------');
});

dragElem.addEventListener('dragstart', function() {
    console.log('-------------Drag Start Event--------------');
    setTimeout(() => {
        this.classList.add('invisible');
    }, 0);
});

dragElem.addEventListener('dragend', function() {
    console.log('-------------Drag End Event--------------');
    setTimeout(() => {
        this.classList.remove('invisible');
    }, 0);
});

droppables.forEach((elem, index) => {
    elem.addEventListener('dragenter', function(event) {
        // 如果不调用event.preventDefault,导致drop事件失效
        event.preventDefault();
        console.log(`Droppable ${index} dropover event`);
    });

    elem.addEventListener('dragover', function(event) {
        // dropover事件的默认处理函数会使得drop事件不被捕获
        event.preventDefault();
        console.log(`Droppable ${index} dropover event`);
        this.classList.add('drag-over');
    });

    elem.addEventListener('dragleave', function(event) {
        event.preventDefault();
        console.log(`Dragleave ${index} dropover event`);
        this.classList.remove('drag-over')
    });

    elem.addEventListener('drop', function(event) {
        console.log(`Drop in ${index}`);
        setTimeout(() => {
            this.append(dragElem);
            dragElem.classList.remove('invisible');
            this.classList.remove('drag-over')
        }, 0);
    });
});

3. 几个注意点

dataTransfer中保存的为字符串,因此我们需要将获取到的字符串先转换成string,然后再转换为dom节点进行添加

4. 代码实现

  • dom节点与string相互转换的一个工具类
const HtmlStringTransfer = function() {
    this.secret = null;

    this.setSecret = function() {
        const _secret = Math.random().toString(36).substr(2);
        this.secret = _secret;
    }

    this.getString = function(HTMLNode) {
        this.setSecret();
        HTMLNode.setAttribute('id', this.secret);
        return HTMLNode.outerHTML;
    };

    this.getNode = function() {
        const _node = document.getElementById(this.secret);
        _node.removeAttribute('id');

        return _node;
    };
};
  • 利用dataTransfer.setData设置源对象
const Factory = new HtmlStringTransfer();

dragElem.addEventListener('dragstart', function(event) {
    console.log('-------------Drag Start Event--------------');

    const _sourceElement = Factory.getString(event.target);

    event.dataTransfer.setData('sourceElement', _sourceElement);
    setTimeout(() => {
        this.classList.add('invisible');
    }, 0);
});
  • 利用dataTransfer.getData在drop时获得源对象
elem.addEventListener('drop', function(event) {
    console.log(`Drop in ${index}`);
    const sourceElement = event.dataTransfer.getData('SourceElement');
    const _sourceElement = Factory.getNode(sourceElement);

    setTimeout(() => {
        this.append(_sourceElement);
        dragElem.classList.remove('invisible');
        this.classList.remove('drag-over');
    }, 0);
});
  • 完整代码
<!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>Test</title>
    <style>
      body {
        background: #eee;
      }

      .droppable {
        display: inline-block;
        width: 160px;
        height: 160px;
        margin: 10px;
        border: 3px salmon solid;
        background-color: #fff;
        margin: 20px;
      }

      .box {
        width: 150px;
        height: 150px;
        background-image: url('./1.jpg');
        background-size: 150px 150px;
      }

      .drag-over {
        border-style: dashed;
      }

      .dragging {
        background-color: yellow;
      }

      .invisible {
        display: none;
      }
    </style>
  </head>
  <body>
    <div class="droppable" src-index="0">
      <div draggable="true" class="box"></div>
    </div>
    <div class="droppable" src-index="1"></div>
    <div class="droppable" src-index="2"></div>
    <div class="droppable" src-index="3"></div>
    <div class="droppable" src-index="4"></div>
    <div class="droppable" src-index="5"></div>

    <script>
      const dragElem = document.querySelector('.box');
      const droppables = document.querySelectorAll('.droppable');

      const HtmlStringTransfer = function() {
        this.secret = null;

        this.setSecret = function() {
          const _secret = Math.random().toString(36).substr(2);
          this.secret = _secret;
        }

        this.getString = function(HTMLNode) {
          this.setSecret();
          HTMLNode.setAttribute('id', this.secret);
          return HTMLNode.outerHTML;
        };

        this.getNode = function() {
          const _node = document.getElementById(this.secret);
          _node.removeAttribute('id');

          return _node;
        };
      };

      const Factory = new HtmlStringTransfer();

      dragElem.addEventListener('drag', function(event) {
        console.log('-------------Drag Event--------------');
      });

      dragElem.addEventListener('dragstart', function(event) {
        console.log('-------------Drag Start Event--------------');

        const _sourceElement = Factory.getString(event.target);

        event.dataTransfer.setData('sourceElement', _sourceElement);
        setTimeout(() => {
          this.classList.add('invisible');
        }, 0);
      });

      dragElem.addEventListener('dragend', function() {
        console.log('-------------Drag End Event--------------');
        setTimeout(() => {
          this.classList.remove('invisible');
        }, 0);
      });

      droppables.forEach((elem, index) => {
        elem.addEventListener('dragenter', function(event) {
          // 如果不调用event.preventDefault,导致drop事件失效
          event.preventDefault();
          console.log(`Droppable ${index} dropover event`);
        });

        elem.addEventListener('dragover', function(event) {
          // dropover事件的默认处理函数会使得drop事件不被捕获
          event.preventDefault();
          console.log(`Droppable ${index} dropover event`);
          this.classList.add('drag-over');
        });

        elem.addEventListener('dragleave', function(event) {
          event.preventDefault();
          console.log(`Dragleave ${index} dropover event`);
          this.classList.remove('drag-over');
        });

        elem.addEventListener('drop', function(event) {
          console.log(`Drop in ${index}`);
          const sourceElement = event.dataTransfer.getData('SourceElement');
          const _sourceElement = Factory.getNode(sourceElement);

          setTimeout(() => {
            this.append(_sourceElement);
            dragElem.classList.remove('invisible');
            this.classList.remove('drag-over');
          }, 0);
        });
      });
    </script>
  </body>
</html>

4. 实验结果分析

1. 实验结果

在这里插入图片描述

2. 事件的监听顺序

在这里插入图片描述

  1. 源对象的drag start
  2. 源对象的drag事件(只要鼠标拖动就会产生该事件)
  3. 拖动后第一次产生源对象所在目标对象的dragenter事件
  4. 源对象所在的对象的dragover事件
  5. 离开原目标对象的dragleave事件
  6. 新目标对象的dragenter事件
  7. 新目标对象的dragover事件
  8. drop时新目标对象的drop事件
2009-12-19 18:44:00 mrtuzi 阅读数 208
  • 《C语言/C++学习指南》Qt界面开发篇视频精讲

    本篇讲解如何使用Qt库进行界面开发,是一个从入门到精通的教程。主要的演示平台为VS2008 + Qt4.7.3,从简单的例子入手,覆盖Qt界面开发的近乎全部技术点。(自定义Widget、对话框、事件处理Singal & Slot、界面线程与工作线程、数据视图ItemView、拖放操作Drag & Drop、界面美化Qt Style等等) 提供全部示例代码的下载。 (前四章免费)

    69477 人正在学习 去看看 邵发

 

 

android Drag and Drop

阅读数 66

drag

阅读数 739

bootstrap之Drag

阅读数 2988

没有更多推荐了,返回首页