使用不带js的title属性的工具提示?

时间:2016-10-07 16:39:08

标签: html css css3

我在title=""选择的:hover属性上制作工具提示的另一个解决方案之后很久都在搜索。

我希望找到一种方法来使用title=""属性并动态更改我的content:""属性。



* {
   -webkit-font-smoothing: antialiased;
   font-family: "Trebuchet MS", Helvetica, sans-serif;
}

div {
  width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
}

button {
  cursor: pointer;
  position: relative;
  display: inline-block;
  background: rgb(25, 181, 206);
  color: #fff;
  padding: 5px 15px;
  border: none;
  font-size: 1em;
}
    
button::after {
  content: "I'm a css tooltip!"; /* <= dynamically title content */
  z-index: -999;
  position: absolute;
  top: 110%;
  left: 0;
  width: 90%;
  padding: 5px 5%;
  font-size: 70%;
  text-align: center;
  color: #fff;
  background: rgb(21, 151, 171);
  opacity: 0;
  -webkit-transform: rotateX(180deg) rotateY(20deg) rotateZ(20deg);
  -moz-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  -o-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  -ms-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  -webkit-transform-origin: 100% 0%;
  transform-origin: 100% 0%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -webkit-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -moz-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -ms-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -o-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
}
    
button:hover::after {
  opacity: 1;
  box-shadow: 0px 2px 3px 0px rgba(0,0,0,0.15);
  -webkit-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  -moz-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  -o-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  -ms-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
}
&#13;
<div>
  <button title="I'm a css tooltip1!">button</button>
  <button title="I'm a css tooltip2!">button</button>
  <button title="I'm a css tooltip3!">button</button>
</div>
&#13;
&#13;
&#13;

Demo

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:4)

如果您需要自定义属性,请使用data-属性,如果您需要非自定义属性(例如标题),请使用data-

&#13;
&#13;
* {
   -webkit-font-smoothing: antialiased;
   font-family: "Trebuchet MS", Helvetica, sans-serif;
}

div {
  width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
}

button {
  cursor: pointer;
  position: relative;
  display: inline-block;
  background: rgb(25, 181, 206);
  color: #fff;
  padding: 5px 15px;
  border: none;
  font-size: 1em;
}
    
button::after {
  content: attr(data-title); /* <= dynamically title content */
  z-index: -999;
  position: absolute;
  top: 110%;
  left: 0;
  width: 90%;
  padding: 5px 5%;
  font-size: 70%;
  text-align: center;
  color: #fff;
  background: rgb(21, 151, 171);
  opacity: 0;
  -webkit-transform: rotateX(180deg) rotateY(20deg) rotateZ(20deg);
  -moz-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  -o-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  -ms-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  -webkit-transform-origin: 100% 0%;
  transform-origin: 100% 0%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -webkit-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -moz-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -ms-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -o-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
}
    
button:hover::after {
  opacity: 1;
  box-shadow: 0px 2px 3px 0px rgba(0,0,0,0.15);
  -webkit-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  -moz-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  -o-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  -ms-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
}
&#13;
<div>
  <button data-title="I'm a css tooltip1!">button</button>
  <button data-title="I'm a css tooltip2!">button</button>
  <button data-title="I'm a css tooltip3!">button</button>
</div>
&#13;
&#13;
&#13;

Jsfiddle link

答案 1 :(得分:-3)

我认为有一个很好的工具提示示例:bootstrap

示例:

<button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="left" title="Tooltip on left">Tooltip on left</button>