滚动时沿SVG蛇形路径移动箭头并切换图标的CSS动画求助
滚动时沿SVG蛇形路径移动箭头并切换图标的实现问题
需要实现以下效果:
- 页面滚动时,箭头沿SVG蛇形路径同步移动
- 当页面滚动进入新章节时,自动切换箭头图标
目前参考了相关滚动路径动画示例,但无法结合自身页面设计复现预期效果,现有代码如下:
HTML代码
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <svg width="246" height="2990" viewBox="0 0 246 2990" fill="none" xmlns="http://www.w3.org/2000/svg"> <defs> <path id="Path_440" d="M210.001 1.5C210.001 1.5 41.0015 324.5 6.50082 617.5C-27.004 902.042 182.501 1032.5 240.001 1313C275.095 1484.2 29.8527 1661 41.0008 1914.5C50.4751 2129.94 230.431 2237.5 235.001 2431.5C240.42 2661.59 41.0008 2988 41.0008 2988" stroke="#F39029" stroke-width="4" stroke-dasharray="20 10"/> </defs> <use xlink:href="#Path_440" stroke="#000" stroke-width="4" stroke-dasharray="1"/> <use id="theFill" xlink:href="#Path_440" stroke="#000" stroke-width="1"/> </svg>
JavaScript代码
window.addEventListener('scroll', function() { let l = Path_440.getTotalLength(); let dasharray = l; let dashoffset = l; e = document.documentElement; theFill.setAttributeNS(null, "stroke-dasharray", l); theFill.setAttributeNS(null, "stroke-dashoffset", l); dashoffset = l - window.scrollY * l / (e.scrollHeight - e.clientHeight); //console.log('window.scrollY', window.scrollY, 'scrollTop', e.scrollTop, 'scrollHeight', e.scrollHeight, 'clientHeight', e.clientHeight, 'dash-offset', dashoffset); theFill.setAttributeNS(null, "stroke-dashoffset", dashoffset); })
内容的提问来源于stack exchange,提问作者Kunal Vijan
相关产品推荐
相关产品推荐

