Feel free to fork and improve. (随意分叉和改进。)
- Pure CSS for a Single Character (单个字符的纯CSS)
- JavaScript used for automation across text or multiple characters (JavaScript用于跨文本或多个字符的自动化)
- Preserves Text Accessibility for screen readers for the blind or visually impaired (为盲人或视障者的屏幕阅读器保留文本可访问性)
Part 1: Basic Solution (第1部分:基本解决方案)
![文字的半样式](https://stackoom.com/link/aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9aWWljMS5wbmc=)
Demo: http://jsfiddle.net/arbel/pd9yB/1694/ (演示: http : //jsfiddle.net/arbel/pd9yB/1694/)
This works on any dynamic text, or a single character, and is all automated. (这适用于任何动态文本或单个字符,并且都是自动化的。) All you need to do is add a class on the target text and the rest is taken care of. (您需要做的就是在目标文本上添加一个类,其余的工作都将得到解决。)
Also, the accessibility of the original text is preserved for screen readers for the blind or visually impaired. (而且,为盲人或视障者的屏幕阅读器保留了原始文本的可访问性。)
Explanation for a single character: (单个字符的说明:)
Pure CSS. (纯CSS。) All you need to do is to apply .halfStyle
class to each element that contains the character you want to be half-styled. (您需要做的就是将.halfStyle
类应用于包含您要半样式设置的字符的每个元素。)
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);
(对于每个包含字符的span元素,您都可以创建一个data属性,例如在这里data-content="X"
,在伪元素上使用content: attr(data-content);
) so the .halfStyle:before
class will be dynamic and you won't need to hard code it for every instance. (因此.halfStyle:before
类将是动态的,并且您无需为每个实例进行硬编码。)
Explanation for any text: (任何文字的说明:)
Simply add textToHalfStyle
class to the element containing the text. (只需将textToHalfStyle
类添加到包含文本的元素中。)
// 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); }); });
.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; }
<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>
( JSFiddle demo ) (( JSFiddle演示 ))
Part 2: Advanced solution - Independent left and right parts (第2部分:高级解决方案-左右独立的部分)
![半文本样式-高级-带文本阴影](https://stackoom.com/link/aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS96WWEwRi5wbmc=)
With this solution you can style left and right parts, individually and independently . (使用此解决方案,您可以分别和独立地为左右零件定型 。)
Everything is the same, only more advanced CSS does the magic. (一切都一样,只有更高级的CSS才能发挥作用。)
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); }); });
.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 */ }
<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>
( JSFiddle demo ) (( JSFiddle演示 ))
Part 3: Mix-Match and Improve (第3部分:混搭和改进)
Now that we know what is possible, let's create some variations. (现在我们知道了可行的方法,让我们创建一些变体。)
-Horizontal Half Parts (-水平半部)
Without Text Shadow: (没有文字阴影:)
![水平半部-无文字阴影](https://stackoom.com/link/aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9pOGRwRS5wbmc=)
Possibility of Text Shadow for each half part independently: (每个部分的文本阴影的可能性分别为:)
![halfStyle-水平半部-带有文字阴影](https://stackoom.com/link/aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS8yZURhUS5wbmc=)
// 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] + '">' +
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…