简体   繁体   English

每次重复时垂直翻转背景图像

[英]Flip vertically a background-image every time it repeat-y

I'm searching for a trick that repeat my background image vertically, but every time the image is repeated I want to flip it vertically. 我正在寻找一种垂直重复背景图像的技巧,但每次重复图像时我都要垂直翻转它。

I tried all the things in my mind with repeat-y but I don't found a solution, even searching for it in google. 我在脑海里尝试了所有重复的东西,但我没有找到解决方案,甚至在谷歌搜索它。

Example: 例:

背景 Straight background-image 直背景图像

背景垂直翻转 Flipped background-image when it repeat-y 重复时,翻转背景图像

背景再次垂直翻转 And then flipped again to straight 然后再次翻转直线

There is a method to obtain that position? 有一种获得该位置的方法吗?

I'll accept the solution in JScript (and library of it) only if there isn't a solution with pure css. 只有在没有纯css的解决方案时,我才会接受JScript(及其库)中的解决方案。

Thank you all! 谢谢你们!

This cannot be done using standard CSS3 since there are no CSS3 properties to rotate a background image. 这不能使用标准CSS3来完成,因为没有CSS3属性来旋转背景图像。

Reference: http://www.w3.org/TR/css3-background 参考: http//www.w3.org/TR/css3-background

As suggested, combine the two motifs in a single image, much simpler and will always work. 正如所建议的那样,将两个图案组合在一个图像中,更加简单并始终有效。

Other answers suggested duplicating and merging the background image. 其他答案建议复制和合并背景图像。 That could add extra megabytes to be downloaded, especially for large images. 这可能会增加额外的兆字节数量,特别是对于大型图像。 In the case of large images, however, it might be sufficient to tile them as many times as your content requires coverage. 但是,如果是大图像,则可能需要将它们平铺多次,因为内容需要覆盖。 Note: This method uses CSS 2D Transforms, which are currently 93% supported . 注意:此方法使用CSS 2D Transforms,目前支持 93%。 In browsers that are not supported, the transforms would be ignored, which may not even matter depending on the design. 在不受支持的浏览器中,将忽略转换,这可能根据设计而不重要。

http://jsfiddle.net/skibulk/6hxeg9cu/ http://jsfiddle.net/skibulk/6hxeg9cu/

.wrap1 {
    transform: scaleY(-1);
    background: url(http://i.stack.imgur.com/I4hlZ.png) left bottom;
}

.wrap2 {
    transform: scaleY(-1);
    background:
        url(http://i.stack.imgur.com/I4hlZ.png) repeat-x left top,
        url(http://i.stack.imgur.com/I4hlZ.png) repeat-x left 500px;
}

Basically I'm using multiple backgrounds repeating horizontally, and offset vertically. 基本上我使用水平重复的多个背景,并垂直偏移。 Then I reflected the container. 然后我反映了容器。 The limitation is that the backgrounds don't repeat forever. 限制是背景不会永远重复。 So if you don't expect your content to push far down the page, this is an option. 因此,如果您不希望您的内容在页面中向下推,那么这是一个选项。

According to my comment heres a pure CSS solution. 根据我的评论,这是一个纯粹的CSS解决方案。

    yourElementWithBackground{
    background-image : url("http:yourBackgroundURL");
    background-repeat: repeat-y;
    }

Example (run in as full page if it doesn't work here or look at this Fiddle ) 示例(如果在这里不起作用或者看这个小提琴,则以完整页面运行)

 body { height: 1000px; width: 300px; background-image : url("http://picload.org/image/odgwcig/unbenannt.png"); background-repeat: repeat-y; } 
If you rely on using a single image and revert it, you could do some JavaScript magic. 如果你依赖于使用单个图像并将其还原,那么你可以做一些JavaScript魔法。 Insert elements with absolute positioning and a lower z-index per JavaScript may one with class "reversed" which contains some css transformation to revert it. 每个JavaScript插入具有绝对定位较低z-index的 元素可以使用“reverse”类 ,其中包含一些css转换以使其恢复。 The Positioning should be done in JS too - for example img1 should have top: 0, img2 top: heightOfImages* 1, img3 top: heightOfImages* 2 and so on. 定位也应该在JS中完成 - 例如img1应该有top:0,img2 top:heightOfImages * 1,img3 top:heightOfImages * 2等等。 Do this until you reached the height of your parent Element or the User's resolution. 这样做直到达到父元素的高度或用户的分辨率。

not really pretty but doable 不是很漂亮,但可行

I would load the background image like so 我会像这样加载背景图像

<style>
body{
    background-image:url('mybackground');
    background-repeat:no-repeat;
}
</style>

背景

As the image has already loaded we can do a little JavaScript without the browser doing much extra work. 由于图像已经加载,我们可以做一些JavaScript而不需要浏览器做太多额外的工作。 we can check whether the height of the image fills the entire window. 我们可以检查图像的高度是否填满整个窗口。

<script>
var el      =  $('body')[0];
var src     =  $(el).css('background-image').slice(4, -1);
var img     =  new Image();

checkHeight =  function(){    
    if(img.height < window.innerHeight){
       repeatBg();     
    }
}
img.onload  =  checkHeight();
img.src = src;
</script>

If the image background does not fill the full height of the window then this background needs repeating/flipping straight away(this will increase load time); 如果图像背景没有填满窗口的整个高度,那么这个背景需要立即重复/翻转(这会增加加载时间); otherwise event listeners can be used to trigger the check later on. 否则,事件侦听器可用于稍后触发检查。
The following function draws the image to the canvas element and creates a dataURL. 以下函数将图像绘制到canvas元素并创建dataURL。 The background-image src is updated to the new dataURL and repeat-background is changed from no-repeat to repeat-y(vertically); background-image src更新为新的dataURL,repeat-background从no-repeat变为repeat-y(vertical);
the event listeners are then remove so the function is not called again. 然后删除事件侦听器,以便不再调用该函数。

<canvas style="display:none" id="canvas"></canvas>


<script>

repeatBg = function(){  
    var canvas = $('#canvas')[0];
    var ctx = canvas.getContext("2d");
    canvas.width = img.width;
    canvas.height = img.height *2 ;
    ctx.drawImage(img,0,0);
    ctx.scale(1, -1);
    ctx.translate(0, -img.height);
    ctx.drawImage(img, 0, -img.height, img.width, img.height);
    var dataURL = canvas.toDataURL();
    $(el).css({
        'background-image' : 'url('+dataURL+')',
        'background-repeat' : 'repeat-y'
    });    
    $(window).off('load, scroll, resize', checkHeight);        
}
$(window).on('load, scroll, resize', checkHeight);



</script>

And hey presto 嘿presto

翻转

http://jsfiddle.net/dtjones1988/y49rvu73/6/ http://jsfiddle.net/dtjones1988/y49rvu73/6/

A purely css solution is not possible imo. 一个纯粹的CSS解决方案是不可能的imo。

It depends on your image. 这取决于你的形象。

If it's a static on which you use to decorate the page, It's best to just modify it by making it two times taller and adding a flipped copy to the bottom part and using that directly via standard css.. 如果它是用于装饰页面的静态,最好只需将它修改两倍,然后在底部添加翻转副本并直接通过标准css使用它。

If it's a dynamic one, you still can generate new image (original+flipped) as data url via js and use it on your element. 如果它是动态的,你仍然可以通过js生成新图像(原始+翻转)作为数据url并在元素上使用它。

Otherwise, the solution would be duplicating divs, which is always inconvenient... 否则,解决方案将是重复div,这总是不方便...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM