Javascript开始和结束在一定%的滚动页面
Javascript start and end on certain % of scroll page
我正在寻求帮助。
我有这个codependency应用到我的页面- http://codepen.io/chriscoyier/pen/YXgWam,但我想开始绘图从某些%(让说60%的页面)和它/有100%的svg文件绘制)在另一个%的滚动(让说80%的页面)
// Get a reference to the <path>
var path = document.querySelector('#path');
var path2 = document.querySelector('#path2');
var path3 = document.querySelector('#path3');
// Get length of path...
var pathLength = path.getTotalLength();
var path2Length = path2.getTotalLength();
var path3Length = path3.getTotalLength();
// Will make very long dashes (the length of the paths itself)
path.style.strokeDasharray = pathLength + ' ' + pathLength;
path2.style.strokeDasharray = path2Length + ' ' + path2Length;
path3.style.strokeDasharray = path3Length + ' ' + path3Length;
// Set offset the dashes so the it appears hidden entirely
path.style.strokeDashoffset = pathLength;;
path2.style.strokeDashoffset = path2Length;
path3.style.strokeDashoffset = path3Length;
// Because Jake Archibald says so
// https://jakearchibald.com/2013/animated-line-drawing-svg/
path.getBoundingClientRect();
path2.getBoundingClientRect();
path3.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
var scrollPercentage = (document.documentElement.scrollTop
document.body.scrollTop) / (document.documentElement.scrollHeight
document.documentElement.clientHeight - 0.5);
}
// Length to offset the dashes
if {
var drawLength = pathLength * scrollPercentage;
var drawLength2 = path2Length * scrollPercentage;
var drawLength3 = path3Length * scrollPercentage;
}
// Draw logo in reverse
path.style.strokeDashoffset = pathLength - drawLength;
path2.style.strokeDashoffset = path2Length - drawLength2;
path3.style.strokeDashoffset = path3Length - drawLength3;
// When complete, remove the dash array, otherwise shapes aren't quite sharp
// Accounts for fuzzy math
if (scrollPercentage >= 0.99) {
path.style.strokeDasharray = "none";
} else {
path.style.strokeDasharray = pathLength + ' ' + pathLength;
}
if (scrollPercentage >= 0.99) {
path2.style.strokeDasharray = "none";
} else {
path2.style.strokeDasharray = path2Length + ' ' + path2Length;
}
if (scrollPercentage >= 0.99) {
path3.style.strokeDasharray = "none";
} else {
path3.style.strokeDasharray = path3Length + ' ' + path3Length;
}
// Add fill to the paths
if (scrollPercentage > 0.99) {
path.setAttribute("fill", "#672A7A");
} else {
path.setAttribute("fill", "none");
}
if (scrollPercentage > 0.99) {
path2.setAttribute("fill", "#672A7A");
} else {
path2.setAttribute("fill", "none");
}
} else {
path.style.strokeDasharray = pathLength + ' ' + pathLength;
}
if (scrollPercentage >= 0.99) {
path2.style.strokeDasharray = "none";
} else {
path2.style.strokeDasharray = path2Length + ' ' + path2Length;
}
if (scrollPercentage >= 0.99) {
path3.style.strokeDasharray = "none";
} else {
path3.style.strokeDasharray = path3Length + ' ' + path3Length;
}
谢谢你,马丁
这只是一个适当设置drawLength的例子。
- 如果它低于0.6(60%),你希望它为0,
- 如果它大于0.8你希望它是pathLength
- 之间需要在0和pathLength之间线性变化。
像这样…
var drawLength = 0;
if (scrollPercentage >= 0.8) {
drawLength = pathLength;
} else if (scrollPercentage >= 0.6) {
drawLength = pathLength * (scrollPercentage - 0.6) * 5;
}
注意5 = 1/(0.8 - 0.6)
相关文章:
- 水平视差滚动从头开始-没有插件(jQuery)
- 如何在从顶部滚动时暂停转盘/滑块,并在滚动到顶部时重新开始
- 滚动框滚动工作,但它在开始时返回
- 在某个滚动点开始页面
- jQuery 完美滚动 - 容器中间的开始滚动条
- 滚动浏览列表元素不会在第一次点击时开始
- Div 在页眉到达其顶部时开始滚动,当其底部到达页脚时停止滚动
- Javascript滚动菜单,仅在页面向下的特定方式后开始
- 从底部开始,然后向上滚动
- 检测触摸开始/触摸结束是否取消了滚动(动量滚动)
- 视差滚动效果,在页面上以一定的百分比开始
- 定位链接滚动条从中间开始
- 使水平滚动条从右端开始
- 平滑滚动以从中心开始
- 如何使html滚动条从底部开始
- 自动开始延迟加载图像,而无需等待用户向下滚动页面
- 滚动开始时的图像跳跃
- 当滚动开始时暂停html5视频
- 如何在滚动开始时运行函数
- 使滚动开始的框滚动到底