简体   繁体   English

如何使用SVG作为响应重复的网页背景?

[英]How to use an SVG as a responsive repeating webpage background?

I want to know how can use a dynamic SVG image as a webpage background with background-repeat CSS property. 我想知道如何使用动态SVG图像作为具有background-repeat CSS属性的网页背景。

Here is a codepen for the SVG itself: http://codepen.io/megakoresh/pen/pyLkb 这是SVG本身的一个codepen: http ://codepen.io/megakoresh/pen/pyLkb

As you can see I have a hex that spawns another hex within it on hover with CSS transition. 正如你所看到的,我有一个十六进制,在它转换时悬停时会在其中生成另一个十六进制。 What I initially throught is that if I use the CSS background-image:url('SVG.svg') , that it would tile them up nicely and voila: 我最初通过的是,如果我使用CSS background-image:url('SVG.svg') ,它会很好地平铺它们并且瞧瞧:

body{
margin: 0;
padding: 0;
background: url('SVG_optimized.svg');
}

But apparently that breaks transition effect. 但显然这打破了过渡效应。 I can technically tile this with PHP (or by hand, just by copypasting SVG objects), but that would make it very heavy on the client's hardware. 我可以在技术上用PHP(或者手工,只是通过复制SVG对象)来平铺它,但这会使它在客户端的硬件上非常沉重。 Is there some way to tile this thing across the page without making PHP spam the page with 1000 and 1 SVG objects? 有没有办法在整个页面上平铺这个东西而不使PHP垃圾邮件成为1000和1个SVG对象的页面?

Edit: If I can't use the background tiling, maybe it's possible to add the hover animation directly into SVG itself? 编辑:如果我不能使用背景平铺,也许可以将悬停动画直接添加到SVG本身? I mean technically it already is: the style of the SVG is imbedded into the SVG file inside <style> tag, but maybe there's some other way to use it such that it works with CSS backgrounds? 我的意思是技术上它已经是:SVG的样式嵌入到<style>标签内的SVG文件中,但也许还有其他一些方法可以使用它,以便它可以与CSS背景一起使用?

Like here , the animation works on this page, despite using the CSS background method, except it's inside the SVG itself somehow. 就像这里一样,动画在这个页面上工作,尽管使用了CSS背景方法,除了它在SVG本身内部。 Maybe it's possible to have a mouseover event in SVG too? 也许在SVG中也可能有鼠标悬停事件? Inkscape has that property, but it didn't seem to work when I tried it (I put the CSS transition in there). Inkscape有这个属性,但是当我尝试它时似乎没有用(我把CSS转换放在那里)。

http://codepen.io/anon/pen/GEkAy http://codepen.io/anon/pen/GEkAy

here's a demonstration using javascript to tile the SVG in an absolutely positioned div behind your content (without having to repeat many instances of the svg in your html source.) I chose to use pointer-events: none; 这里是一个演示,使用javascript将SVG平铺在内容后面的绝对定位的div中(无需在html源代码中重复svg的许多实例。)我选择使用pointer-events: none; on the content div to pass the events through (so the tiles highlight behind the page content). 在内容div上传递事件(因此瓷砖在页面内容后面突出显示)。 This means that if you want users to be able to select text or click stuff you'll need to either remove that line or add pointer-events: auto; 这意味着如果您希望用户能够选择文本或点击内容,则需要删除该行或添加pointer-events: auto; to the specific tags you want mouse-interactable. 您想要鼠标可交互的特定标签。 Note that the entire width of the block h1 element does not pass the mouse through, so the tiles don't highlight if you mouse over the h1 . 请注意,块h1元素的整个宽度不会通过鼠标,因此如果将鼠标悬停在h1 ,则切片不会突出显示。 But it does go through the p element, and you can't drag to select the p element. 但它确实通过p元素,你不能拖动来选择p元素。 (At least in Chrome!) (至少在Chrome中!)

this also might not work on old IE. 这也可能不适用于旧IE。 dunno! 不知道!

full code linked at top. 
(snip)

var $svg = $('.hex').clone();
for(var i = 0; i<n_x*n_y; i++){
  $svg.clone(true).appendTo('#background');
}
$('.CLinner-path').each(function(){
  var self = this;
  $(this).on('mouseover', function(){
    self.classList.add('shiny')
    console.log('mouseover!', self);
  });

  $(this).on('mouseout', function(){
    self.classList.remove('shiny')
  });
});

(snip)

.CLinner-path{
  fill: #fa8928;
  fill-opacity: 1;
  stroke-width: 6;
  marker-start: none;
  marker-mid:   none;
  opacity: 0;
  transition: opacity 0.5s ease;
}

.CLinner-path.shiny{
  opacity:1;
}

You can create this (almost) fully in svg. 您可以在svg中完全创建(几乎)。 See: http://codepen.io/lemnis/pen/tCnGd 请参阅: http//codepen.io/lemnis/pen/tCnGd

There are a few things you should keep mind: 你应该记住一些事情:

  • Instead of background-repeat, you have to use a svg pattern 而不是背景重复,你必须使用svg模式
  • You can't use one pattern, because you want to animate a pattern 您不能使用一种模式,因为您想要为模式设置动画
  • SVG animation doesn't support a hover event, instead i'm using the mouseover and mouseout events to the trigger the animation. SVG动画不支持悬停事件,而是使用mouseover和mouseout事件来触发动画。
  • You can't use it as background-image because you will lose the interactivity 您不能将其用作背景图像,因为您将失去交互性

For animating every element (edit) 用于动画化每个元素(编辑)

Unfortunately you need some javascript for animating every object. 不幸的是,你需要一些javascript来动画每个对象。 You can see here a rough example: http://codepen.io/lemnis/pen/bxpEh . 你可以在这里看到一个粗略的例子: http//codepen.io/lemnis/pen/bxpEh I didn't try to embed the svg with embed, object or iframe. 我没有尝试使用embed,object或iframe嵌入svg。 But I assume it will work fine. 但我认为它会正常工作。 Also I switched back to css hovers and transitions for a better effect. 此外,我切换回css悬停和过渡以获得更好的效果。

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

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