[英]change background color with change in mouse position
我想知道是否可以借助鼠標坐標來設置背景色。
我所擁有的是:
我有一個可拖動的DIV-A和一些其他可拖放的div。
我需要的是:
每當我的DIV-A越過它們時,我需要突出顯示頁面上其他可拖放的div。 我所擁有的是鼠標坐標,是否可以使用jquery在鼠標坐標的基礎上應用css。
類似以下內容可能會起作用。 您可能需要處理窗口的scrollLeft和scrollTop才能使其完美。 你可能會想扼殺和memoize的太(如果降落位置不改變)它。
同樣,可以通過以下方式來調整性能:緩存offset()
,僅在需要時綁定mousemove
,並調整each
循環以利用優化的循環(例如, for(var i=droppables.length;i>-1;){var self = droppables.eq(--i);...}
)。
還要注意,這只會在鼠標經過div時改變div的顏色...不一定在可拖動鼠標經過它們時會改變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", "");
}
});
});
我在這里為您發布了一個演示 。 基本上,這會循環遍歷每個可放置的位置,因此,如果有很多可放置的位置,則可能會減慢鼠標的移動速度。
哦,我添加了兩個變量,可以增加對可放置點的接近度。 根據需要調整xmargin
和ymargin
變量。
的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');
}
});
});
});
看看jQuery UI上的“可視反饋”示例,正如gmcalab所提到的,如果僅使用類作為選擇器,則沒有ID不會成為問題。 抱歉,如果我沒有正確閱讀此內容。
將selector
和selector2
聲明為您想要的任何東西...
$(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);
});
您可以為此使用.hover(),因此,當鼠標位於div上方時,請更改其背景色:
$("yourdiv").hover(function () {
$(this).css("background-color", "#ff0000");
},
function () {
$(this).css("background-color", "#ffffff");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.