簡體   English   中英

SVG線使用jQuery滾動顯示

[英]SVG lines draw on scroll with jQuery

我如何使用css為SVG“ line”元素創建繪制動畫。 我想在滾動條上畫出線條,並具有平滑效果。 有人有什么想法嗎? 我嘗試搜索此內容,但無法通過線條元素找到此效果。 這是我的html和svg:

<div class="box">
<svg width="100%" height="100%">
  <line x1="0" y1="0" x2="0" y2="100%" stroke="gray" stroke-width="2"  />
  <line x1="0" y1="100%" x2="100%" y2="100%" stroke="gray" stroke-width="2"  />
</svg>
</div>
<div class="box2">
<svg width="100%" height="100%">
  <line x1="100%" y1="100%" x2="100%" y2="0" stroke="gray" stroke-width="2"  />
  <line x1="0" y1="100%" x2="100%" y2="100%" stroke="gray" stroke-width="2"  />
</svg>
</div>
<div class="box">
<svg width="100%" height="100%">
  <line x1="0" y1="0" x2="0" y2="100%" stroke="gray" stroke-width="2"  />
  <line x1="0" y1="100%" x2="100%" y2="100%" stroke="gray" stroke-width="2"  />
</svg>
</div>
<div class="box2">
<svg width="100%" height="100%">
  <line x1="100%" y1="100%" x2="100%" y2="0" stroke="gray" stroke-width="2"  />
  <line x1="0" y1="100%" x2="100%" y2="100%" stroke="gray" stroke-width="2"  />
</svg>
</div>

和CSS:

.box{
    width: 100%;
    height: 300px;
    position: relative;
}
.box2{
    width: 100%;
    height: 300px;
    position: relative;
}

演示版

我只知道您可以使用自定義javascript遍歷路徑 ,也許也可以將其用於一行:)

滾動功能的新編輯

 // Get a reference to the <path> var path = document.querySelector('#star-path'); // Get length of path... ~577px in this case var pathLength = path.getTotalLength(); // Make very long dashes (the length of the path itself) path.style.strokeDasharray = pathLength + ' ' + pathLength; // Offset the dashes so the it appears hidden entirely path.style.strokeDashoffset = pathLength; // Jake Archibald says so // https://jakearchibald.com/2013/animated-line-drawing-svg/ path.getBoundingClientRect(); // When the page scrolls... window.addEventListener("scroll", function(e) { // What % down is it? // https://stackoverflow.com/questions/2387136/cross-browser-method-to-determine-vertical-scroll-percentage-in-javascript/2387222#2387222 // Had to try three or four differnet methods here. Kind of a cross-browser nightmare. var scrollPercentage = (document.documentElement.scrollTop + document.body.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight); // Length to offset the dashes var drawLength = pathLength * scrollPercentage; // Draw in reverse path.style.strokeDashoffset = pathLength - drawLength; // When complete, remove the dash array, otherwise shape isn't quite sharp // Accounts for fuzzy math if (scrollPercentage >= 0.99) { path.style.strokeDasharray = "none"; } else { path.style.strokeDasharray = pathLength + ' ' + pathLength; } }); 
 body { /* feel free to change height */ height: 5000px; } #star-svg { position: fixed; top: 50%; left: 50%; width: 150px; height: 150px; margin: -75px 0 0 -75px; } 
 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100.6 107.6" id="star-svg"> <path fill="none" stroke="black" stroke-width="2" id="star-path" d="M43.7,65.8L19.9,83.3c-2.9,1.9-5.1,3.2-7.9,3.2c-5.7,0-10.5-5.1-10.5-10.8 c0-4.8,3.8-8.2,7.3-9.8l27.9-12L8.8,41.4c-3.8-1.6-7.3-5.1-7.3-9.8c0-5.7,5.1-10.5,10.8-10.5c2.9,0,4.8,1,7.6,3.2l23.8,17.4 l-3.2-28.2c-1-6.7,3.5-12,9.8-12c6.3,0,10.8,5.1,9.8,11.7L57,41.8l23.8-17.4c2.9-2.2,5.1-3.2,7.9-3.2c5.7,0,10.5,4.8,10.5,10.5 c0,5.1-3.5,8.2-7.3,9.8L63.9,53.8l27.9,12c3.8,1.6,7.3,5.1,7.3,10.1c0,5.7-5.1,10.5-10.8,10.5c-2.5,0-4.8-1.3-7.6-3.2L57,65.8 l3.2,28.2c1,6.7-3.5,12-9.8,12c-6.3,0-10.8-5.1-9.8-11.7L43.7,65.8z"/> </svg> 

資料來源: https : //css-tricks.com/scroll-drawing/

您需要在路徑中導出svg。 然后您可以根據視口添加滾動動畫。 這是運行您的實例的測試服務器。 示范項目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM