简体   繁体   English

如何在文本上设置线性渐变?

[英]How can I set a linear gradient over text?

So this is what I want to achieve: 所以这就是我想要实现的:

(Ignore the turquoise cells) (忽略绿松石细胞)

在此处输入图片说明

As you may see, the whole text is blurry because of the gradiente which should be over the text. 如您所见,由于渐变应该覆盖整个文本,因此整个文本是模糊的。 When you click on the READ MORE button, the element should go until the bottom and remove the gradient, and when you click on READ LESS, it should get back to its original state. 当您单击READ MORE按钮时,该元素应一直走到底部并除去渐变,而当您单击READ LESS时,它应返回其原始状态。

Right now the design doesn't look like the one I did in the code and I need it exactly. 现在的设计看起来不像我在代码中所做的那样,我确实需要它。 But I am running out of ideas in order to get the same exact same thing as in the design. 但是为了获得与设计中完全相同的东西,我耗尽了所有想法。

Any suggestions? 有什么建议么?

And this is the code: 这是代码:

 $(document).ready(function(){ var toggleReadMore = function() { $('#read-more').click(function(e) { $(this).prev().animate({'height': $(this).prev()[0].scrollHeight + 'px'}, 400); $(this).hide(); $(this).next('#read-less').show(); }); $('#read-less').click(function(e) { $(this).prev().prev().animate({height: '90px'}, 400); $(this).hide(); $(this).prev('#read-more').show(); }); }; toggleReadMore(); }()); 
 #p { height: 50px; overflow: hidden; } #read-less { display: none; } #read-more, #read-less { background: linear-gradient(to bottom, rgba(255,0,0,0), rgba(255,255,255,1)); color: blue; cursor: pointer; position: absolute; bottom: -20px; padding: 15px 0; text-align: center; width: 100%; } #wrapper { position: relative; width: 400px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='wrapper'> <p id='p'>Pinterest taxidermy et heirloom, ennui enim eu bicycle rights fugiat nesciunt commodo. High Life food truck jean shorts in. Blog asymmetrical cold-pressed photo booth. Neutra chia in, mustache Etsy nostrud plaid kogi. Magna polaroid stumptown aliqua put a bird on it gentrify, street art craft beer bicycle rights skateboard. DIY plaid gentrify, sustainable sapiente seitan mumblecore viral cardigan. Nisi pariatur laborum cornhole kitsch tempor fingerstache Bushwick. </p> <div id='read-more'> READ MORE </div> <div id='read-less'> READ LESS </div> </div> 

You can achieve this with the :after state, background gradient and z-index. 您可以使用:after状态,背景渐变和z-index来实现。

.masked{
  background: white;
  z-index: 1;
  position:relative;
}

.masked:after {
  content: "";
  position: absolute;
  top:0;
  left:0;
  width:100%;
  height: 100%;
  z-index: 2;
  background: -webkit-linear-gradient(rgba(255,255,255,0) 0%, rgba(255,255,255,1) 70%);
  background: -moz-linear-gradient(rgba(255,255,255,0) 0%, rgba(255,255,255,1) 70%);
  background: -o-linear-gradient(rgba(255,255,255,0) 0%, rgba(255,255,255,1) 70%);
  background: linear-gradient(rgba(255,255,255,0) 0%, rgba(255,255,255,1) 70%);
}

here is snippet full adapted to your needs: https://jsfiddle.net/9bp1rc0e/1/ 这是完全适合您需求的代码段: https : //jsfiddle.net/9bp1rc0e/1/

Is this an example You're looking for? 这是您要找的示例吗? http://jsbin.com/guwititube/edit?html,css,js,output http://jsbin.com/guwititube/edit?html,css,js,output

If it is then you can make it readable by changing the height of the article:after with jquery onClick function attached to the "Read more" button. 如果是,则可以通过更改article:after的高度来使其可读article:after ,将jquery onClick函数附加到“阅读更多”按钮。 Potentially, changing the height of the article element itself if there's a lot of text. 如果文本很多,则有可能更改article元素本身的高度。

This example is from this thread How to apply a CSS gradient over a text, from a transparent to an opaque colour 此示例来自该线程。 如何在文本上应用CSS渐变,从透明到不透明的颜色

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

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