久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 JavaScript 中相對定位的元素上實(shí)現(xiàn)拖放

Implementing drag and drop on relatively positioned elements in JavaScript(在 JavaScript 中相對定位的元素上實(shí)現(xiàn)拖放)
本文介紹了在 JavaScript 中相對定位的元素上實(shí)現(xiàn)拖放的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號..

對于發(fā)布這么多問題,我深表歉意 - 我還在學(xué)習(xí)!所以,我可以在 html 中拖放第一個元素沒有問題;但是,當(dāng)我嘗試拖放第二個時(shí),它會出現(xiàn)在鼠標(biāo)下方相當(dāng)多.它仍然在我移動鼠標(biāo)的任何地方拖動,但遠(yuǎn)低于.怎么了?

I apologize for posting so many questions- I'm still learning! So, I can drag and drop the first element in the html no problem; however, when I try to drag and drop the second one, it appears quite a bit below the mouse. It still drags wherever I move the mouse, but far below. What's wrong?

這里是javascript:

Here is the javascript:

// JavaScript Document

var posX;
var posY;
var element;

document.addEventListener("mousedown", drag, false);

function drag(event) {
    if(event.target.className == "square") {
        element = event.target;
        posX = event.clientX -parseInt(element.offsetLeft);
        posY = event.clientY -parseInt(element.offsetTop);
        document.addEventListener("mousemove", move, false);
    }
}

function move(event) {

    if (typeof(element.mouseup) == "undefined")
        document.addEventListener("mouseup", drop, false);
    //Prevents redundantly adding the same event handler repeatedly

    element.style.left = event.clientX - posX + "px";
    element.style.top = event.clientY - posY + "px";
}    

function drop() {
    document.removeEventListener("mousemove", move, false);
    document.removeEventListener("mouseup", drop, false);
    //alert("DEBUG_DROP");
}    

這里是html:

<body>

<p class="square">Thing One</p>
<p class="square">Thing Two</p>

</body>

這是css:

/* CSS Document */

.square {
    position: relative;
    width: 100px;
    height: 100px;
    background: red;
}

p {
    padding: 0px;
    margin: 0px;
}

感謝大家的寶貴時(shí)間!非常感謝!

Thank you all for your time! I greatly appreciate it!

推薦答案

這里有兩個不同的問題需要考慮:

There are two separate issues to consider here:

  1. 如何獲取頁面上元素的位置?
  2. 當(dāng)我為 lefttop 樣式屬性分配新值時(shí)會發(fā)生什么?
  1. How can I get the position of an element on the page?
  2. What happens when I assign new values to the left and top style properties?

第一個問題的答案取決于對element.offsetLeftelement.offsetTop的理解.這些屬性給出了相對于 element.offsetParent 的偏移量,定義為最近的(包含層次結(jié)構(gòu)中最近的)定位的包含元素".

The answer to the first question relies on an understanding of element.offsetLeft and element.offsetTop. These properties give the offset relative to element.offsetParent which is defined as "the closest (nearest in the containment hierarchy) positioned containing element".

在您的文檔中,這是 <body> 元素,因此它可以按您的預(yù)期工作.如果您將方塊放在其他元素中,您會發(fā)現(xiàn)您的代碼將停止工作.你需要的是一個函數(shù)來遍歷包含元素的樹并對每個偏移量求和,像這樣:

In your document this is the <body> element, so it works as you expected. If you were to place your squares inside other elements you'd find that your code would stop working. What you'd need is a function to walk up the tree of containing elements and sum each of the offsets, like this:

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return { x: curleft, y: curtop };
    }
}

這樣使用:

var pos= findPos(element);
posX = event.clientX - pos.x;
posY = event.clientY - pos.y;

回答第二個問題需要了解相對定位的元素.相對定位的元素出現(xiàn)在它們通常出現(xiàn)在頁面上的位置相對.當(dāng)您為 topleft 賦值時(shí),您并沒有給出絕對值.您給出的位置被視為相對于布局中元素的原始位置.這是相對定位的特性,而不是錯誤.您可能想使用 position: absolute; 代替.

Answering the second question requires an understanding of relatively positioned elements. Elements that are positioned relatively appear in a position relative to where they would normally appear on the page. When you assign values to top and left you're not giving absolute values. You're giving positions that are treated as relative to the element's original position in the layout. This is a feature of relative positioning, not a bug. You probably want to use position: absolute; instead.

這篇關(guān)于在 JavaScript 中相對定位的元素上實(shí)現(xiàn)拖放的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認(rèn)為文檔“準(zhǔn)備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發(fā)技術(shù)
What do jasmine runs and waitsFor actually do?(jasmine 運(yùn)行和等待實(shí)際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈?zhǔn)椒椒ㄟM(jìn)行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監(jiān)視函數(shù)中的函數(shù)調(diào)用?)
主站蜘蛛池模板: 国产在线观看 | 久久伦理电影 | 亚洲国产精品人人爽夜夜爽 | 中文字幕av网 | 九九99靖品 | 精品一区二区三区在线视频 | 美国黄色毛片 | 99久久久99久久国产片鸭王 | 国产日韩欧美 | 国产精品福利在线 | 国产伊人精品 | 91精品导航| 日韩欧美精品在线 | 欧美成人免费在线视频 | 日韩欧美国产精品一区二区 | 国产aⅴ| 国产一级淫片a直接免费看 免费a网站 | 毛片久久久 | 欧美一区免费在线观看 | 久久成人精品视频 | 日韩欧美一区二区三区四区 | 91视视频在线观看入口直接观看 | 久久亚洲经典 | 日本一区二区三区精品视频 | 久久久久国产一区二区三区 | 欧美一级片在线看 | 日韩精品久久一区二区三区 | 高清av电影 | 日韩成人 | 亚洲综合无码一区二区 | 99riav国产一区二区三区 | 久久久久久国产精品免费 | 午夜欧美一区二区三区在线播放 | 久久一级免费视频 | 亚洲一区导航 | 九九九视频精品 | 国产精品一区久久久 | 国产精品亚洲综合 | 毛片软件| 亚洲精品一区二区三区在线 | 中国一级特黄视频 |