在 Javascript 中拖放
Dragging and Dropping in Javascript
本文关键字:拖放 Javascript 更新时间:2023-09-26
我正在制作一个便笺系统,我希望便笺可以用javascript拖动。我在这个网站上找到了一个拖放脚本,这对我非常有用。但是我有一个"拖拽栏",它位于便笺div内,它应该是您可以拖动该特定便笺的唯一地方。我拥有的脚本仅拖动光标单击的内容,我希望它拖动".dragbar"div 的父元素".parent"。
现在,脚本设置为在单击.parent时拖动.parent。如何在此代码中选择".dragbar"的父div,即".parent"?
如果我让脚本选择 .dragbar,它只是在 .parent 内移动拖动栏。 注意:脚本是原始代码,除了第 87 行和第 95 行中的选择器名称从"拖动"更改为"父"。
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.pagecontent {
width:98%;
height:96%;
overflow:hidden;
background:lightyellow;
border:1px solid black;
padding:10px;
}
.parent {
position:relative;
height:300px;
width:300px;
background-color:gray;
border:1px solid black;
float:left;
margin:0 10px 0 0;
overflow:auto;
}
.dragbar {
height:50px;
width:300px;
background:lightgray;
cursor:move;
position:relative;
}
</style>
</head>
<body>
<div class="pagecontent">
<h1>Dragging Practice</h1>
<h3>Drag and drop the parent div by using the child dragbar</h3>
<pre id="debug">mouse up</pre>
<div class="parent">
<div class="dragbar">I am the Child, my class is .dragbar</div>I am the Parent, my class is
.parent</div>
<div class="parent">
<div class="dragbar">I am the Child, my class is .dragbar</div>I am the Parent, my class is
.parent</div>
<script language="JavaScript" type="text/javascript">
<!--
// this is simply a shortcut for the eyes and fingers
function $(id) {
return document.getElementById(id);
}
var _startX = 0; // mouse starting positions
var _startY = 0;
var _offsetX = 0; // current element offset
var _offsetY = 0;
var _dragElement; // needs to be passed from OnMouseDown to OnMouseMove
var _oldZIndex = 0; // we temporarily increase the z-index during drag
var _debug = $('debug'); // makes life easier
InitDragDrop();
function InitDragDrop() {
document.onmousedown = OnMouseDown;
document.onmouseup = OnMouseUp;
}
function OnMouseDown(e) {
// IE is retarded and doesn't pass the event object
if (e == null) e = window.event;
// IE uses srcElement, others use target
var target = e.target != null ? e.target : e.srcElement;
_debug.innerHTML = target.className == 'parent' //Selector
?
'draggable element clicked' : 'NON-draggable element clicked';
// for IE, left click == 1
// for Firefox, left click == 0
if ((e.button == 1 && window.event != null || e.button == 0) && target.className == 'parent') //Selector
{
// grab the mouse position
_startX = e.clientX;
_startY = e.clientY;
// grab the clicked element's position
_offsetX = ExtractNumber(target.style.left);
_offsetY = ExtractNumber(target.style.top);
// bring the clicked element to the front while it is being dragged
_oldZIndex = target.style.zIndex;
target.style.zIndex = 10000;
// we need to access the element in OnMouseMove
_dragElement = target;
// tell our code to start moving the element with the mouse
document.onmousemove = OnMouseMove;
// cancel out any text selections
document.body.focus();
// prevent text selection in IE
document.onselectstart = function () {
return false;
};
// prevent IE from trying to drag an image
target.ondragstart = function () {
return false;
};
// prevent text selection (except IE)
return false;
}
}
function ExtractNumber(value) {
var n = parseInt(value);
return n == null || isNaN(n) ? 0 : n;
}
function OnMouseMove(e) {
if (e == null) var e = window.event;
// this is the actual "drag code"
_dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px';
_dragElement.style.top = (_offsetY + e.clientY - _startY) + 'px';
_debug.innerHTML = '(' + _dragElement.style.left + ', ' + _dragElement.style.top + ')';
}
function OnMouseUp(e) {
if (_dragElement != null) {
_dragElement.style.zIndex = _oldZIndex;
// we're done with these events until the next OnMouseDown
document.onmousemove = null;
document.onselectstart = null;
_dragElement.ondragstart = null;
// this is how we know we're not dragging
_dragElement = null;
_debug.innerHTML = 'mouse up';
}
}
//-->
</script>
</div>
</body>
</html>
我建议你看看jQuery UI的可拖动和可删除。这些无疑会比你链接的脚本更强大,你可以将元素设置为可以使用jQuery强大的选择器拖动,并且只能在某些元素中拖放。
相关文章:
- 如何在Javascript中创建排序、拖放多级列表
- 为什么使用e.preventDefault的if语句?-拖放javascript
- 拖放下载,支持IE Javascript/Jquery/Html5
- Javascript 拖放脚本
- JavaScript拖放 - 防止元素拖放到另一个可拖放的元素中
- JavaScript/jQuery在两个iframe之间拖放元素
- Javascript拖放测试
- 使用JavaScript(而非HTML5)进行拖放
- 用一点javascript拖放HTML5图像
- Jquery/Javascript代码来测试拖放
- 使用 Javascript 拖放交叉淡入淡出
- HTML5拖放:使用JavaScript在文本框中加载文本文件
- 拖放以在 Javascript 中对矩形重新排序
- 我将如何让OpenLayers功能选择和Javascript拖放一起工作
- 是否可以使用 JavaScript 在 WebBrowser 中拖动 WPF 元素和句柄拖放
- HTML5 / Javascript - 在多个画布之间拖放不起作用的完美示例
- 拖放JavaScript缩略图生成.由于超过base64限制,浏览器崩溃
- 拖放-javascript将ondrasart添加到img中
- 拖放 - Javascript:有没有办法获取拖入文件的文件名
- dom事件-拖放JavaScript查询