webkit 上的 -moz-background-inline-policy
Webkit 中是否存在类似 -moz-background-inline-policy
的东西?此属性基本上使您有机会指定内联元素的每一行的背景应如何呈现。我在不同浏览器上附加相同元素的图像。
这是 Firefox 上的结果(使用 -moz-background-inline-policy:each-box;
)
这是在 Google Chrome 上(我尝试过 -webkit-background-inline-policy
,但它似乎不存在)
更新
由于 Webkit 上没有后台策略属性,我试图使用 jQuery 找到不同的解决方案。我在每行文本后面添加一个额外的跨度。没关系,除了文本测量不正确之外。您可以在此处查看两个示例:
- 原始解决方案(后台内联策略):http://jsfiddle。 net/notme/mCnGy/5/
- 新解决方案(jQuery 跨度): http:// jsfiddle.net/notme/my3br/1/
解决方案
//thanks @Peter Bailey - http://stackoverflow.com/questions/2456442/how-can-i-highlight-the-line-of-text-that-is-closest-to-the-mouse/2456582#2456582
jQuery.fn.wrapLines = function(openTag, closeTag) {
var dummy = this.clone().css({
top: -9999,
left: -9999,
position: 'absolute',
width: this.width()
}).appendTo(this.parent()),
text = dummy.text().match(/\S+\s+/g);
var words = text.length,
lastTopOffset = 0,
lines = [],
lineText = '';
for (var i = 0; i < words; ++i) {
dummy.html(
text.slice(0, i).join('') + text[i].replace(/(\S)/, '$1<span/>') + text.slice(i + 1).join(''));
var topOffset = jQuery('span', dummy).offset().top;
if (topOffset !== lastTopOffset && i != 0) {
lines.push(lineText);
lineText = text[i];
} else {
lineText += text[i];
}
lastTopOffset = topOffset;
}
lines.push(lineText);
this.html(openTag + lines.join(closeTag + openTag) + closeTag);
dummy.remove();
};
//thanks @thirtydot
var fixIt = function() {
//remove previous .dummy
$('.dummy').remove();
$('div.node-title-text').each(function(index) {
var dummy = $(this).clone().removeClass().addClass('dummy').appendTo($(this).parent());
console.log(dummy);
$(dummy).wrapLines('<span><span>', '</span></span>');
var padding = 15;
dummy.css({
left: -padding,
right: -padding
}).find(' > span').css('padding-left', padding*2);
});
};
$(window).load(function(){
$(window).resize(fixIt).resize(); //trigger resize event onLoad
});
Does something like -moz-background-inline-policy
exist in Webkit? This property basically gives you the opportunity to specify how should background render for each line of an inline element. I attach to images of the same element on different browsers.
This is the result on firefox (with -moz-background-inline-policy: each-box;
)
This is on Google Chrome (I've tried -webkit-background-inline-policy
, but it seems it doesn't exist)
UPDATE
Since there is no background policy property on Webkit, I'm trying to find a different solution using jQuery. I'm adding an extra span behind each line of text. It's ok, except for the fact that text is not measured properly. You can see both examples in action here:
- Original solution (background inline policy): http://jsfiddle.net/notme/mCnGy/5/
- New solution (jQuery spans): http://jsfiddle.net/notme/my3br/1/
SOLUTION
//thanks @Peter Bailey - http://stackoverflow.com/questions/2456442/how-can-i-highlight-the-line-of-text-that-is-closest-to-the-mouse/2456582#2456582
jQuery.fn.wrapLines = function(openTag, closeTag) {
var dummy = this.clone().css({
top: -9999,
left: -9999,
position: 'absolute',
width: this.width()
}).appendTo(this.parent()),
text = dummy.text().match(/\S+\s+/g);
var words = text.length,
lastTopOffset = 0,
lines = [],
lineText = '';
for (var i = 0; i < words; ++i) {
dummy.html(
text.slice(0, i).join('') + text[i].replace(/(\S)/, '$1<span/>') + text.slice(i + 1).join(''));
var topOffset = jQuery('span', dummy).offset().top;
if (topOffset !== lastTopOffset && i != 0) {
lines.push(lineText);
lineText = text[i];
} else {
lineText += text[i];
}
lastTopOffset = topOffset;
}
lines.push(lineText);
this.html(openTag + lines.join(closeTag + openTag) + closeTag);
dummy.remove();
};
//thanks @thirtydot
var fixIt = function() {
//remove previous .dummy
$('.dummy').remove();
$('div.node-title-text').each(function(index) {
var dummy = $(this).clone().removeClass().addClass('dummy').appendTo($(this).parent());
console.log(dummy);
$(dummy).wrapLines('<span><span>', '</span></span>');
var padding = 15;
dummy.css({
left: -padding,
right: -padding
}).find(' > span').css('padding-left', padding*2);
});
};
$(window).load(function(){
$(window).resize(fixIt).resize(); //trigger resize event onLoad
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可行,但可能会更有效:
http://jsfiddle.net/thirtydot/UGBXD/ 3/
这是后代的完整代码:
JS:
CSS:
HTML: (与你的相同)
This works, but it could probably be made more efficient:
http://jsfiddle.net/thirtydot/UGBXD/3/
Here's the complete code for posterity:
JS:
CSS:
HTML: (same as yours)
其他浏览器未实现此非标准功能。您需要将每一行包裹在一个跨度中,然后将背景应用到该跨度。
如果您需要动态文本,您可以使用 javascript 来整理跨度,但如果可以对其进行硬编码,那么它应该不会太复杂。
This non-standard feature isn't implemented in other browsers. You'll need to wrap each line in a span, then apply the background to that span.
You could use javascript to sort out the spans for you if you needed to have dynamic text, but if it's possible to hardcode it then it shouldn't be too complex.