admin管理员组文章数量:1291320
i'm about to write my own interactive Tutorial with HTML, Javascript and css. Now i had the idea to create an DIV-element that represents an glowing rectangle to mark specific areas. The problem is when I change the position of the glow-rectangle, to highlight an element that is clickable, the glow rectangle is overlapping the clickable element. So the clickable element is no longer clickable. Is there a way to "disable" the glow-rectangle so it's visible but no longer blocking the underlaying element?
Here is my code:
<div id="hauptteil">
<div id="block" onclick="step2();" style="cursor: pointer">
</div>
<div id="glowDiv">
<div id="glow" class="glow"><img src="images/transBlock.png"/></div>
</div>
</div>
Here is the css code:
#glowDiv {
width: 200px;
height: 100px;
position: absolute;
top: 200px;
left: 200px;
}
.glow {
margin: auto;
width: 147px;
height: 90px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
-webkit-box-shadow: 0px 0px 15px 5px rgba(255, 255, 190, .75);
-moz-box-shadow: 0px 0px 15px 5px rgba(255, 255, 190, .75);
box-shadow: 0px 0px 15px 5px rgba(255, 255, 190, .75);
}
#block {
width: 50px;
height: 50px;
background: red;
}
To visualize my problem. On my page I have a DIV-Element that is visible as a red block. When I start my tutorial, I change the position of my glow-DIV to cover the red block. So it seems that the red block is glowing.
i'm about to write my own interactive Tutorial with HTML, Javascript and css. Now i had the idea to create an DIV-element that represents an glowing rectangle to mark specific areas. The problem is when I change the position of the glow-rectangle, to highlight an element that is clickable, the glow rectangle is overlapping the clickable element. So the clickable element is no longer clickable. Is there a way to "disable" the glow-rectangle so it's visible but no longer blocking the underlaying element?
Here is my code:
<div id="hauptteil">
<div id="block" onclick="step2();" style="cursor: pointer">
</div>
<div id="glowDiv">
<div id="glow" class="glow"><img src="images/transBlock.png"/></div>
</div>
</div>
Here is the css code:
#glowDiv {
width: 200px;
height: 100px;
position: absolute;
top: 200px;
left: 200px;
}
.glow {
margin: auto;
width: 147px;
height: 90px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
-webkit-box-shadow: 0px 0px 15px 5px rgba(255, 255, 190, .75);
-moz-box-shadow: 0px 0px 15px 5px rgba(255, 255, 190, .75);
box-shadow: 0px 0px 15px 5px rgba(255, 255, 190, .75);
}
#block {
width: 50px;
height: 50px;
background: red;
}
To visualize my problem. On my page I have a DIV-Element that is visible as a red block. When I start my tutorial, I change the position of my glow-DIV to cover the red block. So it seems that the red block is glowing.
Share Improve this question asked Aug 30, 2013 at 12:53 Kai R.Kai R. 1852 gold badges3 silver badges11 bronze badges 3- 2 Instead of trying to overlap elements, why not apply the additional glow styles to the active div? – Brian Flanagan Commented Aug 30, 2013 at 12:58
- possible duplicate stackoverflow./questions/1009753/… – Michael Blancaflor Commented Aug 30, 2013 at 13:01
- I thought so it would be easy to highlight specific areas. But maybe your suggestion is the better idea. – Kai R. Commented Aug 30, 2013 at 13:17
4 Answers
Reset to default 5Back in 2013 you had to use a script to solve this problem, but nowadays, pointer-events
is widely supported. Use it!
Normally you would just user the pointer-events
css property. Guess what? IE doesn't support it... With a little help from our friend jquery we can emulate this however!
My approach is to capture the click event in the glow (the absolute positioned div) and check if there are any other elements overlapping the x-y coordinate of the mouse click.
Warning: jQuery ahead!
First just capture the click event:
$('.glow').on('click', function(e) { // our trick here });
Then determine the coordinates of the mouse cursor. Quite easy as jQuery helps us out by giving the coordinates in the event object
var cursorX = e.pageX;
var cursorY = e.pageY;
Now we need to traverse all dom elements that are possibly sitting below our glow div that we want to receive the click event as well.
$('.class-possibly-below').each(function(index, element) { // our calculations here });
If we can get the position relative to the window and it's width and height we can calculate if our cursor was over it while clicking. In that case our cursorX and cursorY values would be in between the left-position
and left-position + width
(and top- and height respectively) of our DOM element we're traversing. Yet again jQuery helps a bit:
var offset = $(element).offset();
xRangeMin = offset.left;
xRangeMax = offset.left + $(element).outerWidth();
We use offset()
instead of position()
because the latter returns the position relative to it's parent. And we use the outerWidth()
and outerHeight()
instead of width()
and height()
respectively because the former also includes padding and border.
Now check if the x and y coordinates are in between the range-min and range-max. If so, trigger the click event on this element.
if(cursorX >= xRangeMin && cursorX <= xRangeMax && ...) {
$(element).trigger('click');
}
You want some action? Check out this fiddle!
As I mentioned in my ment, a different approach would be to add the glow class to the active div:
<div id="hauptteil">
<div id="block" onclick="step2();" class="glow">
</div>
#block {
width: 50px;
height: 50px;
background: red;
}
.glow {
cursor:pointer;
-webkit-box-shadow: 0px 0px 15px 5px rgba(255, 255, 190, .75);
-moz-box-shadow: 0px 0px 15px 5px rgba(255, 255, 190, .75);
box-shadow: 0px 0px 15px 5px rgba(255, 255, 190, .75);
}
You can use pointer-events: none;
to ignore the possible click event of an element. However as stated in the ments the method you are doing does not seem to be the best of options.
#glowDiv {
width: 200px;
height: 100px;
position: absolute;
top: 200px;
left: 200px;
pointer-events: none;
}
The pointer-events property allows for control over how HTML elements respond to mouse/touch events – including CSS hover/active states, click/tap events in Javascript, and whether or not the cursor is visible.
Placing this on your overlaying element will make it transparent to click events.
However this does not work in IE
This is my suggestion am curious if anyone else has another option. I am using the same thing on my website and simply ignoring the overlay on IE using conditional tags.
Here's a simple pin you can check it out http://codepen.io/ElmahdiMahmoud/pen/Ewlto hope this can help!
本文标签: javascriptOverlapping DIV Elements and onclickStack Overflow
版权声明:本文标题:javascript - Overlapping DIV Elements and onclick - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741529803a2383687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论