html - 背景剪辑文本在 IE 中不起作用

标签 html css

我需要以渐变格式显示文本。下面是例子

HTML

<div class="banner">Free account</div>

CSS

.banner{
    font-family: Univers LT Std Bold;
    font-size: 18pt;
    font-weight: bold;
    /* background: lightblue; */
    background: -webkit-linear-gradient(right,#00853f 20%, #8cc63f 80%);
    background: -ms-linear-gradient(right,#00853f 20%, #8cc63f 80%);
    color: transparent;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

问题出在 IE 中的“背景剪辑:文本;”不管用。请建议如何解决这个问题,或者请建议是否有任何替代方法。

最佳答案

根据 http://caniuse.com/#search=background-clip , background-clip 仅在 IE11 或更高版本中支持。它不适用于早期版本的 IE。

此外,从您共享的代码来看,您实际上并没有使用 background-clip 属性,而是使用 -webkit-background-clip。该供应商前缀属性仅适用于 Webkit 浏览器(例如 Chrome、Safari)。您还需要添加标准属性。

有 polyfill 可以帮助解决这个问题。这是来自 https://codepen.io/TimPietrusky/pen/cnvBk 的示例:

 /**
  -webkit-background-clip: text Polyfill
  
  # What? #
  A polyfill which replaces the specified element with a SVG
  in browser where "-webkit-background-clip: text" 
  is not available.

  Fork it on GitHub
  https://github.com/TimPietrusky/background-clip-text-polyfill

  # 2013 by Tim Pietrusky
  # timpietrusky.com
**/

Element.prototype.backgroundClipPolyfill = function () {
  var a = arguments[0],
      d = document,
      b = d.body,
      el = this;

  function hasBackgroundClip() {
    return b.style.webkitBackgroundClip != undefined;
  };
  
  function addAttributes(el, attributes) {
    for (var key in attributes) {
      el.setAttribute(key, attributes[key]);
    }
  }
  
  function createSvgElement(tagname) {
    return d.createElementNS('http://www.w3.org/2000/svg', tagname);
  }
  
  function createSVG() {
    var a = arguments[0],
        svg = createSvgElement('svg'),
        pattern = createSvgElement('pattern'),
        image = createSvgElement('image'),
        text = createSvgElement('text');
    
    // Add attributes to elements
    addAttributes(pattern, {
      'id' : a.id,
      'patternUnits' : 'userSpaceOnUse',
      'width' : a.width,
      'height' : a.height
    });
    
    addAttributes(image, {
      'width' : a.width,
      'height' : a.height
    });
    image.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', a.url);
    
    addAttributes(text, {
      'x' : 0,
      'y' : 80,
      'class' : a['class'],
      'style' : 'fill:url(#' + a.id + ');'
    });
    
    // Set text
    text.textContent = a.text;
      
    // Add elements to pattern
    pattern.appendChild(image);
      
    // Add elements to SVG
    svg.appendChild(pattern);
    svg.appendChild(text);
    
    return svg;
  };
  
  /*
   * Replace the element if background-clip
   * is not available.
   */
  if (!hasBackgroundClip()) {
    var img = new Image();
    img.onload = function() {
      var svg = createSVG({
        'id' : a.patternID,
        'url' : a.patternURL,
        'class' : a['class'],
        'width' : this.width,
        'height' : this.height,
        'text' : el.textContent
      });
      
      el.parentNode.replaceChild(svg, el);
    }
    img.src = a.patternURL;
  }
};

var element = document.querySelector('.headline'); 

/*
 * Call the polyfill
 *
 * patternID : the unique ID of the SVG pattern
 * patternURL : the URL to the background-image
 * class : the css-class applied to the SVG
 */
element.backgroundClipPolyfill({
  'patternID' : 'mypattern',
  'patternURL' : 'http://timpietrusky.com/cdn/army.png',
  'class' : 'headline'
});
body {
  font: 1em sans-serif;
  background: #fff;
  margin: 0 1em;
}

h1 {
  color: red;
  -webkit-text-fill-color: transparent;
  background: -webkit-linear-gradient(transparent, transparent), url(http://timpietrusky.com/cdn/army.png) repeat;
  background: -o-linear-gradient(transparent, transparent);
  -webkit-background-clip: text;
}

/*
 * This style will be shared with the SVG because
 * I have to replace the DOM element in Firefox. 
 * Otherwise the SVG pattern will be broken.
 */
.headline {
  font: bold 2.25em sans-serif;
}

svg {
  height: 8em;
  width: 100%;
}

/*
 * Just some styling... ignore it
 */
article {
  font-size: 1.2em;
  border-top: .15em solid #7BB03B;
  height: 100%;
  text-align: center;
}

a {
  text-decoration: none;
  color: #5794C7;
  transition: color .15s ease-in-out;
}
a:hover {
  color: #7BB03B;
}
<h1 class="headline">-webkit-background-clip: text | Polyfill</h1>

<article>
  <p>
    <a href="https://github.com/TimPietrusky/background-clip-text-polyfill" target="_blank">Fork it on GitHub</a>
  </p>
  
  <p>
    2013 by <a href="https://twitter.com/TimPietrusky" target="_blank">@TimPietrusky</a>
  </p>
</article>

关于html - 背景剪辑文本在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39275608/

相关文章:

html - 计算列表中列表项的出现次数?

html - 可以在视口(viewport)断点处将容器/行转换为容器流体/行流体吗?

jQuery 导航插件刷新整个页面。我该如何解决

jquery - 如何将值传递给 JQUery 回调方法?

javascript - 将数组从 Html 合并到 Jquery

javascript - 使用 jQuery 打开 fancybox onclick

css - 为什么这个段落元素会溢出到它下面的一个 div 上?为什么那个 div 留在原地?

html - CSS Div 以及附带的 Span

javascript - jQuery UI 可调整大小的反向

java - css 找到图像但无法将它们加载到预览中