【问题标题】:Display text when header width = 300px, otherwise set display: none;当 header width = 300px 时显示文本,否则设置 display: none;
【发布时间】:2016-04-06 17:18:30
【问题描述】:

我为我的标题编写了一个代码,当您将标题悬停时,它会将其宽度调整为 300px,现在我想仅在宽度 = 300px 时在其中添加文本,或者在将标题悬停后调整标题大小时慢慢添加文本鼠标。

这是我写的代码。 CSS:

#header {宽度:5px;高度:计算(100vh - 30px);文本对齐:居中;过渡:宽度 0.5s 缓出; -o-transition:宽度 0.5s 缓出; -ms-transition:宽度 0.5s 缓出; -moz-transition:宽度 0.5s 缓出; -webkit-transition:宽度 0.5s 缓出;} #header:hover {宽度:300px;背景:#171C21;}

HTML:

<div id="header">
    <p>test</p>
    <p>test</p>
    <p>test</p>
</div>

如果有任何方法可以在 CSS 中实现,我正在寻找一种 CSS 方法。

实时预览:http://awesomeness.adam.zur.io/

【问题讨论】:

  • overflow: hidden;?
  • 使用媒体查询? @media screen and (max-width: 300px) { // code go here } ??
  • @John 他不是在问屏幕宽度,是吗?

标签: html css


【解决方案1】:

我认为这应该很有用。

#header { 
  width              : 10px                ; 
  height             : calc(100vh - 30px)  ;  
  text-align         : center              ; 
  transition         : width 0.5s ease-out ; 
  -o-transition      : width 0.5s ease-out ; 
  -ms-transition     : width 0.5s ease-out ; 
  -moz-transition    : width 0.5s ease-out ; 
  -webkit-transition : width 0.5s ease-out ;
  background         : #171C21             ; 
  overflow           : hidden              ;
  color              : white               ;
}

#header:hover {
  width : 300px ;
}

#textContent{
  visibility : hidden;  
}

#header:hover  #textContent{
  visibility : visible;
}
<div id="header">
  <div id="textContent">
    <p>test</p>
    <p>test</p>
    <p>test</p>
  <div/>
</div>

【讨论】:

    【解决方案2】:

    您可以通过使用color: rbga(0, 0, 0, 0.0); 的颜色值使其完全透明来使文本不可见,并使用color: rbga(0, 0, 0, 1.0); 使颜色在悬停时变为纯色

    这是一个演示 http://codepen.io/anon/pen/yeggKO 的代码笔 (已编辑,现在悬停时也有 300px 宽度)

    【讨论】:

    • /* DESKTOP */ @media (min-width: 800px) { #header {width: 5px; height: calc(100vh - 30px); color: rgba(255,255,255,0.0); text-align: center; transition: width 0.5s ease-out; -o-transition: width 0.5s ease-out; -ms-transition: width 0.5s ease-out; -moz-transition: width 0.5s ease-out; -webkit-transition: width 0.5s ease-out;} #header:hover {width: 300px; background: #171C21; color: rgba(255,255,255,1.0);} } 试过了,动画在文本上不起作用,“a”不能有不透明度 0.0(alpha = 0)
    【解决方案3】:

    你可以做一件事:

    添加一个div 和hidden 元素,当您在#header 上的hover 使该div 标签的display : block 时。

    这是一个小例子:

    #header {width: 5px; height: calc(100vh - 30px); text-align: center; transition: width 0.5s ease-out; -o-transition: width 0.5s ease-out; -ms-transition: width 0.5s ease-out; -moz-transition: width 0.5s ease-out; -webkit-transition: width 0.5s ease-out;}
    #header .hiddencontent{display:none;}
    #header:hover {width: 300px; background: #171C21;}
    #header:hover .hiddencontent{display:block;}
    <div id="header">
        <p>test</p>
        <p>test</p>
        <p>test</p>
        <div class="hiddencontent">
        <p>test</p>
        <p>test</p>
        <p>test</p>
          <p>test</p>
        <p>test</p>
        <p>test</p>
        </div>
    </div>

    【讨论】:

      【解决方案4】:

      好吧,一切似乎都解决了,但是标题的悬停效果出了点问题。当我取下标题的鼠标时,它不会动画回原来的宽度,它会在没有动画的情况下返回。

      html, body {background: #1A2026; font-family: "Open Sans", "sans-serif";}
      * {margin: 0; padding: 0;}
      a {display: block;}
      
      /* DESKTOP */
      #header {width: 5px; height: calc(100vh - 30px); padding-top: 10px; color: rgba(255,255,255,0.0); text-align: center; transition: width 0.5s ease-out; -o-transition: width 0.5s ease-out; -ms-transition: width 0.5s ease-out; -moz-transition: width 0.5s ease-out; -webkit-transition: width 0.5s ease-out;}
      #header:hover {width: 300px; background: #171C21; color: rgba(255,255,255,1.0);}
      #header .hidden {display:none;}
      #header .hidden > p {font-size: 16px; text-transform: uppercase; margin-bottom: 10px; font-weight: 600;}
      #header .hidden > a {margin-top: 5px; text-decoration: none; color: #fff; font-weight: 600;}
      #header .hidden > a:hover {color: #27ae60;}
      #header:hover .hidden {display:block;}
      
      #footer {width: 100%; height: auto; bottom: 0px; position: absolute;}
      #footer .copyright {height: 30px; background: #171C21; line-height: 30px;}
      #footer .copyright > p {color: #fff; text-align: center; font-size: 13px;}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多