首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文本剪切效果,用于在已写入文本的div上显示图像。CSS

问文本剪切效果,用于在已写入文本的div上显示图像。CSS
EN

Stack Overflow用户
提问于 2019-08-09 06:39:59
回答 2查看 84关注 0票数 0

我正在制作一个显示数字时间的时钟应用程序。

问题是,为了显示时间(如在可见中),它们(相对于时间变化的数字时间数字)必须位于比其他东西更高的z-index标度上。我不能让这些数字可见,所以它们会有很多人所说的那种剪裁效果或敲击效果。

数字时间必须看起来具有裁剪效果。我可以用带有静止文本或数字的静止图像来制作裁剪效果,但不能使效果看起来像我自己的项目中添加到数字时间中的裁剪效果。

数字时间必须对可以改变的图像具有裁剪效果,但同时数字时间div必须是最高的分层z索引,而内容必须保持不变。

我可以很容易地改变到数字时代的颜色,通过写入颜色:绿色;到css,但不能使一个图像的剪裁效果。有没有可能以某种方式将图像添加到变化的数字中,而图像仅在数字所在的区域可见?

描述使用图像作为裁剪效果的网页:https://www.w3schools.com/howto/howto_css_cutout_text.asp

网页上的内容不是我需要的。需要的是相同的文本,但适用于不断变化的数字,而同时数字,或者在本例中的文本,就是它在网页上的方式。想象一下,在白色画布(实现裁剪效果的地方)和图像之间是另一个图层,例如红色。红色图层不能有任何效果,只有一个红色画布,但文本必须有该效果。

有没有人知道如何解决css的这类问题?

谢谢

EN

回答 2

Stack Overflow用户

发布于 2019-08-09 07:03:56

你可以用Javascript来实现,例如:

​

代码语言:javascript
复制
var el = document.getElementById("textYouWantToChange");

function pad(num, size) {
    var s = num+"";
    while (s.length < size) s = "0" + s;
    return s;
}

window.setInterval(function() {
  var now = new Date();
  var time = pad(now.getHours(), 2) + ":" + pad(now.getMinutes(),2) + ":" + pad(now.getSeconds(),2);
  el.innerHTML = time;
}, 1000);
代码语言:javascript
复制
.image-container {
  background-image: url("https://www.w3schools.com/howto/img_nature.jpg"); /* The image used - important! */
  background-size: cover; 
  position: relative; /* Needed to position the cutout text in the middle of the image */
  height: 300px; /* Some height */
}

.text {
  background-color: white;
  color: black;
  font-size: 10vw; /* Responsive font size */
  font-weight: bold;
  margin: 0 auto; /* Center the text container */
  padding: 10px;
  width: 50%;
  text-align: center; /* Center text */
  position: absolute; /* Position text */
  top: 50%; /* Position text in the middle */
  left: 50%; /* Position text in the middle */
  transform: translate(-50%, -50%); /* Position text in the middle */
  mix-blend-mode: screen; /* This makes the cutout text possible */
}
代码语言:javascript
复制
<div class="image-container">
  <div id="textYouWantToChange" class="text">NATURE</div>
</div>

​

票数 0
EN

Stack Overflow用户

发布于 2019-08-11 08:53:17

找到比当前选项更好的这支笔。它使用掩蔽而不是裁剪方法。

https://codepen.io/nopr/pen/Jdsbx

CSS:

代码语言:javascript
复制
   /* Housekeeping */
body {
  background: #FF0000;
  text-align: center;
  padding: 50px 0;
}

/* Reverse Mask */
.mask {

  cursor: pointer;
  display: inline-block;
  overflow: hidden;

  /* Background to mask */
  background: url("");
  background-position: 0 50%;
 -webkit-background-clip: text;
  -moz-background-clip: text;
  -o-background-clip: text;
  background-clip: text;

 /* Text size and style */
  font-size: 150px;
  font-family: Arial, sans-serif;
  font-weight: bold;
  line-height: 1;
  letter-spacing: -10px;
  padding: 0 15px;

}

/* Variants */

.one {  
  color: transparent;
  text-stroke: 25px;
  background-position: 0 25%;
  text-shadow: 0 0 15px rgba(221,221,221,0.2);
  transition: text-shadow 0.35s linear;
}
.one:hover {
  text-shadow: 0 0 10px rgba(221,221,221,1), 0 0 0 #ddd;
}

.two {  
  color: transparent;
  text-stroke: 5px;
  background-position: 0 0;
  transition: background-position 1.5s;
}
.two:hover {
  background-position: 0 50%;
}

.three {
  color: transparent;
  text-shadow: 0 0 0px #ddd;
  text-stroke: 5px;
  transition: text-shadow 0.5s;
}
.three:hover {
  text-shadow: 0 0 5px rgba(255,255,255,0.0);
}

HTML:

代码语言:javascript
复制
<h1 class="mask one">Stockholm</h1>
<h1 class="mask two">Stockholm</h1>
<h1 class="mask three">Stockholm</h1>

不幸的是,我不能将其应用于时钟。时钟变成灰色,而不是在控制台中给出错误。

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

https://stackoverflow.com/questions/57421489

复制
相关文章

相似问题

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