How to scale SVG path to fit the window size?

前端 未结 2 1075
半阙折子戏
半阙折子戏 2021-01-25 07:25

I am having troubles to scale the SVG to fit the window size.

In this example, I have a wavy path and a text element, what I want to achieve here is to move the text el

2条回答
  •  深忆病人
    2021-01-25 08:26

    If you want your SVG to scale to fit the screen (or any parent container), it needs to have a viewBox attribute. This attribute tells the browser the dimensions of the SVG content. Without it, the browser has know way of knowing how much it needs to be scaled.

    Your path is about 3780 width, and the bottom of it is at y=144. So a reasonable value of viewBox would be:

    viewBox="0 0 3780 150"
    

    document.getElementById("MyPath").setAttribute("d", document.getElementById("Path").getAttribute("d"));
    var tl = new TimelineMax({
      repeat: 0,
      delay: 1
    });
    tl.to("#Text", 3, {
      attr: {
        startOffset: '50%',
        opacity: 1
      }
    });
    
    
    window.addEventListener('scroll', function() {
      tl.to("#Text", 3, {
        attr: {
          startOffset: '100%',
          opacity: 0
        }
      });
    }, true);
    @import url(https://fonts.googleapis.com/css?family=Oswald);
    body {
      background-color: #222;
    }
    
    svg {
      overflow: visible;
      width: 100%;
      height: 100%;
      background-color: #fff;
      left: 0;
      top: 0;
      position: absolute;
    }
    
    
    
    
    
    
      Love the little things.
      
    

提交回复
热议问题