javascript - 如何在通过窗口对象退出元素时单击未完全覆盖背景并在悬停时消失的对象

标签 javascript jquery css

This question is related to but not the same as How to click through an object that doesn't completely cover the background and disappear on hover

IMPORTANT - Please take a look at the question above which contains background info, or you will most likely not understand what this question is about

如果您阅读了上面的问题,您就会明白接受的答案是有效的,即:

$(function() {

var box = $('#box_e'),
w = box.outerWidth(),
h = box.outerHeight(),
xmin = box.offset().left,
xmax = xmin + w,
ymin = box.offset().top,
ymax = ymin + h;

box.mouseenter(function() {

    $(this).fadeTo(0,0).addClass('noevents');
});

$('.box').mousemove(function(e) {

    if (elementOut(e) && box.hasClass('noevents')) {
    box.fadeTo(0,0.5).removeClass('noevents');
    }
});

function elementOut(e) {

var x = e.pageX, y = e.pageY,
onelement = x >= xmin && x <= xmax && y >= ymin && y <= ymax;

return !onelement;
}
});

当然,除非你有一些背景知识,否则所有这些都是毫无意义的。最初的问题是,“如何点击一个没有完全覆盖背景并在悬停时消失的对象”,这意味着每当你将鼠标悬停在这个 div 上时,它就会消失,你将能够触发隐藏在对象后面的事件。

这个小片段非常强大,这是一个演示:

$(function() {

var box = $('#box_e'),
w = box.outerWidth(),
h = box.outerHeight(),
xmin = box.offset().left,
xmax = xmin + w,
ymin = box.offset().top,
ymax = ymin + h;

box.mouseenter(function() {

	$(this).fadeTo(0,0).addClass('noevents');
});

$('.box').mousemove(function(e) {

	if (elementOut(e) && box.hasClass('noevents')) {
	box.fadeTo(0,0.5).removeClass('noevents');
	}
});

function elementOut(e) {

var x = e.pageX, y = e.pageY,
onelement = x >= xmin && x <= xmax && y >= ymin && y <= ymax;

return !onelement;
}
});
#box_a, #box_b, #box_c, #box_d {
  height: 100px;
  width: 100px;
  position: absolute;
}

#box_a {
  top: 0px;
  left: 0px;
  background-color: red;
}

#box_b {
  top: 0px;
  left: 100px;
  background-color: blue;
}

#box_c {
  top: 100px;
  left: 0px;
  background-color: yellow;
}

#box_d {
  top: 100px;
  left: 100px;
  background-color: green;
}

#box_e {
  width: 100px;
  height: 100px;
  position: absolute;
  top: 50px;
  left: 50px;
  background-color: black;
  opacity: .5;
}

.noevents {
  pointer-events: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box_a" class="box"></div>
<div id="box_b" class="box"></div>
<div id="box_c" class="box"></div>
<div id="box_d" class="box"></div>
<div id="box_e"></div>

<!--
red   ,  blue
yellow, green
-->

完美地描述和回答了问题。但是有一个小错误。

考虑这个新片段:

$(function() {

var box = $('#box_e'),
w = box.outerWidth(),
h = box.outerHeight(),
xmin = box.offset().left,
xmax = xmin + w,
ymin = box.offset().top,
ymax = ymin + h;

box.mouseenter(function() {

	$(this).fadeTo(0,0).addClass('noevents');
});

$('.box').mousemove(function(e) {

	if (elementOut(e) && box.hasClass('noevents')) {
	box.fadeTo(0,0.5).removeClass('noevents');
	}
});

function elementOut(e) {

var x = e.pageX, y = e.pageY,
onelement = x >= xmin && x <= xmax && y >= ymin && y <= ymax;

return !onelement;
}
});
#box_a, #box_b, #box_c, #box_d {
  height: 100px;
  width: 100px;
  position: absolute;
}

#box_a {
  top: 0px;
  left: 0px;
  background-color: red;
}

#box_b {
  top: 0px;
  left: 100px;
  background-color: blue;
}

#box_c {
  top: 100px;
  left: 0px;
  background-color: yellow;
}

#box_d {
  top: 100px;
  left: 100px;
  background-color: green;
}

#box_e {
  width: 100px;
  height: 100px;
  position: absolute;
  top: 50px;
  left: 50px;
  background-color: black;
  opacity: .5;
}

.noevents {
  pointer-events: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box_a" class="box"></div>
<!-- <div id="box_b" class="box"></div> -->
<div id="box_c" class="box"></div>
<div id="box_d" class="box"></div>
<div id="box_e"></div>

<!--
red   ,  blue
yellow, green
-->

请注意,这是同一件事,但缺少方框 b。另外,请注意当您进入透明框时,同一个透明框会消失。如果您通过其中一个盒子退出,透明盒子会“自动”再次出现。但是,如果您通过 DOM 对象为 window 的右上角退出透明框,则该框不会再次出现,直到您将鼠标悬停在三个框之一上。

这是一个小错误,但毕竟很重要。

此外,两个代码段的 JSCSS “完全”相同。

最佳答案

根据我的理解,您需要如下内容。

因为这个 $('.box').mousemove(function(e),所以当你离开鼠标时,透明框不会再次出现。当你在 .box< 上移动鼠标时 然后它会改变它的不透明度,否则不会。

将其更改为$(window).mousemove(function(e) { 鼠标离开透明框时会再次显示透明框。

$(function() {

var box = $('#box_e'),
w = box.outerWidth(),
h = box.outerHeight(),
xmin = box.offset().left,
xmax = xmin + w,
ymin = box.offset().top,
ymax = ymin + h;

box.mouseenter(function() {

	$(this).fadeTo(0,0).addClass('noevents');
});

$(window).mousemove(function(e) {

	if (elementOut(e) && box.hasClass('noevents')) {
	box.fadeTo(0,0.5).removeClass('noevents');
	}
});

function elementOut(e) {

var x = e.pageX, y = e.pageY,
onelement = x >= xmin && x <= xmax && y >= ymin && y <= ymax;

return !onelement;
}
});
#box_a, #box_b, #box_c, #box_d {
  height: 100px;
  width: 100px;
  position: absolute;
}

#box_a {
  top: 0px;
  left: 0px;
  background-color: red;
}

#box_b {
  top: 0px;
  left: 100px;
  background-color: blue;
}

#box_c {
  top: 100px;
  left: 0px;
  background-color: yellow;
}

#box_d {
  top: 100px;
  left: 100px;
  background-color: green;
}

#box_e {
  width: 100px;
  height: 100px;
  position: absolute;
  top: 50px;
  left: 50px;
  background-color: black;
  opacity: .5;
}

.noevents {
  pointer-events: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box_a" class="box"></div>
<!-- <div id="box_b" class="box"></div> -->
<div id="box_c" class="box"></div>
<div id="box_d" class="box"></div>
<div id="box_e"></div>

<!--
red   ,  blue
yellow, green
-->

关于javascript - 如何在通过窗口对象退出元素时单击未完全覆盖背景并在悬停时消失的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34916275/

相关文章:

javascript - Flickity Slider 按子类删除单元格

HTML 内容在加载时从一个段落移动到多个段落

CSS 二级菜单 - 二级菜单没有出现在正确的位置

javascript - 使用 CSS 或 JavaScript 关闭输入的自动更正

javascript - 通过 jQuery AJAX POST 发送 JavaScript 数组

javascript - 为什么 cat.setName = setName 和 setName.apply pig, ['Babe' ] 的输出是一样的。甚至为什么有必要在代码中包含它们

php - 使用 AJAX 的表单复选框始终在 php 中显示为 ON

jquery - 表格行的旋转图标点

javascript - 带有 `target="_blank "` doesn' t 打开新选项卡或窗口的沙盒 iFrame

javascript - 如何循环遍历几个月的数组