简体   繁体   English

当鼠标悬停时,CSS文本显示在图像上

[英]CSS text appears on image when mouse hover

I'm making CSS hover effect. 我正在使CSS悬停效果。 What I wanted to do is when user hover their mouse on small thumbnail than it shows text, and when user click this thumbnail it shows large image. 我想做的是,当用户将鼠标悬停在小缩略图上而不是显示文本时,而当用户单击此缩略图时则显示大图像。 I made modal but I keep having trouble with text. 我做了模态运算,但是在文字方面一直遇到麻烦。 The text is right under the image and it shows all the time. 文本在图像下方,并且始终显示。 I would like to move this text center of the image and only appears when user hovers their mouse on the image. 我想移动图像的文本中心,并且仅在用户将鼠标悬停在图像上时显示。

CSS 的CSS

 <!-- filter style --> <link rel="stylesheet" href="css/style.css"> <!-- modal style--> <link rel="stylesheet" href="css/w3.css"> /* image style*/ #wrapper{ position:relative; } /* text style*/ #wrapper p{ z-index:100; position:absolute; top:50%; left:90%; visibility:hidden; font-family:'Alegreya', serif; opacity: 0; color:white; } #wrapper:hover #wrapper p{ visibility: visible; opacity: 1; } 
 HTML <section class="cd-gallery"> <ul> <li class="mix color-1 check1 radio2 option3"> <div class="wrapper"> <img src="http://cfile1.uf.tistory.com/image/255D8B4E51ADD7CD04EF09" alt="Image 1" style="width:100%;cursor:zoom-in" onclick="document.getElementById('modal01').style.display='block'"/> <p class="text">Band<br>Portrait</p></div> <div id="modal01" class="w3-modal" onclick="this.style.display='none'"> <span class=" w3-padding-16 w3-display-topright">&times;</span> <div class="w3-modal-content w3-animate-zoom"> <img src="http://cfile1.uf.tistory.com/image/255D8B4E51ADD7CD04EF09" style="width:100%"> </div> </div> </li> </ul> </section> 

insert these rules in your document 在您的文档中插入这些规则

.text {
  opacity: 0;
  display: inline-block;
  text-align: cen;
  position: relative;
  left: 50%;
  transform: translate(-50%);
  -webkit-transform: translate(-50%);
  -moz-transform: translate(-50%);
  -ms-transform: translate(-50%);
  -o-transform: translate(-50%);
}
.wrapper:hover .text {
  opacity: 1;
}

Note the transform property is not support by old browsers 请注意,旧浏览器不支持transform属性

See snippet below 请参见下面的代码段

 <!-- filter style --> <link rel="stylesheet" href="css/style.css"> <!-- modal style--> <link rel="stylesheet" href="css/w3.css"> /* image style*/ #wrapper { position: relative; } /* text style*/ #wrapper p { z-index: 100; position: absolute; top: 50%; left: 90%; visibility: hidden; font-family: 'Alegreya', serif; opacity: 0; color: white; } #wrapper:hover #wrapper p { visibility: visible; opacity: 1; } .text { opacity: 0; display: inline-block; text-align: cen; position: relative; left: 50%; transform: translate(-50%); -webkit-transform: translate(-50%); -moz-transform: translate(-50%); -ms-transform: translate(-50%); -o-transform: translate(-50%); } .wrapper:hover .text { opacity: 1; } 
 HTML <section class="cd-gallery"> <ul> <li class="mix color-1 check1 radio2 option3"> <div class="wrapper"> <img src="http://cfile1.uf.tistory.com/image/255D8B4E51ADD7CD04EF09" alt="Image 1" style="width:100%;cursor:zoom-in" onclick="document.getElementById('modal01').style.display='block'" /> <p class="text">Band <br>Portrait</p> </div> <div id="modal01" class="w3-modal" onclick="this.style.display='none'"> <span class=" w3-padding-16 w3-display-topright">&times;</span> <div class="w3-modal-content w3-animate-zoom"> <img src="http://cfile1.uf.tistory.com/image/255D8B4E51ADD7CD04EF09" style="width:100%"> </div> </div> </li> </ul> </section> 

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

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