2016-03-15 123 views

回答

1

也許你應該使用getTotalLength()來獲取當前路徑長度。 然後,這個腳本就是一個例子,直線在滾動中間增長,然後消失,就像在你的圖紙中一樣。

這裏是一個演示:https://jsfiddle.net/Ltbvyepj/

這裏是Javascript代碼:

var path = document.querySelector(".path"); 
// Get the actual length of your path. 
var len = path.getTotalLength(); 
// Dashes have the exact length of the path. 
path.style.strokeDasharray = len + " " + len; 
// Shift of the length of the path, so the line is quite not visible. 
path.style.strokeDashoffset = len; 
// Attach to the window's scroll event. 
window.addEventListener('scroll', function() { 
    // Getting the page dimensions. 
    var rect = document.querySelector('html').getBoundingClientRect(); 
    // Height is the size of the page which is out of screen. 
    var height = rect.height - window.innerHeight; 
    // Percent of scroll bar. 0 Means the top, 1 the bottom. 
    var percent = height < 0 ? 1 
      : -rect.top/height; 
    // If you omit the `2 *` you will get a growing only path. 
    path.style.strokeDashoffset = len * (1 - 2 * percent); 
}, false);