【问题标题】:Change text on hover, CSS and html更改悬停、CSS 和 html 上的文本
【发布时间】:2017-05-08 10:57:43
【问题描述】:

我想在将鼠标悬停在语音气泡(已创建)上时更改文本,并在鼠标返回时将文本设置回来。我有一个“欢迎!”在语音气泡上设置的文本,我希望它更改为“向下滚动”。另一个问题是我在 Speechbubble+welcome 上设置了一个 css 转换,这样就更难了..

这是我的代码:

#welcome{
 position:absolute;
 top:50%;
 left:50%;
 width:auto;
 height:auto;
-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
}


#speechbubble {
margin-left:110px;
width: 230px;
height: 80px;
line-height:80px;
text-align:center;
font-size:15px;
letter-spacing:2px;
-moz-box-shadow: 5px 5px 5px #888;
-webkit-box-shadow: 5px 5px 5px #888;
box-shadow: 5px 5px 5px #888;
font-family:{select:Title Font};
background: {color:Welcome background};
color:{color:Welcome text};
position: relative;
font-weight:bold;
}
 
#speechbubble:before {
content:"";
position: absolute;
right: 100%;
top: 26px;
width: 0;
height: 0;
border-top: 13px solid transparent;
border-right: 18px solid {color:Welcome background};
border-bottom: 13px solid transparent;
}

#welcome:hover #speechbubble{
-webkit-transition: all 0.7s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
margin-left:120px;
}

#welcome #speechbubble{
  -webkit-transition: all 0.7s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}
<div id="welcome">
<div id="speechbubble">Welcome!</div>

我尝试了一些技巧,例如为第二个文本添加 div 并设置 css 悬停,但没有成功。谁能帮助我?谢谢

【问题讨论】:

    标签: html css text hover textchanged


    【解决方案1】:

    您可以使用伪类:after 并在悬停时更改其内容。

    像这样:

    #welcome {
      position: absolute;
      top: 50%;
      left: 50%;
      width: auto;
      height: auto;
      -webkit-transform: translateX(-50%) translateY(-50%);
      -moz-transform: translateX(-50%) translateY(-50%);
      -ms-transform: translateX(-50%) translateY(-50%);
      transform: translateX(-50%) translateY(-50%);
    }
    
    #speechbubble {
      margin-left: 110px;
      width: 230px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      font-size: 15px;
      letter-spacing: 2px;
      -moz-box-shadow: 5px 5px 5px #888;
      -webkit-box-shadow: 5px 5px 5px #888;
      box-shadow: 5px 5px 5px #888;
      font-family: {
        select: Title Font
      }
      ;
      background: {
        color: Welcome background
      }
      ;
      color: {
        color: Welcome text
      }
      ;
      position: relative;
      font-weight:bold;
    }
    
    #speechbubble:after {
      content: "Welcome!";
    }
    
    #speechbubble:before {
      position: absolute;
      right: 100%;
      top: 26px;
      width: 0;
      height: 0;
      border-top: 13px solid transparent;
      border-right: 18px solid {
        color: Welcome background
      }
      ;
      border-bottom: 13px solid transparent;
    }
    
    #welcome:hover #speechbubble:after {
      content: "Scroll Down";
    }
    
    #welcome:hover #speechbubble {
      -webkit-transition: all 0.7s ease-in-out;
      -moz-transition: all 0.3s ease-in-out;
      -o-transition: all 0.3s ease-in-out;
      transition: all 0.3s ease-in-out;
      margin-left: 120px;
    }
    
    #welcome #speechbubble {
      -webkit-transition: all 0.7s ease-in-out;
      -moz-transition: all 0.3s ease-in-out;
      -ms-transition: all 0.3s ease-in-out;
      -o-transition: all 0.3s ease-in-out;
      transition: all 0.3s ease-in-out;
    }
    <div id="welcome">
      <div id="speechbubble"></div>

    【讨论】:

      【解决方案2】:

      使用:after & :before 试试这个,把它添加到你的css:

      #welcome:hover #speechbubble:after {
          content: "Scroll Down";
      }
      #welcome:hover #speechbubble:before {
          content: "";
      }
      #speechbubble:before {
          content: "Welcome!";
      }
      

      然后从您的 css 中删除此 righttopposition

      #speechbubble:before {
        content:"";
        /*position: absolute;*/
        /*right: 100%;*/
        /*top: 26px;*/
        width: 0;
        height: 0;
        border-top: 13px solid transparent;
        border-right: 18px solid {color:Welcome background};
        border-bottom: 13px solid transparent;
      }
      

      同时删除欢迎这个词:

      <div id="welcome">
      <div id="speechbubble"></div>
      

      这就是 CSS 的魔力 :)

      【讨论】:

      • 谢谢,如果不是正确的方法,这是最简单的方法!
      【解决方案3】:

      这是一个示例代码,它使用具有 data- 属性的 pseduo 元素来 实现的东西:

      #welcome {
        position: absolute;
        top: 50%;
        left: 50%;
        width: auto;
        height: auto;
        -webkit-transform: translateX(-50%) translateY(-50%);
        -moz-transform: translateX(-50%) translateY(-50%);
        -ms-transform: translateX(-50%) translateY(-50%);
        transform: translateX(-50%) translateY(-50%);
      }
      
      body {
        background: black;
      }
      
      
      /* .button */
      
      #speechbubble {
        display: inline-block;
        position: relative;
        margin: 1em;
        padding: 0.67em;
        border: 2px solid #FFF;
        overflow: hidden;
        text-decoration: none;
        font-size: 2em;
        outline: none;
        color: #FFF;
        background: transparent;
        font-family: 'raleway', sans-serif;
      }
      
      #speechbubble span {
        -webkit-transition: 0.6s;
        -moz-transition: 0.6s;
        -o-transition: 0.6s;
        transition: 0.6s;
        -webkit-transition-delay: 0.2s;
        -moz-transition-delay: 0.2s;
        -o-transition-delay: 0.2s;
        transition-delay: 0.2s;
      }
      
      #speechbubble:before {
        content: '';
        position: absolute;
        top: 0.67em;
        left: 0;
        width: 100%;
        text-align: center;
        opacity: 0;
        -webkit-transition: .4s, opacity .6s;
        -moz-transition: .4s, opacity .6s;
        -o-transition: .4s, opacity .6s;
        transition: .4s, opacity .6s;
      }
      
      
      /* :before */
      
      #speechbubble:before {
        content: attr(data-hover);
        -webkit-transform: translate(-150%, 0);
        -moz-transform: translate(-150%, 0);
        -ms-transform: translate(-150%, 0);
        -o-transform: translate(-150%, 0);
        transform: translate(-150%, 0);
      }
      
      
      /* Span on :hover and :active */
      
      #speechbubble:hover span {
        opacity: 0;
        -webkit-transform: scale(0.3);
        -moz-transform: scale(0.3);
        -ms-transform: scale(0.3);
        -o-transform: scale(0.3);
        transform: scale(0.3);
      }
      
      
      /*  
          We show :before pseudo-element on :hover 
          
      */
      
      #speechbubble:hover:before {
        opacity: 1;
        -webkit-transform: translate(0, 0);
        -moz-transform: translate(0, 0);
        -ms-transform: translate(0, 0);
        -o-transform: translate(0, 0);
        transform: translate(0, 0);
        -webkit-transition-delay: .4s;
        -moz-transition-delay: .4s;
        -o-transition-delay: .4s;
        transition-delay: .4s;
      }
      <div id="welcome">
        <div id="speechbubble" data-hover="Scroll Down"><span>Welcome!</span></div>
      
      </div>

      【讨论】:

      • 你能告诉我它是如何产生另一个泡沫的吗?
      • 它不包含在我的代码中,这意味着它可以制作我想要的动画,但是在带有透明气泡、其他字体等的新代码上。我可能不太清楚抱歉
      【解决方案4】:

      这是非常基本的方法。只需将文本放在一些标签中,然后在悬停时显示/隐藏它们。

      您还可以使用 css3 中的 attr 和 :after 功能。

      #welcome{
       position:absolute;
       top:50%;
       left:50%;
       width:auto;
       height:auto;
      -webkit-transform: translateX(-50%) translateY(-50%);
      -moz-transform: translateX(-50%) translateY(-50%);
      -ms-transform: translateX(-50%) translateY(-50%);
      transform: translateX(-50%) translateY(-50%);
      }
      
      
      #speechbubble {
      margin-left:110px;
      width: 230px;
      height: 80px;
      line-height:80px;
      text-align:center;
      font-size:15px;
      letter-spacing:2px;
      -moz-box-shadow: 5px 5px 5px #888;
      -webkit-box-shadow: 5px 5px 5px #888;
      box-shadow: 5px 5px 5px #888;
      font-family:{select:Title Font};
      background: {color:Welcome background};
      color:{color:Welcome text};
      position: relative;
      font-weight:bold;
      }
       
      #speechbubble:before {
      content:"";
      position: absolute;
      right: 100%;
      top: 26px;
      width: 0;
      height: 0;
      border-top: 13px solid transparent;
      border-right: 18px solid {color:Welcome background};
      border-bottom: 13px solid transparent;
      }
      
      #welcome:hover #speechbubble{
      -webkit-transition: all 0.7s ease-in-out;
      -moz-transition: all 0.3s ease-in-out;
      -o-transition: all 0.3s ease-in-out;
      transition: all 0.3s ease-in-out;
      margin-left:120px;
      }
      
      
      #welcome #speechbubble .hover_on {
        display: none;
      }
      #welcome #speechbubble .hover_off {
        display: inline-block;
      }
      
      #welcome:hover #speechbubble .hover_on {
        display: inline-block;
      }
      #welcome:hover #speechbubble .hover_off {
        display: none;
      }
      
      #welcome #speechbubble{
        -webkit-transition: all 0.7s ease-in-out;
        -moz-transition: all 0.3s ease-in-out;
        -ms-transition: all 0.3s ease-in-out;
        -o-transition: all 0.3s ease-in-out;
        transition: all 0.3s ease-in-out;
      }
      <div id="welcome">
      <div id="speechbubble"><p class="hover_off">Welcome!</p><p class="hover_on">Scroll down!</p></div>

      【讨论】:

      • 所以这行得通,但只是部分...
      • 对不起,@MaëlleJumel,但我没有得到你想要的。动画(淡入/淡出)?还是 PS 图标?
      猜你喜欢
      • 2012-05-10
      • 1970-01-01
      • 2020-02-17
      • 2020-09-22
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 2022-10-30
      • 2016-12-22
      相关资源
      最近更新 更多