Mat*_*ean 2717 html javascript css css3
我在找什么:
为角色的一个HALF设置样式的方法.(在这种情况下,一半字母是透明的)
我目前搜索和尝试的内容(没有运气):
以下是我想要获得的一个例子.

是否存在CSS或JavaScript解决方案,或者我将不得不求助于图像?我宁愿不去图像路线,因为这个文本最终会动态生成.
更新:
因为很多人都问过为什么我想要设计一半的角色,这就是原因.我的城市最近花了25万美元为自己定义一个新的"品牌".这个标志就是他们想出来的.许多人抱怨简单性和缺乏创造力,并继续这样做.我的目标是把这个网站想成一个笑话.输入'哈利法克斯'你会明白我的意思.
Arb*_*bel 2865
随意分叉和改进.

演示: http ://jsfiddle.net/arbel/pd9yB/1694/
这适用于任何动态文本或单个字符,并且都是自动化的.您需要做的就是在目标文本上添加一个类,其余的都要处理.
此外,对于盲人或视障人士的屏幕阅读器,保留了原始文本的可访问性.
单个字符的说明:
Pure CSS. All you need to do is to apply .halfStyle class to each element that contains the character you want to be half-styled.
For each span element containing the character, you can create a data attribute, for example here data-content="X", and on the pseudo element use content: attr(data-content); so the .halfStyle:before class will be dynamic and you won't need to hard code it for every instance.
Explanation for any text:
Simply add textToHalfStyle class to the element containing the text.
// jQuery for automated mode
jQuery(function($) {
var text, chars, $el, i, output;
// Iterate over all class occurences
$('.textToHalfStyle').each(function(idx, el) {
$el = $(el);
text = $el.text();
chars = text.split('');
// Set the screen-reader text
$el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
// Reset output for appending
output = '';
// Iterate over all chars in the text
for (i = 0; i < chars.length; i++) {
// Create a styled element for each character and append to container
output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
}
// Write to DOM only once
$el.append(output);
});
});Run Code Online (Sandbox Code Playgroud)
.halfStyle {
position: relative;
display: inline-block;
font-size: 80px; /* or any font size will work */
color: black; /* or transparent, any color */
overflow: hidden;
white-space: pre; /* to preserve the spaces from collapsing */
}
.halfStyle:before {
display: block;
z-index: 1;
position: absolute;
top: 0;
left: 0;
width: 50%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
color: #f00;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Single Characters:</p>
<span class="halfStyle" data-content="X">X</span>
<span class="halfStyle" data-content="Y">Y</span>
<span class="halfStyle" data-content="Z">Z</span>
<span class="halfStyle" data-content="A">A</span>
<hr/>
<p>Automated:</p>
<span class="textToHalfStyle">Half-style, please.</span>Run Code Online (Sandbox Code Playgroud)

With this solution you can style left and right parts, individually and independently.
Everything is the same, only more advanced CSS does the magic.
jQuery(function($) {
var text, chars, $el, i, output;
// Iterate over all class occurences
$('.textToHalfStyle').each(function(idx, el) {
$el = $(el);
text = $el.text();
chars = text.split('');
// Set the screen-reader text
$el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
// Reset output for appending
output = '';
// Iterate over all chars in the text
for (i = 0; i < chars.length; i++) {
// Create a styled element for each character and append to container
output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
}
// Write to DOM only once
$el.append(output);
});
});Run Code Online (Sandbox Code Playgroud)
.halfStyle {
position: relative;
display: inline-block;
font-size: 80px; /* or any font size will work */
color: transparent; /* hide the base character */
overflow: hidden;
white-space: pre; /* to preserve the spaces from collapsing */
}
.halfStyle:before { /* creates the left part */
display: block;
z-index: 1;
position: absolute;
top: 0;
width: 50%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
pointer-events: none; /* so the base char is selectable by mouse */
color: #f00; /* for demo purposes */
text-shadow: 2px -2px 0px #af0; /* for demo purposes */
}
.halfStyle:after { /* creates the right part */
display: block;
direction: rtl; /* very important, will make the width to start from right */
position: absolute;
z-index: 2;
top: 0;
left: 50%;
width: 50%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
pointer-events: none; /* so the base char is selectable by mouse */
color: #000; /* for demo purposes */
text-shadow: 2px 2px 0px #0af; /* for demo purposes */
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Single Characters:</p>
<span class="halfStyle" data-content="X">X</span>
<span class="halfStyle" data-content="Y">Y</span>
<span class="halfStyle" data-content="Z">Z</span>
<span class="halfStyle" data-content="A">A</span>
<hr/>
<p>Automated:</p>
<span class="textToHalfStyle">Half-style, please.</span>Run Code Online (Sandbox Code Playgroud)
现在我们知道什么是可能的,让我们创建一些变化.


// jQuery for automated mode
jQuery(function($) {
var text, chars, $el, i, output;
// Iterate over all class occurences
$('.textToHalfStyle').each(function(idx, el) {
$el = $(el);
text = $el.text();
chars = text.split('');
// Set the screen-reader text
$el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
// Reset output for appending
output = '';
// Iterate over all chars in the text
for (i = 0; i < chars.length; i++) {
// Create a styled element for each character and append to container
output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
}
// Write to DOM only once
$el.append(output);
});
});Run Code Online (Sandbox Code Playgroud)
.halfStyle {
position: relative;
display: inline-block;
font-size: 80px; /* or any font size will work */
color: transparent; /* hide the base character */
overflow: hidden;
white-space: pre; /* to preserve the spaces from collapsing */
}
.halfStyle:before { /* creates the top part */
display: block;
z-index: 2;
position: absolute;
top: 0;
height: 50%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
pointer-events: none; /* so the base char is selectable by mouse */
color: #f00; /* for demo purposes */
text-shadow: 2px -2px 0px #af0; /* for demo purposes */
}
.halfStyle:after { /* creates the bottom part */
display: block;
position: absolute;
z-index: 1;
top: 0;
height: 100%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
pointer-events: none; /* so the base char is selectable by mouse */
color: #000; /* for demo purposes */
text-shadow: 2px 2px 0px #0af; /* for demo purposes */
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Single Characters:</p>
<span class="halfStyle" data-content="X">X</span>
<span class="halfStyle" data-content="Y">Y</span>
<span class="halfStyle" data-content="Z">Z</span>
<span class="halfStyle" data-content="A">A</span>
<hr/>
<p>Automated:</p>
<span class="textToHalfStyle">Half-style, please.</span>Run Code Online (Sandbox Code Playgroud)


// jQuery for automated mode
jQuery(function($) {
var text, chars, $el, i, output;
// Iterate over all class occurences
$('.textToHalfStyle').each(function(idx, el) {
$el = $(el);
text = $el.text();
chars = text.split('');
// Set the screen-reader text
$el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
// Reset output for appending
output = '';
// Iterate over all chars in the text
for (i = 0; i < chars.length; i++) {
// Create a styled element for each character and append to container
output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
}
// Write to DOM only once
$el.append(output);
});
});Run Code Online (Sandbox Code Playgroud)
.halfStyle { /* base char and also the right 1/3 */
position: relative;
display: inline-block;
font-size: 80px; /* or any font size will work */
color: transparent; /* hide the base character */
overflow: hidden;
white-space: pre; /* to preserve the spaces from collapsing */
color: #f0f; /* for demo purposes */
text-shadow: 2px 2px 0px #0af; /* for demo purposes */
}
.halfStyle:before { /* creates the left 1/3 */
display: block;
z-index: 2;
position: absolute;
top: 0;
width: 33.33%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
pointer-events: none; /* so the base char is selectable by mouse */
color: #f00; /* for demo purposes */
text-shadow: 2px -2px 0px #af0; /* for demo purposes */
}
.halfStyle:after { /* creates the middle 1/3 */
display: block;
z-index: 1;
position: absolute;
top: 0;
width: 66.66%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
pointer-events: none; /* so the base char is selectable by mouse */
color: #000; /* for demo purposes */
text-shadow: 2px 2px 0px #af0; /* for demo purposes */
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Single Characters:</p>
<span class="halfStyle" data-content="X">X</span>
<span class="halfStyle" data-content="Y">Y</span>
<span class="halfStyle" data-content="Z">Z</span>
<span class="halfStyle" data-content="A">A</span>
<hr/>
<p>Automated:</p>
<span class="textToHalfStyle">Half-style, please.</span>Run Code Online (Sandbox Code Playgroud)


// jQuery for automated mode
jQuery(function($) {
var text, chars, $el, i, output;
// Iterate over all class occurences
$('.textToHalfStyle').each(function(idx, el) {
$el = $(el);
text = $el.text();
chars = text.split('');
// Set the screen-reader text
$el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
// Reset output for appending
output = '';
// Iterate over all chars in the text
for (i = 0; i < chars.length; i++) {
// Create a styled element for each character and append to container
output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
}
// Write to DOM only once
$el.append(output);
});
});Run Code Online (Sandbox Code Playgroud)
.halfStyle { /* base char and also the bottom 1/3 */
position: relative;
display: inline-block;
font-size: 80px; /* or any font size will work */
color: transparent;
overflow: hidden;
white-space: pre; /* to preserve the spaces from collapsing */
color: #f0f;
text-shadow: 2px 2px 0px #0af; /* for demo purposes */
}
.halfStyle:before { /* creates the top 1/3 */
display: block;
z-index: 2;
position: absolute;
top: 0;
height: 33.33%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
pointer-events: none; /* so the base char is selectable by mouse */
color: #f00; /* for demo purposes */
text-shadow: 2px -2px 0px #fa0; /* for demo purposes */
}
.halfStyle:after { /* creates the middle 1/3 */
display: block;
position: absolute;
z-index: 1;
top: 0;
height: 66.66%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
pointer-events: none; /* so the base char is selectable by mouse */
color: #000; /* for demo purposes */
text-shadow: 2px 2px 0px #af0; /* for demo purposes */
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Single Characters:</p>
<span class="halfStyle" data-content="X">X</span>
<span class="halfStyle" data-content="Y">Y</span>
<span class="halfStyle" data-content="Z">Z</span>
<span class="halfStyle" data-content="A">A</span>
<hr/>
<p>Automated:</p>
<span class="textToHalfStyle">Half-style, please.</span>Run Code Online (Sandbox Code Playgroud)

// jQuery for automated mode
jQuery(function($) {
var text, chars, $el, i, output;
// Iterate over all class occurences
$('.textToHalfStyle').each(function(idx, el) {
$el = $(el);
text = $el.text();
chars = text.split('');
// Set the screen-reader text
$el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
// Reset output for appending
output = '';
// Iterate over all chars in the text
for (i = 0; i < chars.length; i++) {
// Create a styled element for each character and append to container
output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
}
// Write to DOM only once
$el.append(output);
});
});Run Code Online (Sandbox Code Playgroud)
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;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Single Characters:</p>
<span class="halfStyle" data-content="X">X</span>
<span class="halfStyle" data-content="Y">Y</span>
<span class="halfStyle" data-content="Z">Z</span>
<span class="halfStyle" data-content="A">A</span>
<hr/>
<p>Automated:</p>
<span class="textToHalfStyle">Half-style, please.</span>Run Code Online (Sandbox Code Playgroud)

// jQuery for automated mode
jQuery(function($) {
var text, chars, $el, i, output;
// Iterate over all class occurences
$('.textToHalfStyle').each(function(idx, el) {
$el = $(el);
text = $el.text();
chars = text.split('');
// Set the screen-reader text
$el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + text + '</span>');
// Reset output for appending
output = '';
// Iterate over all chars in the text
for (i = 0; i < chars.length; i++) {
// Create a styled element for each character and append to container
output += '<span aria-hidden="true" class="halfStyle" data-content="' + chars[i] + '">' + chars[i] + '</span>';
}
// Write to DOM only once
$el.append(output);
});
});Run Code Online (Sandbox Code Playgroud)
.halfStyle {
position: relative;
display: inline-block;
font-size: 68px;
color: rgba(0, 0, 0, 0.8);
overflow: hidden;
white-space: pre;
transform: rotate(4deg);
text-shadow: 2px 1px 3px rgba(0, 0, 0, 0.3);
}
.halfStyle:before { /* creates the left part */
display: block;
z-index: 1;
position: absolute;
top: -0.5px;
left: -3px;
width: 100%;
content: attr(data-content);
overflow: hidden;
pointer-events: none;
color: #FFF;
transform: rotate(-4deg);
text-shadow: 0px 0px 1px #000;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Single Characters:</p>
<span class="halfStyle" data-content="X">X</span>
<span class="halfStyle" data-content="Y">Y</span>
<span class="halfStyle" data-content="Z">Z</span>
<span class="halfStyle" data-content="A">A</span>
<hr/>
<p>Automated:</p>
<span class="textToHalfStyle">Half-style, please.</span>Run Code Online (Sandbox Code Playgroud)
(JSFiddle演示和samtremaine.co.uk)
可以在同一页面上的所需元素上使用自定义的不同半风格样式集.您可以定义多个样式集并告诉插件使用哪个样式集.
The plugin uses data attribute data-halfstyle="[-CustomClassName-]" on the target .textToHalfStyle elements and makes all the necessary changes automatically.
So, simply on the element containing the text add textToHalfStyle class and data attribute data-halfstyle="[-CustomClassName-]". The plugin will do the rest of the job.

Also the CSS style-sets' class definitions match the [-CustomClassName-] part mentioned above and is chained to .halfStyle, so we will have .halfStyle.[-CustomClassName-]
jQuery(function($) {
var halfstyle_text, halfstyle_chars, $halfstyle_el, halfstyle_i, halfstyle_output, halfstyle_style;
// Iterate over all class occurrences
$('.textToHalfStyle').each(function(idx, halfstyle_el) {
$halfstyle_el = $(halfstyle_el);
halfstyle_style = $halfstyle_el.data('halfstyle') || 'hs-base';
halfstyle_text = $halfstyle_el.text();
halfstyle_chars = halfstyle_text.split('');
// Set the screen-reader text
$halfstyle_el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + halfstyle_text + '</span>');
// Reset output for appending
halfstyle_output = '';
// Iterate over all chars in the text
for (halfstyle_i = 0; halfstyle_i < halfstyle_chars.length; halfstyle_i++) {
// Create a styled element for each character and append to container
halfstyle_output += '<span aria-hidden="true" class="halfStyle ' + halfstyle_style + '" data-content="' + halfstyle_chars[halfstyle_i] + '">' + halfstyle_chars[halfstyle_i] + '</span>';
}
// Write to DOM only once
$halfstyle_el.append(halfstyle_output);
});
});Run Code Online (Sandbox Code Playgroud)
/* start half-style hs-base */
.halfStyle.hs-base {
position: relative;
display: inline-block;
font-size: 80px; /* or any font size will work */
overflow: hidden;
white-space: pre; /* to preserve the spaces from collapsing */
color: #000; /* for demo purposes */
}
.halfStyle.hs-base:before {
display: block;
z-index: 1;
position: absolute;
top: 0;
width: 50%;
content: attr(data-content); /* dynamic content for the pseudo element */
pointer-events: none; /* so the base char is selectable by mouse */
overflow: hidden;
color: #f00; /* for demo purposes */
}
/* end half-style hs-base */
/* start half-style hs-horizontal-third */
.halfStyle.hs-horizontal-third { /* base char and also the bottom 1/3 */
position: relative;
display: inline-block;
font-size: 80px; /* or any font size will work */
color: transparent;
overflow: hidden;
white-space: pre; /* to preserve the spaces from collapsing */
color: #f0f;
text-shadow: 2px 2px 0px #0af; /* for demo purposes */
}
.halfStyle.hs-horizontal-third:before { /* creates the top 1/3 */
display: block;
z-index: 2;
position: absolute;
top: 0;
height: 33.33%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
pointer-events: none; /* so the base char is selectable by mouse */
color: #f00; /* for demo purposes */
text-shadow: 2px -2px 0px #fa0; /* for demo purposes */
}
.halfStyle.hs-horizontal-third:after { /* creates the middle 1/3 */
display: block;
position: absolute;
z-index: 1;
top: 0;
height: 66.66%;
content: attr(data-content); /* dynamic content for the pseudo element */
overflow: hidden;
pointer-events: none; /* so the base char is selectable by mouse */
color: #000; /* for demo purposes */
text-shadow: 2px 2px 0px #af0; /* for demo purposes */
}
/* end half-style hs-horizontal-third */
/* start half-style hs-PeelingStyle, by user SamTremaine on Stackoverflow.com */
.halfStyle.hs-PeelingStyle {
position: relative;
display: inline-block;
font-size: 68px;
color: rgba(0, 0, 0, 0.8);
overflow: hidden;
white-space: pre;
transform: rotate(4deg);
text-shadow: 2px 1px 3px rgba(0, 0, 0, 0.3);
}
.halfStyle.hs-PeelingStyle:before { /* creates the left part */
display: block;
z-index: 1;
position: absolute;
top: -0.5px;
left: -3px;
width: 100%;
content: attr(data-content);
overflow: hidden;
pointer-events: none;
color: #FFF;
transform: rotate(-4deg);
text-shadow: 0px 0px 1px #000;
}
/* end half-style hs-PeelingStyle */
/* start half-style hs-KevinGranger, by user KevinGranger on StackOverflow.com*/
.textToHalfStyle.hs-KevinGranger {
display: block;
margin: 200px 0 0 0;
text-align: center;
}
.halfStyle.hs-KevinGranger {
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.hs-KevinGranger: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;
}
/* end half-style hs-KevinGrangerRun Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>
<span class="textToHalfStyle" data-halfstyle="hs-base">Half-style, please.</span>
</p>
<p>
<span class="textToHalfStyle" data-halfstyle="hs-horizontal-third">Half-style, please.</span>
</p>
<p>
<span class="textToHalfStyle" data-halfstyle="hs-PeelingStyle">Half-style, please.</span>
</p>
<p style="background-color:#000;">
<span class="textToHalfStyle" data-halfstyle="hs-KevinGranger">Half-style, please.</span>
</p>Run Code Online (Sandbox Code Playgroud)
Raz*_* B. 481

我刚刚完成了插件的开发,每个人都可以使用它!希望你会喜欢它.
首先,确保jQuery包含库.获取最新jQuery版本的最佳方法是更新头标记:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
下载文件后,请确保将它们包含在项目中:
<link rel="stylesheet" type="text/css" href="css/splitchar.css">
<script type="text/javascript" src="js/splitchar.js"></script>
Run Code Online (Sandbox Code Playgroud)
您所要做的就是对类进行设置splitchar,然后是包含文本的元素所需的样式.例如
<h1 class="splitchar horizontal">Splitchar</h1>
Run Code Online (Sandbox Code Playgroud)
完成所有这些后,只需确保在文档就绪文件中调用jQuery函数,如下所示:
$(".splitchar").splitchar();
Run Code Online (Sandbox Code Playgroud)
为了使文本看起来完全符合您的要求,您所要做的就是应用您的设计:
.horizontal { /* Base CSS - e.g font-size */ }
.horizontal:before { /* CSS for the left half */ }
.horizontal:after { /* CSS for the right half */ }
Run Code Online (Sandbox Code Playgroud)
而已!现在你已经Splitchar设置了所有插件.有关它的更多信息,请访问http://razvanbalosin.com/Splitchar.js/.
DA.*_*DA. 225
编辑(OCT 2017):
background-clip或者说background-image options现在是由每一个主要的浏览器支持:CanIUse
是的,你可以只用一个字符,只用CSS.
但是,仅限Webkit(和Chrome):
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;
}Run Code Online (Sandbox Code Playgroud)
<h1>X</h1>Run Code Online (Sandbox Code Playgroud)
在视觉上,所有使用两个字符的例子(通过JS,CSS伪元素或只是HTML)看起来很好,但请注意,所有这些都会向DOM添加可能导致可访问性的内容 - 以及文本选择/剪切/粘贴问题.
wva*_*aal 155

我们只使用CSS伪选择器来做到这一点!
此技术适用于动态生成的内容和不同的字体大小和宽度.
HTML:
<div class='split-color'>Two is better than one.</div>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
要包装动态生成的字符串,您可以使用如下函数:
// 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);
}
Run Code Online (Sandbox Code Playgroud)
Fia*_*bre 70
这是一个丑陋的画布实现.我尝试了这个解决方案,但结果比我预期的要糟糕,所以无论如何这里都是如此.

$("div").each(function() {
var CHARS = $(this).text().split('');
$(this).html("");
$.each(CHARS, function(index, char) {
var canvas = $("<canvas />")
.css("width", "40px")
.css("height", "40px")
.get(0);
$("div").append(canvas);
var ctx = canvas.getContext("2d");
var gradient = ctx.createLinearGradient(0, 0, 130, 0);
gradient.addColorStop("0", "blue");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("0.51", "red");
gradient.addColorStop("1.0", "red");
ctx.font = '130pt Calibri';
ctx.fillStyle = gradient;
ctx.fillText(char, 10, 130);
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Example Text</div>Run Code Online (Sandbox Code Playgroud)
Rus*_*kin 70
如果你对此感兴趣,那么Lucas Bebber的Glitch是一个非常相似和超酷的效果:

使用简单的SASS Mixin创建
.example-one {
font-size: 100px;
@include textGlitch("example-one", 17, white, black, red, blue, 450, 115);
}
Run Code Online (Sandbox Code Playgroud)
更多细节见Chris Coyer的CSS Tricks和Lucas Bebber的Codepen页面
Pri*_*ner 60
最接近我可以得到:
$(function(){
$('span').width($('span').width()/2);
$('span:nth-child(2)').css('text-indent', -$('span').width());
});Run Code Online (Sandbox Code Playgroud)
body{
font-family: arial;
}
span{
display: inline-block;
overflow: hidden;
}
span:nth-child(2){
color: red;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>X</span><span>X</span>Run Code Online (Sandbox Code Playgroud)
演示:http://jsfiddle.net/9wxfY/2/
这是一个只使用一个版本的版本:http://jsfiddle.net/9wxfY/4/
Shi*_*pow 52

我刚刚玩@ 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>');
});Run Code Online (Sandbox Code Playgroud)
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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
MSt*_*utt 42
另一种仅限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);
}Run Code Online (Sandbox Code Playgroud)
<span data-char="X">X</span>Run Code Online (Sandbox Code Playgroud)
Adj*_*jit 36
有限的CSS和jQuery解决方案
我不确定这个解决方案有多优雅,但它将所有内容切成两半:http://jsfiddle.net/9wxfY/11/
否则,我已经为您创建了一个很好的解决方案...您需要做的就是为您的HTML提供:
请查看截至2016年6月13日的最新,最准确的编辑内容:http://jsfiddle.net/9wxfY/43/
至于CSS,它是非常有限的......你只需要应用它 :nth-child(even)
$(function(){
var $hc = $('.half-color');
var str = $hc.text();
$hc.html("");
var i = 0;
var chars;
var dupText;
while(i < str.length){
chars = str[i];
if(chars == " ") chars = " ";
dupText = "<span>" + chars + "</span>";
var firstHalf = $(dupText);
var secondHalf = $(dupText);
$hc.append(firstHalf)
$hc.append(secondHalf)
var width = firstHalf.width()/2;
firstHalf.width(width);
secondHalf.css('text-indent', -width);
i++;
}
});Run Code Online (Sandbox Code Playgroud)
.half-color span{
font-size: 2em;
display: inline-block;
overflow: hidden;
}
.half-color span:nth-child(even){
color: red;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="half-color">This is a sentence</div>Run Code Online (Sandbox Code Playgroud)
小智 29
.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;
}
Run Code Online (Sandbox Code Playgroud)
http://experimental.samtremaine.co.uk/half-style/
你可以将这段代码撬开来做各种有趣的事情 - 这只是我的同事和我昨晚想出来的一个实现.
小智 24
一个很好的WebKit解决方案,利用了background-clip: text支持:http://jsfiddle.net/sandro_paganotti/wLkVt/
span{
font-size: 100px;
background: linear-gradient(to right, black, black 50%, grey 50%, grey);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
Run Code Online (Sandbox Code Playgroud)
Ric*_*Zea 22
FWIW,这是我对CSS的看法:http://codepen.io/ricardozea/pen/uFbts/
几点说明:
我这样做的主要原因是测试自己,看看我是否能够完成造型的一半角色,同时实际上为OP提供了有意义的答案.
我知道这不是一个理想的或最具可扩展性的解决方案,这里的人们提出的解决方案对于"真实世界"场景来说要好得多.
我创建的CSS代码基于我想到的第一个想法和我个人对问题的处理方法.
我的解决方案仅适用于对称字符,如X,A,O,M.**它不适用于不对称字符,如B,C,F,K或小写字母.
**然而,这种方法创造了非常有趣的"形状",具有不对称的字符.尝试将X更改为K或小写字母,如CSS中的h或p :)
HTML
<span class="half-letter"></span>
Run Code Online (Sandbox Code Playgroud)
SCSS
.half-character {
display: inline-block;
font: bold 350px/.8 Arial;
position: relative;
&:before, &:after {
content: 'X'; //Change character here
display: inline-block;
width: 50%;
overflow: hidden;
color: #7db9e8;
}
&:after {
position: absolute;
top: 0;
left: 50%;
color: #1e5799;
transform: rotateY(-180deg);
}
}
Run Code Online (Sandbox Code Playgroud)
Ali*_*eza 20
对于较短的文字,这样的事情怎么样?
如果您使用循环执行某些操作,使用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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
Nic*_*ell 16
如果您愿意,也可以使用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);Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
http://codepen.io/nicbell/pen/jGcbq
Sle*_*eek 14
这可以通过CSS :before选择器和content property value.
.halfed, .halfed1 {
float: left;
}
.halfed, .halfed1 {
font-family: arial;
font-size: 300px;
font-weight: bolder;
width: 200px;
height: 300px;
position: relative; /* To help hold the content value within */
overflow: hidden;
color: #000;
}
.halfed:before, .halfed1:before {
width: 50%; /* How much we'd like to show */
overflow: hidden; /* Hide what goes beyond our dimension */
content: 'X'; /* Halfed character */
height: 100%;
position: absolute;
color: #28507D;
}
/* For Horizontal cut off */
.halfed1:before {
width: 100%;
height: 55%;
}Run Code Online (Sandbox Code Playgroud)
<div class="halfed"> X </div>
<div class="halfed1"> X </div>Run Code Online (Sandbox Code Playgroud)
您可以使用以下代码.在这个例子中,我使用了h1标签并添加了一个属性data-title-text="Display Text",该属性将在h1标签文本元素上显示不同的颜色文本,从而产生半颜色文本效果,如下例所示
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);
}Run Code Online (Sandbox Code Playgroud)
<h1 data-title-text="Display Text">Display Text</h1>Run Code Online (Sandbox Code Playgroud)
只为载入史册!
我在 5-6 年前为自己的工作提出了一个解决方案,即Gradeext(纯 javascript 和纯 css,无依赖)。
技术解释是你可以创建一个这样的元素:
<span>A</span>
Run Code Online (Sandbox Code Playgroud)
现在,如果要在文本上制作渐变,则需要创建多个图层,每个图层都具有特定的颜色,所创建的光谱将说明渐变效果。
例如看看这是a 里面的词lorem<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>
Run Code Online (Sandbox Code Playgroud)
你可以继续做这个模式很长时间和很长一段。
如果要在文本上创建垂直渐变效果怎么办?
然后还有另一个可能有用的解决方案。我会详细描述。
<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>
Run Code Online (Sandbox Code Playgroud)
如果您想让这些渐变效果移动并从中创建动画怎么办?
好吧,还有另一种解决方案。您绝对应该检查animation: true甚至.hoverable()方法,这将导致基于光标位置的渐变开始!(听起来很酷 xD)
这就是我们在文本上创建渐变(线性或径向)的简单方式。如果您喜欢这个想法或想了解更多信息,您应该查看提供的链接。
也许这不是最好的选择,也许不是最好的执行方式,但它会开辟一些空间来创造令人兴奋和令人愉快的动画,以激励其他人寻求更好的解决方案。
它将允许您在文本上使用渐变样式,甚至 IE8 也支持!
在这里你可以找到一个有效的现场演示,原始存储库也在 GitHub 上,开源并准备获得一些更新 (:D)
这是我第一次(是的,5 年后,你没听错)在 Internet 上的任何地方提到这个存储库,我对此感到很兴奋!
[更新 - 2019 年 8 月:] Github 删除了该存储库的github-pages演示,因为我来自伊朗!此处仅提供源代码...
| 归档时间: |
|
| 查看次数: |
235567 次 |
| 最近记录: |