我要找的是:

一种为角色的一半设置样式的方法。(在这种情况下,一半的字母是透明的)

我目前搜索并尝试的内容(运气不佳):

半个字符/字母的样式设置方法使用CSS或JavaScript设置字符的部分样式将CSS应用于50%的字符

下面是我试图获得的一个例子。

对此,是否存在CSS或JavaScript解决方案,还是我必须求助于图像?我宁愿不走图像路线,因为文本最终将动态生成。


更新:

既然很多人都问我为什么要塑造一个角色的一半,这就是为什么。我所在的城市最近花了25万美元为自己定义了一个新的“品牌”。这个标志就是他们想出的。许多人抱怨网站的简单和缺乏创造力,并继续这样做。我的目标是把这个网站当成一个笑话。输入“哈利法克斯”,你就会明白我的意思。


当前回答

现在在GitHub上作为插件!

请随意选择并改进。

演示|下载Zip|Half-Style.com(重定向到GitHub)


单个字符的纯CSSJavaScript用于跨文本或多个字符的自动化为盲人或视觉用户保留屏幕阅读器的文本可访问性受损

第1部分:基本解决方案

演示:http://jsfiddle.net/arbel/pd9yB/1694/


这适用于任何动态文本或单个字符,并且都是自动化的。你所需要做的就是在目标文本上添加一个类,剩下的就处理好了。

此外,为盲人或视力受损的屏幕阅读器保留了原文的可访问性。

单个字符的解释:

纯CSS。您所需要做的就是将.halfStyle类应用于包含要设置为半样式的字符的每个元素。

对于包含字符的每个span元素,您可以创建一个数据属性,例如,这里datacontent=“X”,在伪元素上使用content:attr(datacontent);所以.halfStyle:before类将是动态的,您不需要为每个实例硬编码。

任何文本的解释:

只需将textToHalfStyle类添加到包含文本的元素中。


//自动模式的jQueryjQuery(函数($){var文本,字符,$el,i,输出;//遍历所有类出现$('.textToHalfStyle').each(函数(idx,el){$el=$(el);text=$el.text();chars=text.split('');//设置屏幕阅读器文本$el.html('<span style=“position:absolute!important;clip:rect(1px 1px 1fx 1px);clip:direct(1fx,1px,1px)”>'+text+'</span>');//重置附加输出输出=“”;//遍历文本中的所有字符对于(i=0;i<chars.length;i++){//为每个字符创建样式元素并附加到容器output+='<span aria hidden=“true”class=“halfStyle”数据content=“'+chars[i]+'”>'+chars[i]+'</span>';}//仅写入DOM一次$el.append(输出);});});半样式{位置:相对;显示:内联块;字体大小:80px;/*或者任何字体大小都可以*/颜色:黑色;/*或透明,任何颜色*/溢出:隐藏;空白:pre;/*以防止空间塌陷*/}半样式:之前{显示:块;z指数:1;位置:绝对;顶部:0;左:0;宽度:50%;内容:属性(数据内容);/*伪元素的动态内容*/溢出:隐藏;颜色:#f00;}<script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js“></script><p>单个字符:</p><span class=“halfStyle”数据content=“X”>X</span><span class=“halfStyle”数据content=“Y”>Y</span><span class=“halfStyle”数据content=“Z”>Z</span><span class=“halfStyle”数据content=“A”>A</span><hr/><p>自动化:</p>请选择半样式</span>

(JSFiddle演示)


第2部分:高级解决方案-独立的左侧和右侧部分

使用此解决方案,您可以单独和独立地设置左侧和右侧零件的样式。

一切都是一样的,只有更高级的CSS才有魔力。

jQuery(函数($){var文本,字符,$el,i,输出;//遍历所有类出现$('.textToHalfStyle').each(函数(idx,el){$el=$(el);text=$el.text();chars=text.split('');//设置屏幕阅读器文本$el.html('<span style=“position:absolute!important;clip:rect(1px 1px 1fx 1px);clip:direct(1fx,1px,1px)”>'+text+'</span>');//重置附加输出输出=“”;//遍历文本中的所有字符对于(i=0;i<chars.length;i++){//为每个字符创建样式元素并附加到容器output+='<span aria hidden=“true”class=“halfStyle”数据content=“'+chars[i]+'”>'+chars[i]+'</span>';}//仅写入DOM一次$el.append(输出);});});半样式{位置:相对;显示:内联块;字体大小:80px;/*或者任何字体大小都可以*/颜色:透明;/*隐藏基础字符*/溢出:隐藏;空白:pre;/*以防止空间塌陷*/}.halfStyle:在{/*创建左侧部分之前*/显示:块;z指数:1;位置:绝对;顶部:0;宽度:50%;内容:属性(数据内容);/*伪元素的动态内容*/溢出:隐藏;指针事件:无;/*所以基本字符可以通过鼠标选择*/颜色:#f00;/*用于演示目的*/文本阴影:2px-2px 0px#af0;/*用于演示目的*/}.halfStyle:在{/*创建正确的部分之后*/显示:块;方向:rtl;/*非常重要,将使宽度从右侧开始*/位置:绝对;z指数:2;顶部:0;左:50%;宽度:50%;内容:属性(数据内容);/*伪元素的动态内容*/溢出:隐藏;指针事件:无;/*所以基本字符可以通过鼠标选择*/颜色:#000;/*用于演示目的*/文本阴影:2px 2px 0px#0af;/*用于演示目的*/}<script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js“></script><p>单个字符:</p><span class=“halfStyle”数据content=“X”>X</span><span class=“halfStyle”数据content=“Y”>Y</span><span class=“halfStyle”数据content=“Z”>Z</span><span class=“halfStyle”数据content=“A”>A</span><hr/><p>自动化:</p>请选择半样式</span>

(JSFiddle演示)



Part 3: Mix-Match and Improve

Now that we know what is possible, let's create some variations.


-Horizontal Half Parts

  • Without Text Shadow:

  • Possibility of Text Shadow for each half part independently:

// 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: 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 */
}
<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)



-Vertical 1/3 Parts

  • Without Text Shadow:

  • Possibility of Text Shadow for each 1/3 part independently:

// 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 { /* 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 */
}
<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)



-Horizontal 1/3 Parts

  • Without Text Shadow:

  • Possibility of Text Shadow for each 1/3 part independently:

// 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 { /* 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 */
}
<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)



-HalfStyle Improvement By @KevinGranger

// 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);
  });
});
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.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)



-PeelingStyle improvement of HalfStyle by @SamTremaine

// 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: 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;
}
<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 and on samtremaine.co.uk)



Part 4: Ready for Production

Customized different Half-Style style-sets can be used on desired elements on the same page. You can define multiple style-sets and tell the plugin which one to use.

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);
    });
});
/* 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-KevinGranger
<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>

(JSFiddle demo)

其他回答

如果您愿意,也可以使用SVG执行此操作:

var title=document.querySelector('h1'),text=title.innerHTML,svgTemplate=document.querySelector('svg'),charStyle=svgTemplate.querySelector('#text');svgTemplate.style.display=“块”;var空间=0;对于(var i=0;i<text.length;i++){var x=charStyle.cloneNode();x.text内容=文本[i];svgTemplate.appendChild(x);x.setAttribute('x',空格);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“版本=”1.1“><defs id=“FooDefs”><linearGradient id=“MyGradient”x1=“0%”y1=“0%”x2=“100%”y2=“0”><停止偏移=“50%”停止颜色=“蓝色”/><停止偏移=“50%”停止颜色=“红色”/></linearGradient></defs><text y=“50%”id=“text”style=“font-size:72px;fill:url(#MyGradient)”></text></svg><h1>这不是解决方案X</h1>

http://codepen.io/nicbell/pen/jGcbq

这里是画布上的一个丑陋的实现。我尝试了这个解决方案,但结果比我预期的更糟,所以无论如何,它还是在这里。

$(“div”).each(函数){var CHARS=$(this).text().split(“”);$(this).html(“”);$.each(CHARS,函数(索引,字符){var canvas=$(“<canvas/>”).css(“宽度”,“40px”).css(“高度”,“40px”).get(0);$(“div”).append(画布);var ctx=canvas.getContext(“2d”);var gradient=ctx.createLinearGradient(0,0,130,0);gradient.addColorStop(“0”,“蓝色”);gradient.addColorStop(“0.5”,“蓝色”);gradient.addColorStop(“0.51”,“红色”);gradient.addColorStop(“1.0”,“红色”);ctx.font=“130pt Calibri”;ctx.fillStyle=渐变;ctx.fillText(字符,10,130);});});<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script><div>示例文本</div>

这是一个仅针对整行文本的CSS解决方案,而不仅仅是字符元素。

第二部分{位置:相对;顶部:2米;高度:2米;文本转换:全宽;}div:之前,div:之后{content:attr(数据内容);位置:绝对;顶部:0;右:0;底部:0;左:0;}div:之后{颜色:红色;/*单个字符的掩码。通过重复此掩码,所有字符串都将被掩码*/-webkit掩码图像:线性渐变(向右,透明0,透明.5em,白色.5em,白1em);-webkit掩码重复:repeat-x;/*向右重复面罩*/-webkit掩码大小:1em;/*单个字符的相对宽度*//*非供应商掩码设置*/遮罩图像:线性渐变(向右,透明0,透明.5em,白色.5em,白1em);掩码重复:repeat-x;掩模尺寸:1em;}/*演示目的*/input[name=“fontSize”]:第一个类型:checked ~ div{字体大小:1em;}input[name=“fontSize”]:第一个类型+input:checked ~ div{字体大小:2em;}input[name=“fontSize”]:第一个类型+input+input:checked ~ div{字体大小:3em;}字体大小:<input type=“radio”name=“fontSize”value=“1em”><input type=“radio”name=“fontSize”value=“2em”checked><input type=“radio”name=“fontSize”value=“3em”><div data content=“仅限CSS的解决方案…”></div><div data content=“在Firefox上试用!”></div>

其想法是为每个字符应用一个水平CSS掩码,隐藏其前半部分[0-0.5em],并显示后半部分[0.5em-1em]。

掩码的宽度为掩码大小:1em,以匹配字符串中第一个字符的宽度。通过使用掩码repeat:repeat-x,相同的掩码将应用于第二个、第三个字符,依此类推。

我原以为使用单格字体可以解决使用相同宽度字母的问题,但我错了。相反,我通过使用文本转换解决了这个问题:我相信,不幸的是,只有Firefox才支持全宽度。

使用相对单位em允许设计根据字体大小进行放大/缩小。

适用于所有浏览器的Vanilla JavaScript解决方案

如果Firefox不是一个选项,那么使用此脚本进行救援。

它通过为每个字符插入子跨度来工作。在每个span中,从字母的宽度(即span元素的宽度)[0%-50%]和[50%-100%]处放置一个非重复CSS掩码。

这样,我们就不再有使用相同宽度字符的限制。

常量dataElement=document.getElementById(“data”),content=dataElement.textContent,zoom=功能(fontSize){dataElement.style['font-size']=fontSize+'em';};while(dataElement.firstChild){dataElement.firstChild.remove()}对于(var i=0;i<content.length;++i){常量spanElem=document.createElement('span'),ch=含量[i];spanElem.setAttribute('data-ch',ch);spanElem.appendChild(document.createTextNode(ch==''?'\u00A0':ch));data.appendChild(spanElem);}#数据{位置:相对;顶部:2米;高度:2米;字体大小:2em;}#数据跨度{显示:内联块;位置:相对;颜色:透明;}#数据跨度:之前,#数据范围:之后{content:attr(数据ch);显示:内联块;位置:绝对;顶部:0;右:0;底部:0;左:0;文本对齐:居中;颜色:初始;}#数据范围:之后{颜色:红色;-webkit掩码图像:线性渐变(向右,透明0,透明50%,白色50%,白色100%);掩模图像:线性梯度(向右,透明0,透明50%,白色50%,白色100%);}字体大小:<input-type=“range”min=1 max=4 step=0.05 value=2 oninput=“zoom(this.value)”onchange=“zoom(this.value)”><div id=“data”>后备解决方案。。。对于所有浏览器</div>

有限的CSS和jQuery解决方案

我不知道这个解决方案有多优雅,但它将所有事情都削减了一半:http://jsfiddle.net/9wxfY/11/

否则,我为您创建了一个很好的解决方案。。。您需要做的就是为HTML设置以下内容:

请查看截至2016年6月13日的最新且准确的编辑:http://jsfiddle.net/9wxfY/43/

至于CSS,它非常有限。。。您只需将其应用于:第n个孩子(偶数)

$(函数){var$hc=$('.half color');var str=$hc.text();$hc.html(“”);变量i=0;var字符;var dupText;而(i<str.length){chars=str[i];如果(chars==“”)chars=“”;dupText=“<span>”+字符+“</span>”;var firstHalf=$(dupText);var secondHalf=$(dupText);$hc.append(前半部分)$hc.append(secondHalf)var width=firstHalf.width()/2;firstHalf.width(宽度);secondHalf.css('text-indent',-width);i++;}});半色跨度{字体大小:2em;显示:内联块;溢出:隐藏;}半色跨度:第n个子级(偶数){颜色:红色;}<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script>这是一个句子</div>

另一种仅使用CSS的解决方案(如果您不想编写特定于字母的CSS,则需要数据属性)。这一款更全面(已测试IE 9/10、Chrome最新版本和FF最新版本)

跨度{位置:相对;颜色:rgba(50,50200,0.5);}跨度:之前{content:attr(数据字符);位置:绝对;宽度:50%;溢出:隐藏;颜色:rgb(50,50200);}<span data char=“X”>X</span>