防止儿童触发悬停

时间:2019-12-02 03:34:32

标签: html css css-transitions

我正在为一个需要在一页上使用表单的类进行的最后一个项目。长话短说,我希望将鼠标悬停在“联系人”按钮上时显示表单。但是,当表单停留在空间中时,它会出现。如何从没有 JavaScript的节点的子节点上触发时移除悬停事件?这是一个基本的Web创作类,因此除非有必要,否则我不想使用JavaScript。

我也想在.droptainer上保持过渡。悬停时将display: none设置为display: block可以删除过渡,因为display属性无法设置动画。

/* styles for the navigation menu */

nav ul {
  list-style: none;
  position: relative;
  padding: 0;
  display: grid;
  grid-template: auto / repeat(5, 1fr);
  align-content: center;
}

nav ul ul {
    display: none;
    position: absolute;
    top: 100%;
    width: 20%;
}

nav ul li:hover > ul {
    display: block;    
}

nav>ul::after {
    content: "";
    clear: both;
    display: block;
}

nav ul ul li {
    
    float: none;
}

nav ul li a:hover {
  color: black;
  background-color: orange;
  transition: 500ms;
}

nav ul li a {
    border: 2px solid gray;
    border-radius: 10px;
    text-align: center;
    display: block;
    padding: .7em 0;
    text-decoration: none;
    background-color: black;
    color: floralwhite;
}

/* styles for the dropdown form */

.droptainer {
  opacity: 0;
  display: block;
  position: absolute;
  top: 100%;
  left: 60%;
  margin: 0.5%;
  padding: 2em 10%;
  background-color: orange;
  border: 2px solid gray;
  border-radius: 15px;
  box-shadow: 5px -5px 12px 4px rgba(0, 0, 0, 0.50);
  transform: translatey(-5%);
  transition: 600ms 250ms;
  
}

.drop_btn:hover > .droptainer {
  transform: translatey(5%);
  opacity: 1;  
}

.droptainer:after {
  content: "";
  position: absolute;
  top: -9%;
  left: 50%;
  width: 50%;
  height: 1.75em;
  }
 <nav class="nav_menu" role="navigation">
    <ul>
      <li><a href="index.html">Home</a></li>
      <li><a href="nav/ingredients.html">Ingredients</a>
        <ul>
          <li><a href="nav/fish.html">Fish</a></li>
          <li><a href="nav/vegetables.html">Vegetables</a></li>
          <li><a href="nav/condiments.html">Condiments</a></li>
          <li><a href="nav/others.html">Others</a></li>
        </ul>
      </li>
      <li><a href="nav/history.html">History</a></li>
      <li><a href="nav/trivia.html">Trivia</a></li>
      <li class="drop_btn">
        <a href="#">Contact</a>
        <div class="droptainer">
          <h3>Sign up for the latest sushi news</h3>
          <form>
            <label>Name</label>
            <input type="text">
            <input type="text">
            <label>Email</label>
            <input type="text">
            <input type="checkbox">
            <input type="checkbox">
            <label>I want to sign up for SushiNews.</label>
            <button>Submit</button>
            <button>Reset</button>
          </form>
        </div>
      </li>
    </ul>
</nav>

3 个答案:

答案 0 :(得分:1)

只要Droptainer为WidgetsBinding.instance.addPostFrameCallback((_) { Navigator.pushNamed(context, '/'); }); ,只需设置pointer-events: none

hidden
/* styles for the navigation menu */

nav ul {
  list-style: none;
  position: relative;
  padding: 0;
  display: grid;
  grid-template: auto / repeat(5, 1fr);
  align-content: center;
}

nav ul ul {
  display: none;
  position: absolute;
  top: 100%;
  width: 20%;
}

nav ul li:hover>ul {
  display: block;
}

nav>ul::after {
  content: "";
  clear: both;
  display: block;
}

nav ul ul li {
  float: none;
}

nav ul li a:hover {
  color: black;
  background-color: orange;
  transition: 500ms;
}

nav ul li a {
  border: 2px solid gray;
  border-radius: 10px;
  text-align: center;
  display: block;
  padding: .7em 0;
  text-decoration: none;
  background-color: black;
  color: floralwhite;
}


/* styles for the dropdown form */

.droptainer {
  opacity: 0;
  display: block;
  position: absolute;
  top: 100%;
  left: 60%;
  margin: 0.5%;
  padding: 2em 10%;
  background-color: orange;
  border: 2px solid gray;
  border-radius: 15px;
  box-shadow: 5px -5px 12px 4px rgba(0, 0, 0, 0.50);
  transform: translatey(-5%);
  transition: 600ms 250ms;
  pointer-events: none;
}

.drop_btn:hover>.droptainer {
  transform: translatey(5%);
  opacity: 1;
  pointer-events: all;
}

.droptainer:after {
  content: "";
  position: absolute;
  top: -9%;
  left: 50%;
  width: 50%;
  height: 1.75em;
}

答案 1 :(得分:0)

一种解决方案是默认将display设置为none,然后在悬停时将其设置为block(将display切换为{ {1}}。

不幸的是,对此的警告是opacitytransition属性上不起作用,因此您的元素将立即显示-尽管这可能就足够了。

display
/* styles for the navigation menu */

nav ul {
  list-style: none;
  position: relative;
  padding: 0;
  display: grid;
  grid-template: auto / repeat(5, 1fr);
  align-content: center;
}

nav ul ul {
    display: none;
    position: absolute;
    top: 100%;
    width: 20%;
}

nav ul li:hover > ul {
    display: block;    
}

nav>ul::after {
    content: "";
    clear: both;
    display: block;
}

nav ul ul li {
    
    float: none;
}

nav ul li a:hover {
  color: black;
  background-color: orange;
  transition: 500ms;
}

nav ul li a {
    border: 2px solid gray;
    border-radius: 10px;
    text-align: center;
    display: block;
    padding: .7em 0;
    text-decoration: none;
    background-color: black;
    color: floralwhite;
}

/* styles for the dropdown form */

.droptainer {
  display: none;
  position: absolute;
  top: 100%;
  left: 60%;
  margin: 0.5%;
  padding: 2em 10%;
  background-color: orange;
  border: 2px solid gray;
  border-radius: 15px;
  box-shadow: 5px -5px 12px 4px rgba(0, 0, 0, 0.50);
  transform: translatey(-5%);
  transition: 600ms 250ms;
  
}

.drop_btn:hover > .droptainer {
  transform: translatey(5%);
  display: block; 
}

.droptainer:after {
  content: "";
  position: absolute;
  top: -9%;
  left: 50%;
  width: 50%;
  height: 1.75em;
  }

另一个选择是切换<nav class="nav_menu" role="navigation"> <ul> <li><a href="index.html">Home</a></li> <li><a href="nav/ingredients.html">Ingredients</a> <ul> <li><a href="nav/fish.html">Fish</a></li> <li><a href="nav/vegetables.html">Vegetables</a></li> <li><a href="nav/condiments.html">Condiments</a></li> <li><a href="nav/others.html">Others</a></li> </ul> </li> <li><a href="nav/history.html">History</a></li> <li><a href="nav/trivia.html">Trivia</a></li> <li class="drop_btn"> <a href="#">Contact</a> <div class="droptainer"> <h3>Sign up for the latest sushi news</h3> <form> <label>Name</label> <input type="text"> <input type="text"> <label>Email</label> <input type="text"> <input type="checkbox"> <input type="checkbox"> <label>I want to sign up for SushiNews.</label> <button>Submit</button> <button>Reset</button> </form> </div> </li> </ul> </nav>而不是visibility。这使您可以保留opacity ...,但是该元素最初仍将显示在页面顶部,然后 then 移动(可能不希望如此)。不过,您可以通过定位来抵消这一点。

transition
/* styles for the navigation menu */

nav ul {
  list-style: none;
  position: relative;
  padding: 0;
  display: grid;
  grid-template: auto / repeat(5, 1fr);
  align-content: center;
}

nav ul ul {
    display: none;
    position: absolute;
    top: 100%;
    width: 20%;
}

nav ul li:hover > ul {
    display: block;    
}

nav>ul::after {
    content: "";
    clear: both;
    display: block;
}

nav ul ul li {
    
    float: none;
}

nav ul li a:hover {
  color: black;
  background-color: orange;
  transition: 500ms;
}

nav ul li a {
    border: 2px solid gray;
    border-radius: 10px;
    text-align: center;
    display: block;
    padding: .7em 0;
    text-decoration: none;
    background-color: black;
    color: floralwhite;
}

/* styles for the dropdown form */

.droptainer {
  visibility: hidden;
  display: block;
  position: absolute;
  top: 100%;
  left: 60%;
  margin: 0.5%;
  padding: 2em 10%;
  background-color: orange;
  border: 2px solid gray;
  border-radius: 15px;
  box-shadow: 5px -5px 12px 4px rgba(0, 0, 0, 0.50);
  transform: translatey(-5%);
  transition: 600ms 250ms;
  
}

.drop_btn:hover > .droptainer {
  transform: translatey(5%);
  visibility: visible;  
}

.droptainer:after {
  content: "";
  position: absolute;
  top: -9%;
  left: 50%;
  width: 50%;
  height: 1.75em;
  }

答案 2 :(得分:0)

有一种解决方案,并且要使其简短并让您易于理解:

更改代码

.droptainer {
  opacity:0;
  visibility: hidden;
  position: absolute;
  top: 100%;
  left: 60%;
  margin: 0.5%;
  padding: 2em 10%;
  background-color: orange;
  border: 2px solid gray;
  border-radius: 15px;
  box-shadow: 5px -5px 12px 4px rgba(0, 0, 0, 0.50);
  transform: translatey(0);
  transition: 600ms 250ms;


}

.drop_btn:hover  .droptainer {
  transform: translatey(5%);
  visibility: visible;
  opacity:1;

}

仅在单击按钮时,它将为您提供完美的过渡。 空格问题已解决。

注意:请记住使用不显示可见性的过渡。