【问题标题】:Display text when hover on image悬停在图像上时显示文本
【发布时间】:2017-02-09 18:51:17
【问题描述】:

我想在将鼠标悬停在图像上时更改为另一个图像并显示文本。我已经成功地在悬停时更改为另一个图像,但我不知道如何显示文本。而且我还想自定义文本(颜色、大小、位置)。我的代码:

<section id="works">
		<div class="container">
			<div class="desktop-12 columns"><div class="border-top"></div></div>

			
				<a href="single-illustration-sidebar-left.html" class="permalink">
					<div class="desktop-3 mobile-half columns">
						<div class="item first-row">
							<h3>Bird</h3>
							<span class="category">Illustration</span>

							<img src="images/thumb_item01.png" onmouseover="this.src='images/thumb_item01a.png';" onmouseout="this.src='images/thumb_item01.png';"/>
						</div><!-- // .item -->
					</div><!-- // .desktop-3 -->
                  <div class="clear"></div>
				</a>
				</div></section>

谢谢。

【问题讨论】:

  • 你能发布你的完整 HTML 吗?
  • 我已经用完整的代码编辑了帖子,请检查,谢谢
  • 你想显示什么样的文字?有一个标题属性可用于图片以简单地显示其标题。您还可以使用 css :hover 伪类来在有人将鼠标悬停在父元素上时显示内容。
  • @Phuong 另外,不要忘记接受您选择的答案,点击答案分数下方的灰色复选标记

标签: jquery html image text hover


【解决方案1】:

在单个查询中使用 mousentermouseleave

我建议将特定的选择器类 .firstImage 应用于您的图像。

$('.firstImage').on({
  mouseenter: function (evt) {
    $(this).attr('src', 'http://placehold.it/350x350')
    $(this).parent('div').css('color', 'red');
  },
  mouseleave: function (evt) {
    $(this).attr('src', 'http://placehold.it/350x150')  
    $(this).parent('div').css('color', 'blue');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="works">
		<div class="container">
			<div class="desktop-12 columns"><div class="border-top"></div></div>

			
				<a href="single-illustration-sidebar-left.html" class="permalink">
					<div class="desktop-3 mobile-half columns">
						<div class="item first-row">
							<h3>Bird</h3>
							<span class="category">Illustration</span>

							<img src="http://placehold.it/350x150" class="firstImage"/>
						</div><!-- // .item -->
					</div><!-- // .desktop-3 -->
                  <div class="clear"></div>
				</a>
				</div></section>

【讨论】:

    【解决方案2】:

    使用 CSS :hover 显示文本,这里与兄弟选择器 + 一起使用

    通过为您的 CSS 规则使用一个类,例如 .showtext:hover + div,您的所有文本只需要一个类,与 id 相反,每个文本都需要唯一

    根据评论更新

    为了将文本放在图像顶部,我在它们周围添加了一个包装器并给文本一个位置

    .imgwrapper {
      position: relative;
    }
    .showtext + div {
      position: absolute;
      left: 0;
      top: 0;
      display: none;
      pointer-events: none;
      font-size: 24px;
      width: 250px;
    }
    .showtext:hover + div {
      display: block;
    }
    <section id="works">
      <div class="container">
        <div class="desktop-12 columns">
          <div class="border-top"></div>
        </div>
    
        <a href="single-illustration-sidebar-left.html" class="permalink">
          <div class="desktop-3 mobile-half columns">
            <div class="item first-row">
              <h3>Bird</h3>
              <span class="category">Illustration</span>
    
              <div class="imgwrapper">
                <img class="showtext" src="http://placehold.it/250" onmouseover="this.src='http://placehold.it/250/0ff';" onmouseout="this.src='http://placehold.it/250';" />
    
                <div>Some text to be shown on top of the image</div>
              </div>
    
            </div>
            <!-- // .item -->
          </div>
          <!-- // .desktop-3 -->
          <div class="clear"></div>
        </a>
      </div>
    </section>

    【讨论】:

    • 感谢您的回复,我是初学者。我必须命名 div 吗?因为我还有很多其他的 div,如果是的话应该是 img + #name 和 img:hover + #name?
    • @Phuong 更新了我的答案,通过使用像 .showtext:hover + div 这样的类,您的所有图像只需要一个
    • 谢谢,它显示了文字,但文字在图像下方,我希望文字在图像上,有什么办法吗?
    • @Phuong 再次更新了我的答案,展示了如何做到这一点
    • 完美运行!!非常感谢!请问.imgwraper的用途是什么?
    【解决方案3】:

    试试这个:

    $(document).ready(function() {
        
        $(".myimg").mouseenter(function(){
            $(this).attr({src:"http://kurld.com/images/wallpapers/flower-image/flower-image-18.jpg"});
            $(".txt").show();
            
        }).mouseleave(function(){
            $(this).attr({src:"https://static.pexels.com/photos/67857/daisy-flower-spring-marguerite-67857.jpeg"});
            $(".txt").hide();
            
        })
    })
     img {
       width: 200px;
       height: 100px;
       }
    .txt {
      display: none;
      position: absolute;
      bottom: 10px;
      left: 10px;
      background-color: #fff;
      }
    .wrapper {
      position: relative;
      }
     <section id="works">
    		<div class="container">
    			<div class="desktop-12 columns"><div class="border-top"></div></div>
    				<a href="single-illustration-sidebar-left.html" class="permalink">
    					<div class="desktop-3 mobile-half columns">
    						<div class="item first-row">
    							<h3>Bird</h3>
    							<span class="category">Illustration</span>
                                <div class="wrapper">
    							<img class="myimg" src="https://static.pexels.com/photos/67857/daisy-flower-spring-marguerite-67857.jpeg" />
                                <div class="txt">Red Flower</div>
                                </div>
    						</div><!-- // .item -->
    					</div><!-- // .desktop-3 -->
                      <div class="clear"></div>
    				</a>
    				</div></section>
            
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    【讨论】:

      【解决方案4】:

      您只需要为该文本“div”提供一个 id 并将其更新为您的 CSS:

      img + div {
        display: none;
      }
      img:hover + #ImageText {
        display: block;
      }
      <section id="works">
        <div class="container">
          <div class="desktop-12 columns">
            <div class="border-top"></div>
          </div>
      
      
          <a href="single-illustration-sidebar-left.html" class="permalink">
            <div class="desktop-3 mobile-half columns">
              <div class="item first-row">
                <h3>Bird</h3>
                <span class="category">Illustration</span>
      
                <img src="http://placehold.it/50" onmouseover="this.src='http://placehold.it/50/f00';" onmouseout="this.src='http://placehold.it/50';" />
      
                <div id="ImageText">Some text to be shown</div>
              </div>
              <!-- // .item -->
            </div>
            <!-- // .desktop-3 -->
            <div class="clear"></div>
          </a>
        </div>
      </section>

      【讨论】:

      • 谢谢,它显示了文字,但文字在图像下方,我希望文字在图像上,有什么办法吗?
      • @Phuong 你可以只添加一个包含隐藏图像文本的跨度。在图像悬停上,您只需显示该文本范围。
      猜你喜欢
      • 2013-07-11
      • 2016-12-23
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多