首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS在悬停或单击图像上显示单独的文本

CSS在悬停或单击图像上显示单独的文本
EN

Stack Overflow用户
提问于 2021-09-12 17:34:42
回答 1查看 65关注 0票数 1

当用户悬停或单击图像时,我尝试显示一条文本消息。这些消息将是:“好的”或“好的”每个图像的不同文本。由于某些原因,我不能让它工作,我可以让它工作,如果我悬停在普通的旧文本上,而不是在图像上。我已经拔掉头发两天了,现在真的需要帮助了。

代码语言:javascript
复制
<link rel='stylesheet' href='https://afeld.github.io/emoji-css/emoji.css'>

<style>

        @import url("https://fonts.googleapis.com/css?family=Lato:400,700");
        
        .rating-wrapper {
          max-width: 400px;
          margin: 80px auto;
          background: #fff;
          padding: 1em;
          border-radius: 3px;
          box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.1);
        }
        .rating-wrapper .rating-label {
          text-align: center;
          font-weight: 700;
          display: block;
        }
        .rating-wrapper .ratingItemList {
          max-width: 300px;
          margin: auto;
          display: flex;
          justify-content: space-between;
          padding: 1em 0;
        }
        .rating-wrapper input.rating {
          display: none;
        }
        .rating-wrapper label.rating {
          padding: 5px 3px;
          font-size: 32px;
          opacity: 0.7;
          filter: grayscale(1);
          cursor: pointer;
        }
        .rating-wrapper label.rating:hover {
          filter: grayscale(0.84);
          transform: scale(1.1);
          transition: 100ms ease;
        }
        .rating-wrapper input.rating:checked + label.rating {
          filter: grayscale(0);
          opacity: 1;
          transform: scale(1.1);
        }
        
        .hide {
          display: none;
        }
            
        .myDIV:hover + .hide {
          display: block;
          color: red;
        }

</style>
代码语言:javascript
复制
    <form class="rating-wrapper">
      <label class="rating-label">How helpful was this?
        <div class="ratingItemList">
          
          <input class="rating rating-4" id="rating-4-2" type="radio" value="4" name="rating"/>
          <label class="rating rating-4" for="rating-4-2"><i class="em em-grinning"></i></label>
          
          <input class="rating rating-5" id="rating-5-2" type="radio" value="5" name="rating"/>
          <label class="rating rating-5" for="rating-5-2"><i class="em em-star-struck"></i></label>
        </div>
      </label>
      
      <label class="myDIV">Hover over me.</label>
      
      <div class="hide"><br>I am shown when someone hovers over the image above.</div>
        
    </form>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-12 21:01:54

根据Selector Reference的说法,我没有看到任何只使用CSS来做这件事的方法。但它可以用纯Javascript来完成。

代码语言:javascript
复制
const divShow = document.querySelector('.hide');
function showUp(){
  
  divShow.style.display = "block";
  divShow.style.color = "red";
}
function hide(){
  divShow.style.display = "none";
  
}
代码语言:javascript
复制
  @import url("https://fonts.googleapis.com/css?family=Lato:400,700");
        
        .rating-wrapper {
          max-width: 400px;
          margin: 80px auto;
          background: #fff;
          padding: 1em;
          border-radius: 3px;
          box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.1);
        }
        .rating-wrapper .rating-label {
          text-align: center;
          font-weight: 700;
          display: block;
          position: relative;
        }
        .rating-wrapper .ratingItemList {
          max-width: 300px;
          margin: auto;
          display: flex;
          justify-content: space-between;
          padding: 1em 0;
        }
        .rating-wrapper input.rating {
          display: none;
        }
        .rating-wrapper label.rating {
          padding: 5px 3px;
          font-size: 32px;
          opacity: 0.7;
          filter: grayscale(1);
          cursor: pointer;
        }
        .rating-wrapper label.rating:hover {
          filter: grayscale(0.84);
          transform: scale(1.1);
          transition: 100ms ease;
        }
        .rating-wrapper input.rating:checked + label.rating {
          filter: grayscale(0);
          opacity: 1;
          transform: scale(1.1);
        }
        
        .hide {
          display: none;
        }
代码语言:javascript
复制
<link rel='stylesheet' href='https://afeld.github.io/emoji-css/emoji.css'>
<form class="rating-wrapper">
      <label class="rating-label">How helpful was this?
        <div class="ratingItemList">
          
          <input class="rating rating-4" id="rating-4-2" type="radio" value="4" name="rating"/>
          
          <label onmouseover="showUp()" onmouseleave="hide()"  class="rating rating-4" for="rating-4-2"><i class="em em-grinning"></i>
            
          </label>
         
          
          <input class="rating rating-5" id="rating-5-2" type="radio" value="5" name="rating"/>
          <label onmouseover="showUp()" onmouseleave="hide()" class="rating rating-5" for="rating-5-2"><i class="em em-star-struck"></i></labe>
          
        </div>
      </label>
      
      <label onmouseover="showUp()" onmouseleave="hide()" class="myDIV">Hello world</label>
      <div class="hide"><br>I am shown when someone hovers over the image above.</div>
      
        
    </form>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69153826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档