我刚刚玩@ Arbel的解决方案:
var textToHalfStyle = $('.textToHalfStyle').text(); var textToHalfStyleChars = textToHalfStyle.split(''); $('.textToHalfStyle').html(''); $.each(textToHalfStyleChars, function(i,v){ $('.textToHalfStyle').append('<span class="halfStyle" data-content="' + v + '">' + v + '</span>'); });
body{ background-color: black; } .textToHalfStyle{ display:block; margin: 200px 0 0 0; text-align:center; } .halfStyle { font-family: 'Libre Baskerville', serif; position:relative; display:inline-block; width:1; font-size:70px; color: black; overflow:hidden; white-space: pre; text-shadow: 1px 2px 0 white; } .halfStyle:before { display:block; z-index:1; position:absolute; top:0; width: 50%; content: attr(data-content); /* dynamic content for the pseudo element */ overflow:hidden; color: white; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <span class="textToHalfStyle">Dr. Jekyll and M. Hide</span>
的 编辑(2017年10月): background-clip 更确切地说 background-image options 现在每个主要浏览器都支持: 我可以用吗 强>
background-clip
background-image options
是的,你可以只用一个字符,只用CSS。
但是,仅限Webkit(和Chrome):
http://jsbin.com/rexoyice/1/
h1 { display: inline-block; margin: 0; /* for demo snippet */ line-height: 1em; /* for demo snippet */ font-family: helvetica, arial, sans-serif; font-weight: bold; font-size: 300px; background: linear-gradient(to right, #7db9e8 50%,#1e5799 50%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
<h1>X</h1>
在视觉上,所有使用两个字符的例子(通过JS,CSS伪元素或只是HTML)看起来很好,但请注意,所有这些都会向DOM添加可能导致可访问性的内容 - 以及文本选择/剪切/粘贴问题。
它可能是无关紧要的,也许不是,但不久前,我创建了一个jQuery函数,它可以完成同样的事情,但是是水平的。
我将它称为“Strippex”对于'stripe'+'text',演示: http://cdpn.io/FcIBg
我不是说这是任何问题的解决方案,但我已经尝试将css应用于一半的角色,但横向,所以这个想法是一样的,实现可能是可怕的,但它的确有效。
啊,最重要的是,我很开心创造它!
另一种仅限CSS的解决方案(如果您不想编写特定于字母的CSS,则需要数据属性)。这个更全面的工作(测试IE 9/10,Chrome最新和FF最新)
span { position: relative; color: rgba(50,50,200,0.5); } span:before { content: attr(data-char); position: absolute; width: 50%; overflow: hidden; color: rgb(50,50,200); }
<span data-char="X">X</span>
如果您愿意,也可以使用SVG执行此操作:
var title = document.querySelector('h1'), text = title.innerHTML, svgTemplate = document.querySelector('svg'), charStyle = svgTemplate.querySelector('#text'); svgTemplate.style.display = 'block'; var space = 0; for (var i = 0; i < text.length; i++) { var x = charStyle.cloneNode(); x.textContent = text[i]; svgTemplate.appendChild(x); x.setAttribute('x', space); space += x.clientWidth || 15; } title.innerHTML = ''; title.appendChild(svgTemplate);
<svg style="display: none; height: 100px; width: 100%" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"> <defs id="FooDefs"> <linearGradient id="MyGradient" x1="0%" y1="0%" x2="100%" y2="0%"> <stop offset="50%" stop-color="blue" /> <stop offset="50%" stop-color="red" /> </linearGradient> </defs> <text y="50%" id="text" style="font-size: 72px; fill: url(#MyGradient)"></text> </svg> <h1>This is not a solution X</h1>
http://codepen.io/nicbell/pen/jGcbq
您可以使用以下代码。这个例子我在这里使用过 h1 标记并添加了一个属性 data-title-text="Display Text" 将出现不同颜色的文字 h1 标记文本元素,它生成半色调文本,如下例所示
h1
data-title-text="Display Text"
body { text-align: center; margin: 0; } h1 { color: #111; font-family: arial; position: relative; font-family: 'Oswald', sans-serif; display: inline-block; font-size: 2.5em; } h1::after { content: attr(data-title-text); color: #e5554e; position: absolute; left: 0; top: 0; clip: rect(0, 1000px, 30px, 0); }
<h1 data-title-text="Display Text">Display Text</h1>
我们只使用CSS伪选择器来做到这一点!
此技术适用于动态生成的内容和不同的字体大小和宽度。
的 HTML: 强>
<div class='split-color'>Two is better than one.</div>
的 CSS: 强>
.split-color > span { white-space: pre-line; position: relative; color: #409FBF; } .split-color > span:before { content: attr(data-content); pointer-events: none; /* Prevents events from targeting pseudo-element */ position: absolute; overflow: hidden; color: #264A73; width: 50%; z-index: 1; }
要包装动态生成的字符串,您可以使用如下函数:
// Wrap each letter in a span tag and return an HTML string // that can be used to replace the original text function wrapString(str) { var output = []; str.split('').forEach(function(letter) { var wrapper = document.createElement('span'); wrapper.dataset.content = wrapper.innerHTML = letter; output.push(wrapper.outerHTML); }); return output.join(''); } // Replace the original text with the split-color text window.onload = function() { var el = document.querySelector('.split-color'), txt = el.innerHTML; el.innerHTML = wrapString(txt); }
.halfStyle { position:relative; display:inline-block; font-size:68px; /* or any font size will work */ color: rgba(0,0,0,0.8); /* or transparent, any color */ overflow:hidden; white-space: pre; /* to preserve the spaces from collapsing */ transform:rotate(4deg); -webkit-transform:rotate(4deg); text-shadow:2px 1px 3px rgba(0,0,0,0.3); } .halfStyle:before { display:block; z-index:1; position:absolute; top:-0.5px; left:-3px; width: 100%; content: attr(data-content); /* dynamic content for the pseudo element */ overflow:hidden; color: white; transform:rotate(-4deg); -webkit-transform:rotate(-4deg); text-shadow:0 0 1px black; }
http://experimental.samtremaine.co.uk/half-style/
你可以将这段代码撬开来做各种有趣的事情 - 这只是我的同事和我昨晚想出来的一个实现。
一个很好的WebKit解决方案,利用了 background-clip: text 支持: http://jsfiddle.net/sandro_paganotti/wLkVt/
background-clip: text
span{ font-size: 100px; background: linear-gradient(to right, black, black 50%, grey 50%, grey); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
如果你对此感兴趣,那么Lucas Bebber的Glitch是一个非常相似和超酷的效果:
使用简单的SASS Mixin创建
.example-one { font-size: 100px; @include textGlitch("example-one", 17, white, black, red, blue, 450, 115); }
更多细节在 Chris Coyer的CSS技巧 和 Lucas Bebber的Codepen页面
对于较短的文字,这样的事情怎么样?
如果您使用循环执行某些操作,使用JavaScript重复这些字符,它甚至可以用于更长的文本。无论如何,结果是这样的:
p.char { position: relative; display: inline-block; font-size: 60px; color: red; } p.char:before { position: absolute; content: attr(char); width: 50%; overflow: hidden; color: black; }
<p class="char" char="S">S</p> <p class="char" char="t">t</p> <p class="char" char="a">a</p> <p class="char" char="c">c</p> <p class="char" char="k">k</p> <p class="char" char="o">o</p> <p class="char" char="v">v</p> <p class="char" char="e">e</p> <p class="char" char="r">r</p> <p class="char" char="f">f</p> <p class="char" char="l">l</p> <p class="char" char="o">o</p> <p class="char" char="w">w</p>
只是为了历史记录!
我在5 - 6年前为自己的工作提出了一个解决方案 Gradext (纯javascript和纯css,没有依赖)。
技术说明是你可以创建一个这样的元素:
<span>A</span>
现在,如果要在文本上创建渐变,则需要创建一些多个图层,每个图层都单独进行着色,并且创建的光谱将说明渐变效果。
例如,看看这个词 LOREM 里面的 <span> 并会产生水平渐变效果( 检查示例 ):
<span>
<span data-i="0" style="color: rgb(153, 51, 34);">L</span> <span data-i="1" style="color: rgb(154, 52, 35);">o</span> <span data-i="2" style="color: rgb(155, 53, 36);">r</span> <span data-i="3" style="color: rgb(156, 55, 38);">e</span> <span data-i="4" style="color: rgb(157, 56, 39);">m</span>
并且你可以继续长时间和长段的这种模式。
如果你想创建一个怎么办? 的 垂直梯度 强> 对文本的影响?
然后是另一个可能有用的解决方案。我会详细描述。
假设我们的第一个 <span> 再次。但内容不应该是单独的字母;内容应该是全文,现在我们要复制一样的?? <span> 一次又一次(跨度的数量将定义渐变的质量,更多的跨度,更好的结果,但性能不佳)。看看这个:
<span data-i="6" style="color: rgb(81, 165, 39); overflow: hidden; height: 11.2px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span> <span data-i="7" style="color: rgb(89, 174, 48); overflow: hidden; height: 12.8px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span> <span data-i="8" style="color: rgb(97, 183, 58); overflow: hidden; height: 14.4px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span> <span data-i="9" style="color: rgb(105, 192, 68); overflow: hidden; height: 16px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span> <span data-i="10" style="color: rgb(113, 201, 78); overflow: hidden; height: 17.6px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span> <span data-i="11" style="color: rgb(121, 210, 88); overflow: hidden; height: 19.2px;">Lorem ipsum dolor sit amet, tincidunt ut laoreet dolore magna aliquam erat volutpat.</span>
如果你想让这些渐变效果移动并从中创建动画怎么办?
好吧,还有另一种解决方案。你一定要检查一下 animation: true 甚至 .hoverable() 根据光标位置导致渐变开始的方法! (听起来很酷xD)
animation: true
.hoverable()
这就是我们如何在文本上创建渐变(线性或径向)。如果您喜欢这个想法或想了解更多信息,请查看提供的链接。
也许这不是最好的选择,也许不是最好的方式,但它会开辟一些空间来创造令人兴奋和令人愉快的动画,以激励其他人获得更好的解决方案。
它允许您在文本上使用渐变样式,即使是IE8也支持!
在这里,您可以找到一个现场演示 原始存储库是 这里也是GitHub,开源 并准备得到一些更新(:D)
这是我第一次(是的,5年之后,你已经听到了它)在互联网上的任何地方提到这个存储库,我很兴奋!