【发布时间】:2010-04-29 12:09:56
【问题描述】:
我想知道是否可以借助鼠标坐标设置背景颜色。
我拥有的是:
我有一个可拖动的 DIV-A 和一些其他可放置的 div。
我需要的是:
每当我的 DIV-A 越过它们时,我需要突出显示页面上可放置的其他 div。我所拥有的是鼠标坐标,是否可以使用 jquery 在鼠标坐标的基础上应用 css。
【问题讨论】:
标签: javascript jquery css jquery-ui
我想知道是否可以借助鼠标坐标设置背景颜色。
我拥有的是:
我有一个可拖动的 DIV-A 和一些其他可放置的 div。
我需要的是:
每当我的 DIV-A 越过它们时,我需要突出显示页面上可放置的其他 div。我所拥有的是鼠标坐标,是否可以使用 jquery 在鼠标坐标的基础上应用 css。
【问题讨论】:
标签: javascript jquery css jquery-ui
以下内容可能会起作用。您可能需要处理窗口的 scrollLeft 和 scrollTop 以使其完美。您可能也想要throttle 和memoize(如果放置位置不变)。
此外,还可以通过缓存offset(),仅在需要时绑定mousemove,并通过调整each 循环以利用优化循环(例如for(var i=droppables.length;i>-1;){var self = droppables.eq(--i);...})来调整它的更多性能。
还请注意,这只会在鼠标经过它们时更改 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", "");
}
});
});
【讨论】:
我在这里为您发布了demo。基本上这会在每个可放置位置循环,所以如果你有很多它们,它可能真的会减慢鼠标移动。
哦,我添加了两个变量,如果你想增加与 droppable 的接近度,你可以调整。根据需要调整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');
}
});
});
});
【讨论】:
drop.eq(i) 对吗?如果有很多放置目标,它可能会明显更快。
.eq(i)。我测试了它,当我用 10 个掉落目标测试它时它会稍微快一点,谢谢!哦,它看起来也更干净:)
$(this)(并缓存它)而不是drop.eq(i)。我怀疑除非你有 1000 个 dropables,否则会有任何明显的加速......但它是一种干净的做事方式,并鼓励那些刚接触 jQuery 的人正确缓存他们的 dom 查找。
$(this) 而不是 drop.eq(i) 并将该类应用于所有下拉框,所以我离开了它,它一定是初始代码格式.. .现在改回来。但我没有理由缓存它。
查看“视觉反馈”示例 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");
});
【讨论】: