【问题标题】:CSS: Background repeat incrementCSS:背景重复增量
【发布时间】:2014-05-15 13:21:15
【问题描述】:

我有这个fiddle

在我的网页上,我的客户希望在记事本上查看他们的内容,但他们也有全宽响应式设计。

我想要实现的是在容器顶部有一行循环,但我不希望在最后显示一半,因为那看起来很糟糕。

所以在我的小提琴中,第二行很简单:

HTML

<div id="note-top2" class="note-top"></div>

CSS

#note-top2 {
    background:url(http://i.imgur.com/8hc1eSh.png) repeat-x;
}
.note-top {
    width:100%;
    height:38px;
    text-align:center;
}

这加载很快,但我们在最后有半个循环。

所以我尝试使用 jQuery 来“破解”它:

HTML

<div id="note-top" class="note-top"></div>

<img id="loop" src="http://i.imgur.com/8hc1eSh.png" alt="" />

CSS

.note-top {
    width:100%;
    height:38px;
    text-align:center;
}
#loop {
    display:none;
}

JS

function checkNoteWidth(start) {
    var el = $('#note-top');
    var loop = $('#loop');
    var elw = el.width();
    var loopw = loop.width();
    var imgCount = Math.floor(elw/loopw);
    if (start) {
        el.html('');
        for (var i=0;i<imgCount;i++) {
            el.append(loop.clone().attr('id','').addClass('loopimg'));      
        }
    } else {
        if (imgCount > $('.loopimg').length) { //if we need more images
            for (var i=0;i<(imgCount-$('.loopimg').length);i++) {
                el.append(loop.clone().attr('id','').addClass('loopimg'));      
            }
        } else if (imgCount < $('.loopimg').length) { //if we have too many images
            for (var i=0;i<($('.loopimg').length-imgCount);i++) {
                $('.loopimg').eq(i).remove();      
            }
        }
    }
}

checkNoteWidth(true)
$(window).resize(function() {
    checkNoteWidth(false);
});

但是,如果您使用它快速调整大小或在浏览器上单击最大/最小化,则它无法正常工作(太多或不够,取决于您选择的方式)。

有人对更好的方法有任何想法或建议吗?告诉背景图像仅在 10 像素后重复的某种方式?

【问题讨论】:

  • 背景尺寸% ?
  • 刚刚玩过背景尺寸,不是我真正想要的:/
  • 我什至不知道你到底想要什么结果。
  • 好的,我已经回答了我的评论,所以我猜你没有 CSS 选项
  • @JamieBarker 我没有深入研究您的 JS 代码,但如果只是在快速调整大小时在下一行显示一些闪烁元素的问题,您可以通过添加 @987654328 来快速解决@到.note-topjsfiddle.net/viphalongpro/tNJa4/6

标签: jquery html css background repeat


【解决方案1】:

如果你把它做成一个边框图像,你可能会得到更好的结果,并使用

border-image-repeat: round;

http://css-tricks.com/understanding-border-image/

【讨论】:

  • 问题在于IE compatibility :(
  • 但是,它似乎确实做了我需要它做的事情 :/
  • 您是否考虑过使用modernizr 为后备设计样式?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 2011-06-30
  • 1970-01-01
  • 2019-10-13
  • 2014-08-05
相关资源
最近更新 更多