gpt4 book ai didi

javascript - 带有 className 的可拖动 div

转载 作者:行者123 更新时间:2023-11-30 05:51:19 25 4
gpt4 key购买 nike

我发现 theZillion ( http://thezillion.wordpress.com/2012/08/29/javascript-draggable-no-jquery/ ) 的这个脚本可以使 div 可拖动。我正在尝试使用此脚本按类名移动 div。而不是通过 ID。

我有一个可以工作的事件处理程序,但在我添加脚本时却没有...控制台也没有显示任何错误。关于如何使这项工作有任何想法吗?

这是我的代码:

function wrappmover(){
var moveEvent = "dice-window-wrapper";
var addClassArr= document.getElementsByClassName(moveEvent);
for(var i=0; i<addClassArr.length; i++){
var addClass = addClassArr[i];
addClass.addEventListener("click", movewrapp, true);
}
function movewrapp() {
var classToMove = "dice-window-wrapper";
var elems = document.getElementsByClassName(classToMove);
var tzdragg = function(){
return {
startMoving : function(evt){
evt = evt || window.event;
var posX = evt.clientX,
posY = evt.clientY,
a = document.getElementsByClassName(classToMove),
divTop = a.style.top,
divLeft = a.style.left;
divTop = divTop.replace('px','');
divLeft = divLeft.replace('px','');
var diffX = posX - divLeft,
diffY = posY - divTop;
document.onmousemove = function(evt){
evt = evt || window.event;
var posX = evt.clientX,
posY = evt.clientY,
aX = posX - diffX,
aY = posY - diffY;
tzdragg.move('elem',aX,aY);
}
},
stopMoving : function(){
document.onmousemove = function(){}
},
move : function(divid,xpos,ypos){
var a = document.getElementById(divid);
document.getElementById(divid).style.left = xpos + 'px';
document.getElementById(divid).style.top = ypos + 'px';
}
}
}();

最佳答案

好的,所以你想在你的页面上有可拖动的元素?

看看下面的代码(这里是 a working example )。我希望你会发现它是不言自明的,但以防万一还有评论:

// Wrap the module in a self-executing anonymous function
// to avoid leaking variables into global scope:
(function (document) {
// Enable ECMAScript 5 strict mode within this function:
'use strict';

// Obtain a node list of all elements that have class="draggable":
var draggable = document.getElementsByClassName('draggable'),
draggableCount = draggable.length, // cache the length
i; // iterator placeholder

// This function initializes the drag of an element where an
// event ("mousedown") has occurred:
function startDrag(evt) {

// The element's position is based on its top left corner,
// but the mouse coordinates are inside of it, so we need
// to calculate the positioning difference:
var diffX = evt.clientX - this.offsetLeft,
diffY = evt.clientY - this.offsetTop,
that = this; // "this" refers to the current element,
// let's keep it in cache for later use.

// moveAlong places the current element (referenced by "that")
// according to the current cursor position:
function moveAlong(evt) {
that.style.left = (evt.clientX - diffX) + 'px';
that.style.top = (evt.clientY - diffY) + 'px';
}

// stopDrag removes event listeners from the element,
// thus stopping the drag:
function stopDrag() {
document.removeEventListener('mousemove', moveAlong);
document.removeEventListener('mouseup', stopDrag);
}

document.addEventListener('mouseup', stopDrag);
document.addEventListener('mousemove', moveAlong);
}

// Now that all the variables and functions are created,
// we can go on and make the elements draggable by assigning
// a "startDrag" function to a "mousedown" event that occurs
// on those elements:
for (i = 0; i < draggableCount; i += 1) {
draggable[i].addEventListener('mousedown', startDrag);
}
}(document));

将其加载或包装在 <script></script> 中标签 as close as possible to </body>这样它就不会阻止浏览器获取其他资源。

其实去掉评论,是一个很小的功能。与您提供的网站上的相比,体积更小、效率更高。

可能的改进

考虑用类似 makeDraggable(selector); 的东西替换匿名包装器其中 selectorCSS selector ,所以你可以做一些疯狂的事情,比如:

makeDraggable('#dragMe, #dragMeToo, .draggable, li:nth-child(2n+1)');

可以使用 document.querySelectorAll 来实现 document.getElementsByClassName 能够执行复杂的 CSS 查询而不是简单的类名查找.

注意事项

  • 如果页面有任何滚动 - 拖动看起来会损坏;考虑通过 scrollX and scrollY 调整拖动元素的位置
  • 这在 Internet Explorer 中显然不起作用(请自行解决)。
  • 可能存在内存泄漏(需要分析和测试)。

编辑:添加新的可拖动元素的解决方案

所以您希望能够添加更多可拖动元素?有几种方法可以解决这个问题。例如你可以写一个 makeDraggable(element);函数并在要添加到 DOM 的元素上调用它。它当然会起作用,但让我们看看不同的东西,好吗?

与其查询 DOM 以搜索可拖动元素并为它们分配事件监听器,我们为什么不为文档主体上的“mousedown”事件分配一个

触发时,事件对象将包含对 target element 的引用这是事件已分派(dispatch)的对象(您鼠标按下的元素)。代码的相关部分现在类似于:

// Performs a check if the current element is draggable and if yes,
// then the dragging is initiated:
function startDragIfDraggable(evt) {
// Check if the target element (referenced by evt.target) contains a
// class named "draggable" (http://stackoverflow.com/questions/5898656/):
if (evt.target.classList.contains('draggable')) {
// Invoke startDrag by passing it the target element as "this":
startDrag.call(evt.target, evt);
}
}

// Listen for any "mousedown" event on the document.body and attempt dragging
// the target element (the one where "mousedown" occurred) if it's draggable:
document.body.addEventListener('mousedown', startDragIfDraggable);

这里是 a working example上面的代码。作为奖励,它具有向 DOM 添加新的可拖动元素的模拟功能。

除了能够拖动动态添加的可拖动元素之外,这种方法还可以为我们节省一些内存,因为我们现在可以避免将一堆事件监听器分配给一堆元素。但是,如果您正在开发的应用程序需要大量点击(例如游戏),那么您可能会因为对每次点击进行检查而浪费一点 CPU。

关于javascript - 带有 className 的可拖动 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14695933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com