添加文本悬停图像,不带html

时间:2017-09-22 12:54:44

标签: javascript html css3 hover

我无权编辑html文件,只能编辑css和js。我想对某些图像使用悬停效果,以便弹出居中的文本。我知道图像的div类但不能在体内添加文本,我怎么能通过js或css来做呢? 我应该使用像

这样的东西吗?

element.getElementsByClassName(类名)

然后使用css?

来自devtools:

<head>
  <base href="/home/">
  <title>TITLE</title>
  <meta name="description" content="BLA BLA BLA">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta name="site-id" content="19750">
  <meta name="site-path" content="/home">
  <meta name="site-update-url" content="https://node.sv-cdn.com/notifications">
  <link rel="icon" type="image/png" href="/favicon.png">

  <link rel="stylesheet" media="all" href="//smallvictories-production-assets.s3.amazonaws.com/assets/feed-23dad8deef60c63dc6e181346844e7439ba4f76c25ed84e0fb8bd63d3ce2023f.css">
  <link rel="stylesheet" href="./_sv_custom.css">
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.js"></script>
  <!--[if lt IE 9]>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
  <![endif]-->
<style>html.turbolinks-progress-bar::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  z-index: 2000;
  background-color: #0076ff;
  height: 3px;
  opacity: 0.99;
  width: 0%;
  transition: width 300ms ease-out, opacity 150ms ease-in;
  transform: translate3d(0,0,0);
}</style><link rel="stylesheet" type="text/css" href="chrome-extension://onhiacboedfinnofagfgoaanfedhmfab/dist/contentStyles.css"></head>
<body id="home" class="site-19750 page  home home feed" data-no-turbolink="">
  <div class="sv-loader" id="sv-loader"><div class="sv-loader-loading"></div></div>
  <div class="sv-theme">
    <div class="sv-feed">
  <div class="sv-feed-info" id="sv-feed-info" style="display: none;">
    <div class="sv-feed-info-inner">
      <h1 class="sv-feed-title sv-feed-info-title">WOLOBAN</h1>
      <div class="sv-feed-description sv-feed-info-description"><p>BLA BLA BLA</p></div>
    </div>
  </div>
  <a href="#sv-feed-info" class="sv-control sv-feed-control" data-sv-toggle="Info">Info</a>
  <div class="sv-feed-inner">
    <div class="sv-feed-block">
      <div class="sv-feed-meta">
        <h1 class="sv-feed-title sv-feed-meta-title">WOLOBAN</h1>
      </div>
    </div>
      <div class="sv-feed-item sv-feed-item-0  home 01photography jpg home01photographyjpg" id="svhome01photographyjpg">
    <div class="sv-feed-item-photo">
        <a target="_blank" href="/HOME/PHOTOGRAPHY">
            <img data-src="//aws.sv-cdn.com/site_assets/splash/3735baaa999/HOME/01-PHOTOGRAPHY.jpg" alt="01 PHOTOGRAPHY" class="lazyload lazy-loaded" src="//aws.sv-cdn.com/site_assets/splash/3735baaa999/HOME/01-PHOTOGRAPHY.jpg">
</a>        <div class="sv-feed-item-photo-caption"><p>Photography</p></div>
    </div>
</div>
      <div class="sv-feed-item sv-feed-item-1  home 02video jpg home02videojpg" id="svhome02videojpg">
    <div class="sv-feed-item-photo">
        <a target="_blank" href="/HOME/VIDEO">
            <img data-src="//aws.sv-cdn.com/site_assets/splash/3755baaa999/HOME/02-VIDEO.jpg" alt="02 VIDEO" class="lazyload lazy-loaded" src="//aws.sv-cdn.com/site_assets/splash/3755baaa999/HOME/02-VIDEO.jpg">
</a>        <div class="sv-feed-item-photo-caption"><p>Video</p></div>
    </div>
</div>
  </div>
</div>
  </div>

  <script src="//smallvictories-production-assets.s3.amazonaws.com/assets/feed-786a3a45375601be731dc81462c9dcf2bba435942bb0dbe9cd6bef8dd17bc5b5.js"></script>
  <script src="./_sv_custom.js"></script>
<span id="sbmarwbthv5"></span><a style="display: none; " id="exifview-absolute-url-convert-proxy" href="dummy"></a></body>
sv-feed-item sv-feed-item-0 home 01photography jpg home01photographyjpg“id =”svhome01photographyjpg 是我要将悬停文字应用于

的图片

2 个答案:

答案 0 :(得分:1)

仅CSS样式:

.sv-feed-item-photo > a:hover:before {
  position: absolute;
  font-size: 50px;
  content: "This is the text!";
  color: #fff;
}

答案 1 :(得分:0)

这是Javascript。

const imageContainer = document.querySelector('#svhome01photographyjpg');
imageContainer.appendChild('<div class="my-text-over-image">my text just here</div>')

和css(您可以在类中添加属性以对文本进行操作):

#svhome01photographyjpg {
   position: relative;
   ...
}

.my-text-over-image {
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%, -50%);
   z-index: 2;
}