2016-11-15 80 views
-1

我想通過滾動上的svg路徑移動對象=)我試圖在滾動路徑中添加部分路徑,但它仍然不起作用。幫助!=) https://jsfiddle.net/YuriiBielozertsev/Ltx9ed0L/通過svg路徑在滾動上移動圖像

<?xml version="1.0"?> 
<svg viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink"> 
    <!-- Draw the outline of the motion path in grey, along with 2 small circles at key points --> 
    <path d="M10,110 A120,120 -45 0,1 110 10 A120,120 -45 0,1 10,110" stroke="green" stroke-width="2" fill="none" id="theMotionPath"/> 
     <circle cx="10" cy="110" r="3" fill="#000"/> 
     <circle cx="110" cy="10" r="3" fill="#000"/> 

     <!-- Red circle which will be moved along the motion path. --> 
     <circle cx="0" cy="" r="5" fill="red"> 

     <!-- Define the motion path animation --> 
     <animateMotion dur="6s" repeatCount="indefinite"> 
      <mpath xlink:href="#theMotionPath"/> 
     </animateMotion> 
    </circle> 
</svg> 
+1

嗨。可悲的是你的問題太模糊了,這就是你爲什麼會得到或者會得到低估的原因。最快的答案是去http://jsfiddle.net並設置一個問題的工作演示。然後我們可以快速檢查它,而不必浪費時間猜測。將鏈接發佈到你的答案中,你會得到更好的迴應。 –

回答

2

像這樣的事情?

如何工作的:

  1. 當我們拿到一個滾動事件我們:
  2. 計算多遠下跌的一頁,我們是
  3. 轉換使用<path>元函數這一比例在路徑上的位置getTotalLength()getPointAtLength()
  4. 重新定位點,使其出現在這一點上。

function positionTheDot() { 
 

 
    // What percentage down the page are we? 
 
    var scrollPercentage = (document.documentElement.scrollTop + document.body.scrollTop)/(document.documentElement.scrollHeight - document.documentElement.clientHeight); 
 

 
    // Get path length 
 
    var path = document.getElementById("theMotionPath"); 
 
    var pathLen = path.getTotalLength(); 
 
    
 
    // Get the position of a point at <scrollPercentage> along the path. 
 
    var pt = path.getPointAtLength(scrollPercentage * pathLen); 
 
    
 
    // Position the red dot at this point 
 
    var dot = document.getElementById("dot"); 
 
    dot.setAttribute("transform", "translate("+ pt.x + "," + pt.y + ")"); 
 
    
 
}; 
 

 
// Update dot position when we get a scroll event. 
 
window.addEventListener("scroll", positionTheDot); 
 

 
// Set the initial position of the dot. 
 
positionTheDot();
.verylong { 
 
    height: 2000px; 
 
} 
 

 
svg { 
 
    position: fixed; 
 
    width: 200px; 
 
    height: 200px; 
 
}
<svg viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink"> 
 
    <!-- Draw the outline of the motion path in grey, along with 2 small circles at key points --> 
 
    <path d="M10,110 A120,120 -45 0,1 110 10 A120,120 -45 0,1 10,110" stroke="green" stroke-width="2" fill="none" id="theMotionPath"/> 
 
    <circle cx="10" cy="110" r="3" fill="#000"/> 
 
    <circle cx="110" cy="10" r="3" fill="#000"/> 
 

 
    <!-- Red circle which will be moved along the motion path. --> 
 
    <circle cx="0" cy="0" r="5" fill="red" id="dot"/> 
 
</svg> 
 

 
<div class="verylong"> 
 
</div>

+0

thnks =)是否可以旋轉對象?如果它不會被舍入(像飛)'代碼''code'使這樣的行爲不起作用 –

+0

您可以使用'getPointAtLength()'來獲取這一點稍微落後於當前的觀點。然後使用這兩個座標點來計算畫出「飛行」的角度。 –