任何人都可以告訴我如何將點擊功能分配給元素中的特定座標嗎?在JQuery中使用鼠標位置設置點擊事件
回答
$(document).click(function(event) {
var top = 0,
right = 200,
bottom = 200,
left = 0;
var x = event.pageX;
var y = event.pageY;
if ((x >= left && x <= right) && (y >= top && y <= bottom))
{
// do stuff if within the rectangle
}
});
這似乎工作正常,但有沒有辦法輕鬆定義可點擊區域的小寬度和高度? 定義可點擊區域的位置有一些幫助嗎? –
您可以使用Jquery的$(...)。offset()方法來獲取元素的絕對x和y。 所以像左= $(elm).offset()。left,top = $(elm).offset()。top;然後爲邊界框的其他邊添加所需的寬度和高度。 – Supericy
那會是if語句嗎? –
如果您只想讓部分元素響應點擊(這是我如何閱讀您的問題),您可以通過查看點擊發生的位置來做到這一點。
jQuery(function($) {
$("#target").click(function(event) {
var $this = $(this),
width = $this.width(),
pos = $this.offset(),
x = event.pageX - pos.left,
y = event.pageY - pos.top;
display("Click was at (" + x + "," + y + ") in the element");
if (x > (width/2)) {
display("Clicked in the right half");
}
});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
event.pageX
和event.pageY
是文件座標,這是什麼offset
功能爲您提供的元素。 (儘管名字,offset
不給你的偏移量相對於元素的偏移父母;這是position
奇怪但卻真實。)所以你可以翻譯event.pageX
和event.pageY
至元素座標通過簡單地從event.pageY
減去event.pageX
pos.left
和pos.top
。
jquery中的每個事件都有pageX
和pageY
屬性。所以,如果你觸發一個事件,你可以檢查座標:
$('#elem').click(function(e)) {
var x = e.pageX;
var y = e.pageY;
if (x > x1 && x < x2 && y > y1 && y < y2) {
do_something
}
}
在這種情況下x1
,x2
,y1
,y2
是矩形的座標。
pageX
,pageY
是頁面座標,如果你需要一個元素中的相關元素,你需要獲取元素在頁面上的位置,然後根據元素位置計算真實座標。如上所述
直播例如http://jsfiddle.net/LBKTe/1
基本上同樣的事情AlecTMH。
// top left and botom right corners of the clickable area
var x1 = 10, x2 = 30, y1 = 10, y2 = 30;
$(document).on('click', '#block', function(e) {
// get x/y coordinates inside
var cx = e.clientX;
var cy = e.clientY;
offset = $(this).offset();
x = cx-offset.left;
y = cy-offset.top;
// if our click coordinates are within constrains, show an alert
if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {
alert('click!');
}
});
你好演示http://jsfiddle.net/gYkXS/3/
希望這有助於,有一個很好的一個!乾杯!
代碼
$(document).ready(function(){
$("#foo").mousemove(function(e){
window.xPos = e.pageX;
window.yPos = e.pageY;
alert("Position X = " + e.pageX + "Position y = " + e.pageY);
});
});
如果僅僅有少數的「感興趣的領域」,你能避免整個元素上捕捉鼠標點擊通過疊加所需大小的一個或多個空元素用position: absolute
風格和高z-index
,即:
.hotspot {
position: absolute;
z-index: 1000;
cursor: pointer;
cursor: hand;
}
div {
position: relative;
}
canvas {
background-color: #f0f0f0;
}
<div class="frame">
<canvas width="400" height="400"> </canvas>
<div class="hotspot" style="left: 100px; top: 100px; width: 40px; height: 40px">
</div>
</div>
不幸的是,我需要那裏沒有點擊功能的元素,否則它會中斷其他功能,謝謝你的幫助。 –
- 1. 如何使用jQuery設置按鈕點擊事件的位置?
- 2. CSS設置元素位置到鼠標位置上的事件
- 3. 在鼠標點擊事件上的文本框末尾的鼠標位置
- 4. 在事件中獲取鼠標位置
- 5. 使用鼠標'點擊'而不是'鼠標懸停'事件,jquery
- 6. jQuery鼠標位置和調用動態創建元素的點擊事件
- 7. 獲取鼠標點擊事件的位置從一個JButton
- 8. 根據點擊位置放棄NSWindow上的鼠標事件
- 9. 如何使用Perl在GTK窗口中點擊鼠標位置?
- 10. 使用鼠標單擊/事件獲取像素位置
- 11. Pygame-雪碧設置位置與鼠標點擊
- 12. jQuery鼠標位置
- 13. 根據用戶點擊的位置獲取鼠標位置
- 14. 設置鼠標位置
- 15. 設置鼠標位置
- 16. 如何在graphsharp中的特定頂點上設置鼠標點擊事件?
- 17. 捕捉鼠標位置(不點擊)
- 18. C#WebBrowser獲取鼠標點擊位置
- 19. openGL 2D鼠標點擊位置
- 20. 瀏覽器鼠標位置點擊
- 21. 檢測鼠標點擊位置
- 22. 鼠標點擊圖片上的位置
- 23. 使用jquery點擊重置事件?
- 24. HighStock HighCharts點擊事件設置標記
- 25. 如何在鼠標點擊時獲得鼠標位置 - Python Gtk
- 26. 如何檢測jQuery中的鼠標點擊位置
- 27. JQuery鼠標懸停和點擊事件
- 28. AppleScript單擊鼠標位置
- 29. 確定鼠標點擊位置的位置
- 30. 在鼠標點擊獲取ms圖表中的標記位置
可能的重複http://stackoverflow.com/questions/2845178/triggering-a-javascript-click-event-at-specific-coordinates –
不是重複的 - 如果我正確理解他,OP想要_register_一個特定位置的處理程序,而不是假的事件。 – Alnitak
用什麼粒度 - 一個矩形,一個像素,其他的東西? – Alnitak