admin管理员组文章数量:1427288
I was wondering if it is possible to set background-color with help of mouse coordinates.
What i have is:
I have a DIV-A which is draggable and some other divs which are droppable.
What i need is :
I need to highlight other divs on my page which are droppable, whenever my DIV-A passes over them. What i have is mouse coordinates, is it possible to apply css on the bases of mouse coordinates using jquery.
I was wondering if it is possible to set background-color with help of mouse coordinates.
What i have is:
I have a DIV-A which is draggable and some other divs which are droppable.
What i need is :
I need to highlight other divs on my page which are droppable, whenever my DIV-A passes over them. What i have is mouse coordinates, is it possible to apply css on the bases of mouse coordinates using jquery.
Share Improve this question edited Apr 29, 2010 at 12:59 Ashish Rajan asked Apr 29, 2010 at 12:09 Ashish RajanAshish Rajan 1,2005 gold badges15 silver badges27 bronze badges5 Answers
Reset to default 4Something like the following may work. You will probably need to deal with window's scrollLeft and scrollTop to get it perfect. You will probably want to throttle and memoize (if the drop positions don't change) it too.
Also, some more performance can be tweaked out of it by caching offset()
, only binding mousemove
when needed, and by tweaking the each
loop to utilize an optimized loop (e.g. for(var i=droppables.length;i>-1;){var self = droppables.eq(--i);...}
).
Also note that this will only change the color of the divs when the MOUSE passes over them...not necessarily when the draggable passes over them...this makes things a little more plicate but the function below should send you in the right direction.
$(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", "");
}
});
});
I posted a demo for you here. Basically this cycles through each droppable position, so if you have a lot of them, it could really slow down mouse movement.
Oh, and I added two variables you can adjust if you want to increase the proximity to the droppable. Adjust the xmargin
and ymargin
variables as desired.
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>
Script
$(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 pare 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');
}
});
});
});
Have a look at the "Visual feedback" sample over at jQuery UI, and as gmcalab mentioned, not having IDs is not an issue if you just use a class as the selector. Sorry if I'm not reading this correctly.
Declare selector
and selector2
to whatever you want...
$(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);
});
You can use .hover() for this, so when the mouse is over the div, change it's background colour:
$("yourdiv").hover(function () {
$(this).css("background-color", "#ff0000");
},
function () {
$(this).css("background-color", "#ffffff");
});
本文标签: javascriptchange background color with change in mouse positionStack Overflow
版权声明:本文标题:javascript - change background color with change in mouse position - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745496811a2660842.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论