我的工具提示可以适应触摸屏吗?

时间:2016-05-10 20:21:37

标签: html css html5 css3

我已经为我的广告制作了一些工具提示,这些工具提示了很多术语 - 用简单的鼠标显示它们。显然它们不适用于触摸界面。

我只限于理想的纯HTML5和CSS3,绝对没有jquery,理想情况下没有javascript。我尝试过更改(或者说是添加):激活到:悬停类,但触摸屏上什么都没有。

当前的HTML和CSS

.tiptext {
  cursor: help;
  color: black;
  font-family: 'ProximaNova', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;
  font-weight: 600 !important;
  border-bottom: 1px solid #ebebeb;
  box-shadow: inset 0 -5px 0 #ebebeb;
  -webkit-transition: background .15s cubic-bezier(.33, .66, .66, 1);
  transition: background .15s cubic-bezier(.33, .66, .66, 1);
  text-decoration: none;
  font-size: 14px;
  line-height: 172%;
  -webkit-animation-name: link-helpoff;
  -webkit-animation-duration: 1s;
  animation-name: link-helpoff;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  transition-delay: 0.4s;
}
.tiptext::after {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  color: transparent;
  background-color: transparent;
  transition: background-color 0.5s linear;
}
.tiptext:hover::after {
  background-color: rgba(255, 255, 255, 0.6);
}
.description {
  border: 1px solid #e3e3e3;
  background: white;
  width: auto;
  max-width: 275px;
  height: auto;
  padding: 10px;
  font-family: 'ProximaNova', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;
  font-weight: 300;
  color: rgb(39, 44, 45);
  font-size: 13px;
  z-index: 500;
  position: absolute;
  margin-left: 50px;
  margin-top: 20px;
  cursor: default;
  display: inline-block;
}
.tiptext > .description {
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s linear 0.4s, opacity 0.4s linear;
}
.tiptext:hover > .description {
  visibility: visible;
  opacity: 1;
  transition-delay: 0s;
  -webkit-transition: opacity 0.2s ease-in;
  -moz-transition: opacity 0.2s ease-in;
  -ms-transition: opacity 0.2s ease-in;
  -o-transition: opacity 0.2s ease-in;
  transition: opacity 0.2s ease-in;
}
.tiptext:hover {
  color: black;
  -webkit-animation-name: link-help;
  -webkit-animation-duration: 0.6s;
  animation-name: link-help;
  animation-duration: 0.6s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  transition-delay: 0s;
}
<span class="tiptext">
  <span class="description" style="text-align:center;">You can read more about the topic in this pop up box</span>
  Mouse over me to learn more.
</span>

在CSS中有一些动画和技巧(我没有包含链接帮助动画,但是你明白了)基本上当你将鼠标放在它上面时盒子会淡入淡出 - 并且还有一个完整的屏幕白色背景渐渐消失,有点不透明,可以将鼠标悬浮在盒子上 - 如果在触摸屏设备上没有这样的话,这没什么大不了的。

我怀疑可能需要进行重大更改才能在触摸屏设备上按下相同的弹出框。

2 个答案:

答案 0 :(得分:3)

是的,有办法。

首先:添加属性&#34; ontouchstart&#34;到你的按钮。 第二:为:active,:focus和:hover添加样式。

我在iOS上测试了这个并且它有效。 (Haven并未尝试使用Android)。

&#13;
&#13;
div {
  display: none;
}
button {
  width: 200px;
  height: 50px;
  border: 1px solid red;
  display: inline-block;
  
}
button:active,
button:focus,
button: hover {
  background-color: blue;
}
button:active + div,
button:focus + div,
button:hover + div {
  display: block;
}
&#13;
<p>Hello</p>
<button ontouchstart>Activate</button>
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Impedit, ipsa, officiis! Est voluptatibus explicabo sed atque provident vel deleniti nulla quis, ipsa quas dolorum, dolorem cum possimus accusamus impedit nostrum!</div>
<p>Goodbye</p>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

执行此操作的一种方法是将tabindex属性添加到.tiptext元素,并为其赋值-1。这将允许元素获得焦点,因此可以使用:focus伪类进行选择。

您还需要将touchstart事件添加到body标记(或您正在使用的元素的任何父元素),以便让iOS识别{{1 }和:active伪类。

:focus

<body ontouchstart>
.tiptext {
  cursor: help;
  color: black;
  font-family: 'ProximaNova', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;
  font-weight: 600 !important;
  border-bottom: 1px solid #ebebeb;
  box-shadow: inset 0 -5px 0 #ebebeb;
  -webkit-transition: background .15s cubic-bezier(.33, .66, .66, 1);
  transition: background .15s cubic-bezier(.33, .66, .66, 1);
  text-decoration: none;
  font-size: 14px;
  line-height: 172%;
  -webkit-animation-name: link-helpoff;
  -webkit-animation-duration: 1s;
  animation-name: link-helpoff;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  transition-delay: 0.4s;
}
.tiptext::after {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  color: transparent;
  background-color: transparent;
  transition: background-color 0.5s linear;
}
.tiptext:focus::after,.tiptext:hover::after {
  background-color: rgba(255, 255, 255, 0.6);
}
.description {
  border: 1px solid #e3e3e3;
  background: white;
  width: auto;
  max-width: 275px;
  height: auto;
  padding: 10px;
  font-family: 'ProximaNova', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;
  font-weight: 300;
  color: rgb(39, 44, 45);
  font-size: 13px;
  z-index: 500;
  position: absolute;
  margin-left: 50px;
  margin-top: 20px;
  cursor: default;
  display: inline-block;
}
.tiptext > .description {
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s linear 0.4s, opacity 0.4s linear;
}
.tiptext:focus > .description,.tiptext:hover > .description {
  visibility: visible;
  opacity: 1;
  transition-delay: 0s;
  -webkit-transition: opacity 0.2s ease-in;
  -moz-transition: opacity 0.2s ease-in;
  -ms-transition: opacity 0.2s ease-in;
  -o-transition: opacity 0.2s ease-in;
  transition: opacity 0.2s ease-in;
}
.tiptext:focus,.tiptext:hover {
  color: black;
  -webkit-animation-name: link-help;
  -webkit-animation-duration: 0.6s;
  animation-name: link-help;
  animation-duration: 0.6s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  transition-delay: 0s;
}