简体   繁体   English

滚动时使SVG对象保持静态

[英]Keep an SVG Object static while scrolling

In my web application, I am generating a SVG file which contains a "header" that I would like to keep visible at the top of the window as the user scrolls down the image. 在我的Web应用程序中,我正在生成一个SVG文件,其中包含一个“标头”,当用户向下滚动图像时,该标头希望在窗口顶部保持可见。

I can think of two ways of accomplishing this, but am looking for any other bright ideas. 我可以想到两种方法来实现此目的,但是我正在寻找其他任何出色的想法。 My two thoughts are: 我的两个想法是:

  • Generate two separate SVGs, one being the "header" and one being the content and then simply displaying them in two different HTML <div> elements, allowing the lower one to scroll. 生成两个单独的SVG,一个为“标题”,一个为内容,然后仅将它们显示在两个不同的HTML <div>元素中,从而使下一个SVG滚动。 I don't like this idea because I would have to generate two separate documents. 我不喜欢这个主意,因为我必须生成两个单独的文档。

  • Utilise ECMAScript in the SVG itself and find a way to float the <g> that contains the header at the top of the page. 在SVG本身中利用ECMAScript,找到一种方法来浮动<g> ,该<g>包含页面顶部的标题。 I like this, because whenever the SVG is viewed it should work, but I am not sure how to accomplish this and would appreciate any pointers or examples. 我喜欢这样,因为每当查看SVG时它都应该工作,但是我不确定如何完成此工作,并且会感谢任何指示或示例。

I've got an example of the first case here : 我在这里有第一种情况的示例:

footer {
    background: url(images/grasspat.svgz);
    height: 64px; width: 100%;
    position: fixed; left: 0; right: 0; top: auto; bottom:0;
}

The other case, which will of course require the client to have script enabled, and does flicker a bit (because of how it's done: scroll event > dom operation > repaint), a full example can be seen here . 另一种情况当然需要客户端启用脚本,并且确实有些闪烁(由于其完成方式:scroll event> dom operation> repaint),可以在此处看到完整的示例。

Can't you just use CSS? 您不能只使用CSS吗?

#header_id {
    position: fixed;
    top : 0;
 }

Edit 编辑

Sorry, I was thrown by the term object. 抱歉,我被术语“对象”所吸引。 I thought the svg WAS the header for another document. 我认为svg是另一个文档的标题。 But I checked , and the W3C docs say you can apply CSS2 positioning to anything but the outer-most element. 但是我检查了一下 ,W3C文档说您可以将CSS2定位应用于除最外层元素之外的任何内容。 Let me know if it's as simple as my idea. 让我知道这是否和我的想法一样简单。 I'd love to know. 我很想知道。

This may or may not be outside the scope of your requirements, just thinking out loud. 只是想一想,这可能超出或可能不在您的要求范围内。 You could put the page content in a viewBox with an svg scrollbar on it. 您可以将页面内容放在带有svg滚动条的viewBox中。 ie: 即:

<svg>
  <g id='header'>Header</g>
  <svg viewBox='0 0 800 600'>Page Content</svg>
  <g id='scrollbar'></g>
</svg>

This would mean a lot of EMCAScript work on the scroll bar. 这将意味着在滚动条上进行大量的EMCAScript工作。 But most of that should be available at carto.net: 但是大多数应该可以在carto.net上找到:

http://www.carto.net/papers/svg/samples/ http://www.carto.net/papers/svg/samples/

The actual viewport for the viewBox can be kept in proportion using the aspect tag too. 也可以使用Aspect标签将viewBox的实际视口保持成比例。

I have tackled a similar problem with header and footer to stay fixed and found best performance and flexibility using an html page with three svg docs in fixed position divs. 我已经解决了一个相似的问题,即页眉和页脚保持固定,并使用在固定位置div中包含三个svg文档的html页面发现了最佳性能和灵活性。 Works perfect in all browsers, resizes really smooth, and scales correctly at all window sizes. 在所有浏览器中均可完美运行,可真正调整大小,并在所有窗口尺寸下均可正确缩放。

You can create and position multiple <svg> elements, which you can set (at least in script from my own experience) to be position:fixed and this will work. 您可以创建和定位多个<svg>元素,可以将其设置(至少根据我的经验在脚本中)为position:fixed ,这将起作用。 Ensure your "floating" svg area is on top in the z-order. 确保“浮动” svg区域在z顺序中位于顶部。

One caveat: Firefox is different regarding "click-through" on floating svg layers like this than the other browsers. 一个警告:Firefox在这样的浮动svg层上的“点击”功能与其他浏览器有所不同。 Firefox 3.6 respects the pointer-events property better than prior versions of Firefox. 与以前的Firefox版本相比,Firefox 3.6更尊重pointer-events属性。 The others default to allowing click-through. 其他默认为允许点击。

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

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