简体   繁体   English

可拖动div背景在div1以上时为一种颜色,在div2以上时为另一种颜色

[英]Draggable div background to be one colour when over div1 and then another when over div2

I have a draggable div which is initially positioned over a blue header so has a white background with blue text and white border so it stands out. 我有一个可拖动的div,它最初位于蓝色标题上方,因此具有带有蓝色文本和白色边框的白色背景,因此非常醒目。 When it is dragged over the rest of the page which has a white background I would like the background to become blue, text white and border blue. 当将其拖到具有白色背景的页面其余部分上时,我希望背景变为蓝色,文本为白色和边框为蓝色。

The draggable div is positioned using absolute so is actually placed outside of the header element so it would need to detect what area of the page it is over. 可拖动的div使用绝对位置定位,因此实际上放置在标头元素的外部,因此它需要检测它在页面的哪个区域。

Can this be done using CSS or Javascript / jQuery? 可以使用CSS或Javascript / jQuery完成此操作吗?

In JQuery I imagine you could catch the mouseOver event ( https://api.jquery.com/mouseover/ ) and when it is triggered you would see with some check if there is a simulataneous drag happening meanwhile and if it is happening, change the colors of the element where the mouseOver event took place. 在JQuery中,我想您可以捕获mouseOver事件( https://api.jquery.com/mouseover/ ),并且在触发该事件时,您将通过一些检查查看是否同时发生了同时拖动,如果发生了拖动,请进行更改mouseOver事件发生位置的元素的颜色。

important parts of code: 代码的重要部分:

HTML: HTML:

<div draggable="true" class="bar">Drag me!</div>     

<div id="foo"><p>To be dragged over.</div>      

JQuery: At mouseOver see if another element is dragged on the moment: jQuery:在mouseOver上,看看是否此时拖动了另一个元素:

$( '#foo' ).mouseover(function() {
   if ($('.bar').is('.ui-draggable-dragging')) { return; // change color in here. }

}); });

NOTE: You have to have this attached by id, at least I couldn't imagine how you otherwise apply color change to correct element. 注意: 您必须将此ID附加起来,至少我无法想象您如何将颜色更改应用于正确的元素。 Color change can be a function, though, and take in element. 颜色变化可以是一种功能,并且可以作为要素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM