简体   繁体   English

如何在图像 hover 上显示文本而不是文本 hover - CSS/SASS

[英]How to show text on image hover rather than text hover - CSS/SASS

I'm sure this is a simple fix but can't get my head around what I'm doing wrong.我确信这是一个简单的解决方法,但我无法理解我做错了什么。 I'm trying to show the 'title-text' as well as reduce opacity of the image on hover of the image.我试图在图像的 hover 上显示“标题文本”并降低图像的不透明度。 I have the image opacity reduction down but can't seem to make the text appear unless I directly hover the text.我降低了图像不透明度,但似乎无法使文本出现,除非我直接 hover 文本。 Any help would be appreciated!任何帮助,将不胜感激! Thank you谢谢

#work {
  .items {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-gap: 1rem;

    .item {
      position: relative;
      background: $dark-color;
      // overflow: hidden;

      &:after {
        content: '';
        position: absolute;
        display: block;
        background: inherit;
        opacity: 0;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
      }

      // bring in main color overlay
      &:hover:after {
        opacity: 0.3;
      }

      &-text {
        position: absolute;
        top: 50%;
        left: 0;
        bottom: 0;
        right: 0;
        opacity: 0;
        text-align: center;
        z-index: 1;
        color: #fff;
      }

      // bring in text on hover
      &-image:hover &-text {
        opacity: 1;
      }

      &-image:before {
        content: '';
        display: block;
        padding-top: 75%;
        overflow: hidden;
      }

      &-image img {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: auto;
        line-height: 0;
      }
    }
  }

I think you can wrap the image and the text in a div:我认为您可以将图像和文本包装在 div 中:

<div class="wrapper">
    <img src="image.jpg">
    <p>Your Title</p>
</div>

<style>
    .wrapper {
        position: relative;
        overflow: hidden;
    }
    .wrapper img {
        display: block;
        max-width: 100%;
        line-height: 0;
    }
    .wrapper p {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        opacity: 0;
        z-index: 2;
    }
    .wrapper:hover img {
        opacity: 0.6;
    }
    .wrapper:hover p {
        opacity: 1;
    }
</style>

If I understand you correctly, what you are trying to do can not be done with HTML/CSS alone.如果我对您的理解正确,那么您尝试做的事情不能仅使用 HTML/CSS 来完成。 You would have to use JavaScript for such an effect.您必须使用 JavaScript 才能获得这样的效果。

Here is an example using jQuery which adds the class.darken to.item on hover.这是一个使用 jQuery 的示例,它将 class.darken 添加到 hover 上的.item。 This will not work in your case without you rewriting your code, but you get the idea.如果您不重写代码,这将不适用于您的情况,但您明白了。 You can of course add whatever in those two functions:您当然可以在这两个函数中添加任何内容:

<script>
    (function(){
        jQuery(".item").hover(
            function() {
                jQuery(this).addClass('darken');
            }, function() {
                jQuery(this).removeClass('darken');
            }
        );
    })();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM