0
A
回答
1
爲什麼不能簡單地通過jQuery的使用拖放:
<script>
$(function() {
$("#draggable").draggable();
});
</script>
+0
actully我擁有的是這個我因爲,我的老師希望我們用這樣的東西做這件事: 但我不能完成我的功能,如果你能幫助我,這是一個初學者,這將是好事。
1
我已經把一個簡單的工作示例定義了Draggable
對象。您可以指定拖動項目(您正在四處移動的元素)以及拖動邊界(空間或元素)(您正在移動項目)。如果您想要將可拖動項目限制在頁面上的某個空間(例如容器)上,或者定義數學基礎的相對座標系統,則邊界的概念非常重要。
我的解決方案是不是最快的,但它證明了這一概念:
$(function() {
window.mousedown = 0;
$(window).on('mousedown mouseup', function(e) {
if(e.type == 'mousedown') { this.mousedown++; }
else { this.mousedown--; }
});
var Draggable = function(dragItem, dragBoundary) {
this.item = $(dragItem).css('position', 'absolute');
this.item.on('mousemove', $.proxy(this.handleDragEvent, this));
this.boundary = $(dragBoundary).css('position', 'relative');
};
Draggable.prototype.handleDragEvent = function(e) {
if(window.mousedown) {
var mousePosition = this.mapToBoundary([e.clientX, e.clientY]);
var mouseX = mousePosition[0],
mouseY = mousePosition[1];
if(typeof this.prevMouseX == "undefined") this.prevMouseX = mouseX;
if(typeof this.prevMouseY == "undefined") this.prevMouseY = mouseY;
this.itemX = this.item.offset().left - this.boundary.offset().left;
this.itemY = this.item.offset().top - this.boundary.offset().top;
var deltaX = mouseX - this.prevMouseX,
deltaY = mouseY - this.prevMouseY;
this.item.css({
'left': this.itemX + deltaX,
'top': this.itemY + deltaY
});
this.prevMouseX = mouseX;
this.prevMouseY = mouseY;
}
};
Draggable.prototype.mapToBoundary = function(coord) {
var x = coord[0] - this.boundary.offset().left;
var y = coord[1] - this.boundary.offset().top;
return [x,y];
};
var draggable = new Draggable($('.draggable'), $('.container'));
});
注意,我們維持對全球一mousedown
價值,使我們能夠確定何時是適當的拖動我們的元素(我們只添加一個mousemove
偵聽器到拖動項目本身)。我還在邊界div
之上加入了一個間隔div
,以演示如何在頁面的任何位置移動邊界,並且座標系統仍然精確。代碼實際上限制在其分配的邊界內的可拖動項目可以使用簡單的數學來書寫。
這裏是小提琴:http://jsfiddle.net/bTh9s/3/
編輯:
這裏開始到一些代碼,限制其容器內的可拖動項目。
Draggable.prototype.restrictItemToBoundary = function() {
var position = this.item.position();
position.right = position.left + this.item.outerWidth();
position.bottom = position.top + this.item.outerHeight();
if(position.left <= 0) {
this.item.css('left', 1);
} else if(position.right >= this.boundary.outerWidth()) {
this.item.css('left', this.boundary.outerWidth() - this.item.outerWidth());
}
if(position.top <= 0) {
this.item.css('top', 1);
} else if(position.bottom >= this.boundary.outerHeight()) {
this.item.css('top', this.boundary.outerHeight() - this.item.outerHeight());
}
};
此方法應該在更新拖動項目的CSS定位之後在Draggable.handleDragEvent中調用。看來這個解決方案很糟糕,但這是一個開始。
相關問題
- 1. 頸後跟隨着鼠標
- 2. 鼠標點擊標籤
- 3. CodedUi:鼠標點擊座標
- 4. 釋放鼠標捕獲並讓鼠標點擊通過
- 5. MATLAB如何讓鼠標點擊座標
- 6. Powershell,鼠標點擊
- 7. SeaDragon鼠標點擊
- 8. 鼠標點擊jquery
- 9. 鼠標點擊JTable
- 10. 鼠標點擊java
- 11. 鼠標點擊事件 - 在Firefox上的鼠標座標
- 12. 如何讓鼠標點擊後懸停?
- 13. 鼠標點擊後防止鼠標輸入事件
- 14. 區分機器人鼠標點擊和人類鼠標點擊
- 15. 如何在鼠標點擊時淡出鼠標,當鼠標被點擊並拖動然後熄滅?
- 16. 在網站上跟蹤鼠標點擊
- 17. 跟蹤鼠標光標android
- 18. 區分鼠標雙擊和鼠標點擊wpf
- 19. 單擊鼠標2次鼠標單擊後發生更改
- 20. 鼠標點擊原點
- 21. HTML5帆布遊戲將鼠標點擊到鼠標點。
- 22. 在NetLogo中檢測鼠標點擊/鼠標上移
- 23. 如何在鼠標點擊時獲得鼠標位置 - Python Gtk
- 24. 如何讓鼠標光標在鼠標上抓取?
- 25. 讓鼠標點擊screenX和screenY在AS3
- 26. 在C#中單擊鼠標獲取鼠標座標
- 27. jquery圖像跟隨鼠標點擊時
- 28. 鼠標座標
- 29. 光標在鼠標點擊更改
- 30. 將鼠標懸停在鼠標上
「當它在mousedown上時,當它在mouseup上時,它保持在它的最後位置。」你能澄清你的意思嗎? –
對不起,當它在mousedown上的div時,當它在鼠標上時,它在最後一個位置上是saty,它是 – user3308331
「當它在mousedown時休眠div」 - 因此,頁面上的div跟隨光標,因爲鼠標移動,當鼠標按鈕關閉? –