如何在按钮悬停

时间:2017-04-21 06:21:39

标签: html css

使用CSS&amp; HTML我试图创建一个应该出现在按钮悬停上的弹出窗口。但是在下面的代码中,弹出窗口同时显示<button>&amp; <h2>标记。由于我是初学者,我无法理解为获得正确输出所需的更改。请帮我解决这个问题。

HTML PAGE

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
   <title> POPOUP</title>

   <link rel="stylesheet" type="text/css" href="tooltip.css">


</head>
<body style="text-align:center">
<div class="popup">
<h2>Popup</h2>

<button type="button" id="b1">BUTTON</button>
  <span class="popuptext" id="myPopup">A Simple Popup!</span>
</div>

</body>
</html>

我没有正确的JS知识,因此我使用CSS作为悬停功能。

CSS

/* Popup container - can be anything you want */
.popup {
    position: relative;
    display: inline-block;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* The actual popup */
.popup .popuptext {
    visibility: hidden;
    width: 160px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 8px 0;
    position: absolute;
    z-index: 1;
    top: 225%;
    left: 50%;
    margin-left: -80px;
}

/* Popup arrow */
.popup .popuptext::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

/* Toggle this class - hide and show the popup */
.popup .show {
    visibility: visible;
    -webkit-animation: fadeIn 1s;
    animation: fadeIn 1s;
}

.popup:hover .popuptext {
    visibility: visible;
    opacity: 1;
}






 /* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
    from {opacity: 0;} 
    to {opacity: 1;}
}

@keyframes fadeIn {
    from {opacity: 0;}
    to {opacity:1 ;}
}

1 个答案:

答案 0 :(得分:3)

您需要使用此选择器,只有在ajaxRequest1.abort();悬停时才会触发弹出窗口。

#b1

而不是这个,当.popup #b1:hover + .popuptext 或它的所有子元素都悬停时,这会触发弹出窗口。

.popup

.popup:hover .popuptext 
.popup {
  position: relative;
  display: inline-block;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}


/* The actual popup */

.popup .popuptext {
  visibility: hidden;
  width: 160px;
  background-color: #555;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 8px 0;
  position: absolute;
  z-index: 1;
  top: 225%;
  left: 50%;
  margin-left: -80px;
}


/* Popup arrow */

.popup .popuptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #555 transparent transparent transparent;
}


/* Toggle this class - hide and show the popup */

.popup .show {
  visibility: visible;
  -webkit-animation: fadeIn 1s;
  animation: fadeIn 1s;
}

.popup #b1:hover + .popuptext {
  visibility: visible;
  opacity: 1;
}


/* Add animation (fade in the popup) */

@-webkit-keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}