2010-04-29 73 views
1

我想知道是否可以在鼠標座標的幫助下設置背景顏色。更改背景顏色,改變鼠標位置

我有的是:

我有一個DIV-A是拖動和其他一些的div它們投擲的。

我需要的是:

我需要強調我的網頁,其中有可放開,每當我的DIV-A經過它們對其他申報單。我有什麼是鼠標座標,是否有可能應用CSS的鼠標座標使用jQuery的基礎上。

回答

-1

您可以使用.hover()對於這一點,所以當鼠標在DIV,改變它的背景色:

$("yourdiv").hover(function() { 
    $(this).css("background-color", "#ff0000"); 
    }, 
    function() { 
    $(this).css("background-color", "#ffffff"); 
}); 
+0

因爲downvoter未加評論,我會:這不會起作用,因爲 「yourdiv」 會不會「徘徊」是因爲它被可拖動的元素阻塞。 – 2010-04-29 12:27:47

0

聲明selectorselector2到任何你想要的......

$(selector).mousemove(function(event) { 

    // Set some bounds, these are arbitrary here not sure what sort of area your looking for... 
    var lowerXBound= 0, 
     upperXBound = 100, 
     lowerYBound = 0, 
     upperYBound = 100, 
     currentX = event.pageX, 
     currentY = event.pageY; 

    var color = currentX > lowerXBound && currentX < upperXBound && currentY > lowerYBound && currentY < upperYBound ? 'red' : 'green'; 

    $(selector2).css('background-color', color); 
}); 
+1

啊,你打我吧......是啊。我們都在同一軌道上......但我認爲我的答案可能會更好地涵蓋多個可投放的對象。你怎麼看? – 2010-04-29 12:21:23

+0

但我沒有可拖動div的ID,因爲它們是動態創建的 – 2010-04-29 12:21:48

+1

@David Murdoch,我也很喜歡你的解決方案,很好的一個人。 – Gabe 2010-04-29 12:25:31

4

像下面這樣的東西可能會起作用。您可能需要處理窗口的scrollLeft和scrollTop以使其完美。你可能會想要throttlememoize(如果放置位置不變)。

另外,一些更性能可以通過緩存offset(),僅在需要時結合mousemove,並通過調整each循環利用的優化循環(例如for(var i=droppables.length;i>-1;){var self = droppables.eq(--i);...})進行調整出來。

還要注意,這隻會在MOUSE通過它們時更改div的顏色......不一定當可拖動的文件經過它們時......這會使事情變得更加複雜一些,但下面的函數應該會向您發送在正確的方向。

$(document).mousemove(function(e){ 
    // this should be throttled... 
    var x = e.pageX, 
     y = e.pageY; 
    // this loop could be optimized... 
    $("div.droppables").each(function(){ 
     // these vars could be memoized... 
     var self = $(this), 
      divL = self.offset().left, 
      divT = self.offset().top, 
      divR = self.width() + divL, 
      divB = self.height() + divT; 
     // if the MOUSE coords are between the droppable's coords 
     // change the background color 
     if(x >= divL && x <= divR && y >= divT && y <= divB){ 
       self.css("background", "red"); 
     } 
     else{ 
       // reset the background color 
       self.css("background", ""); 
     } 
    }); 
}); 
+0

我不得不問...爲什麼downvote?請告訴: – 2010-04-29 12:31:21

+0

@大衛默多克:得到錯誤'缺少變量名稱' – 2010-04-29 12:37:18

+0

我剛測試過它。沒有錯誤。我已經更新了一些代碼;嘗試一下。此外,張貼您實際使用的代碼。 – 2010-04-29 12:41:50

1

看一看「視覺反饋」在 jQuery UI品嚐過,並作爲gmcalab提到的,沒有標識不是一個問題,如果你只使用一個類的選擇。對不起,如果我沒有正確閱讀這個。

+0

很好,我不知道這個! – 2010-04-29 12:45:48

2

我在這裏爲您發佈了demo。基本上這是循環遍歷每個可丟棄的位置,所以如果你有很多它們,它可能真的減緩了鼠標移動。

哦,我添加了兩個變量,您可以調整,如果你想增加接近droppable。根據需要調整xmarginymargin變量。

CSS

.draggable { width: 90px; height: 90px; padding: 0.5em; position: relative; top: 0; left: 0; z-index: 2; } 
.droppable { width: 120px; height: 120px; padding: 0.5em; position: absolute; z-index: 1; } 
#drop1 { top: 150px; left: 300px; } 
#drop2 { top: 400px; left: 100px; } 

HTML

<div class="draggable ui-widget-content"> 
    <p>Drag me to my target</p> 
</div> 

<div id="drop1" class="droppable ui-widget-header"> 
    <p>Drop here</p> 
</div> 

<div id="drop2" class="droppable ui-widget-header"> 
    <p>Drop here</p> 
</div> 

腳本

$(function(){ 
var xmargin = 10, 
    ymargin = 10, 
    drag = $('.draggable'), 
    drop = $('.droppable'), 
    dgw = drag.outerWidth() + xmargin, 
    dgh = drag.outerHeight() + ymargin, 
    pos = []; 

drop 
    .droppable({ 
    //hoverClass: 'ui-state-active', 
    drop: function(event, ui) { 
    $(this).addClass('ui-state-highlight').find('p').html('Dropped!'); 
    } 
    }) 
    // set up droppable coordinates array (left, top, right, bottom) for each element 
    .each(function(i){ 
    var dropzone = drop.eq(i); 
    var l = dropzone.position().left, 
    t = dropzone.position().top, 
    r = l + dropzone.outerWidth() + xmargin, 
    b = t + dropzone.outerHeight() + ymargin; 
    pos.push([l,t,r,b]); 
    }); 

drag 
    .draggable() 
    // bind to drag event, or this could be placed inside the draggable function 
    .bind("drag", function(event,ui){ 
    var l = ui.offset.left, 
     t = ui.offset.top; 
    // cycle through each droppable and compare current postion to droppable array 
    drop.each(function(i){ 
    if ((l + dgw) > pos[i][0] && l < pos[i][2] && (t + dgh) > pos[i][1] && t < pos[i][3]) { 
    $(this).addClass('ui-state-active'); 
    } else { 
    $(this).removeClass('ui-state-active'); 
    } 
    }); 
    }); 

}); 
+0

+1。這幾乎就是我在我的回答中建議的實現......只需要添加一些限制;另外,改變一個元素的類可能會比使用javascript直接應用樣式要慢(取決於元素的數量和選擇器的複雜性)。ps:你知道你可以做drop。 eq(i)'對嗎?如果有很多放置目標,它可能會明顯加快。 – 2010-04-29 17:25:08

+0

@David:是的,我忘了'.eq(i)'。我測試了它,目標,謝謝!哦,它看起來更乾淨:) – Mottie 2010-04-29 17:44:41

+0

儘可能使用'$(this)'(並緩存它)而不是'drop.eq(i)'。我懷疑除非你有1000個dropables,否則會有明顯的加速......但它是一種乾淨的做事方式,鼓勵那些新來的jQuery來正確緩存他們的dom查找。 – 2010-04-29 17:53:30