webkit 上的 -moz-background-inline-policy

发布于 2024-10-28 20:13:39 字数 2603 浏览 2 评论 0原文

Webkit 中是否存在类似 -moz-background-inline-policy 的东西?此属性基本上使您有机会指定内联元素的每一行的背景应如何呈现。我在不同浏览器上附加相同元素的图像。

这是 Firefox 上的结果(使用 -moz-background-inline-policy:each-box;

Firefox

这是在 Google Chrome 上(我尝试过 -webkit-background-inline-policy,但它似乎不存在)

Google Chrome

更新

由于 Webkit 上没有后台策略属性,我试图使用 jQuery 找到不同的解决方案。我在每行文本后面添加一个额外的跨度。没关系,除了文本测量不正确之外。您可以在此处查看两个示例:

  1. 原始解决方案(后台内联策略):http://jsfiddle。 net/notme/mCnGy/5/
  2. 新解决方案(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;)

Firefox

This is on Google Chrome (I've tried -webkit-background-inline-policy, but it seems it doesn't exist)

Google Chrome

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:

  1. Original solution (background inline policy): http://jsfiddle.net/notme/mCnGy/5/
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏至、离别 2024-11-04 20:13:39

这可行,但可能会更有效:

http://jsfiddle.net/thirtydot/UGBXD/ 3/

这是后代的完整代码:

JS:

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

var fixIt = function() {
    $('.dummy').remove();

    $('div.node-title-text').each(function(index) {
        //remove previous .dummy
        var dummy = $(this).clone().removeClass().addClass('dummy').appendTo($(this).parent());
        $(dummy).wrapLines('<span><span>', '</span></span>');
    });
};
$(window).resize(fixIt).resize(); //trigger resize event onLoad

CSS:

.node-title {
    position: relative;
    margin-bottom: 16px
}
.node-title-text {
    position: relative;
    z-index: 2
}
.dummy {
    position: absolute;
    top: 0; left: 0;
    z-index: 1
}
.dummy > span {
    background: url('https://i.sstatic.net/HPofR.png') top left no-repeat,
        url('https://i.sstatic.net/C8ImH.png') top right no-repeat,
        url('https://i.sstatic.net/9is9r.png') top center repeat-x;
}
.dummy > span > span {
    visibility: hidden
}

HTML: (与你的相同)

<div class="node-title">
    <div class="node-title-text">
        <a href="#">Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            Sed suscipit fermentum leo eu scelerisque.</a>
    </div>
</div>

This works, but it could probably be made more efficient:

http://jsfiddle.net/thirtydot/UGBXD/3/

Here's the complete code for posterity:

JS:

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

var fixIt = function() {
    $('.dummy').remove();

    $('div.node-title-text').each(function(index) {
        //remove previous .dummy
        var dummy = $(this).clone().removeClass().addClass('dummy').appendTo($(this).parent());
        $(dummy).wrapLines('<span><span>', '</span></span>');
    });
};
$(window).resize(fixIt).resize(); //trigger resize event onLoad

CSS:

.node-title {
    position: relative;
    margin-bottom: 16px
}
.node-title-text {
    position: relative;
    z-index: 2
}
.dummy {
    position: absolute;
    top: 0; left: 0;
    z-index: 1
}
.dummy > span {
    background: url('https://i.sstatic.net/HPofR.png') top left no-repeat,
        url('https://i.sstatic.net/C8ImH.png') top right no-repeat,
        url('https://i.sstatic.net/9is9r.png') top center repeat-x;
}
.dummy > span > span {
    visibility: hidden
}

HTML: (same as yours)

<div class="node-title">
    <div class="node-title-text">
        <a href="#">Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            Sed suscipit fermentum leo eu scelerisque.</a>
    </div>
</div>
喜你已久 2024-11-04 20:13:39

其他浏览器未实现此非标准功能。您需要将每一行包裹在一个跨度中,然后将背景应用到该跨度。

如果您需要动态文本,您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文