javascript - javascript 在页面滚动时被激活
问题描述
我从codepen获得了一些代码,其中进度条根据给定的100个数字填充。我希望它在滚动到时激活,而不是在重新加载时激活,它当前设置为。我已经在这里找不到任何东西了。Javascript对我来说是新的,所以试着掌握它,谢谢
$('.progress-wrap').each(function(){
percent = $(this);
bar = $(this).children('.progress-bar');
moveProgressBar(percent, bar);
});
// on browser resize...
$(window).resize(function() {
$('.progress-wrap').each(function(){
percent = $(this);
bar = $(this).children('.progress-bar');
moveProgressBar(percent, bar);
});
});
// SIGNATURE PROGRESS
function moveProgressBar(percent, bar) {
var getPercent = (percent.data('progress-percent') / 100);
var getProgressWrapWidth = percent.width();
var progressTotal = getPercent * getProgressWrapWidth;
var animationLength = 1000;
// on page load, animate percentage bar to data percentage length
// .stop() used to prevent animation queueing
bar.stop().animate({
left: progressTotal
}, animationLength);
}
<div class="progress-wrap progress star" data-progress-percent="70">
<div class="progress-bar progress"></div>
</div>
解决方案
看起来您正在使用 jquery - 在 vanilla javascript 中这样做会更容易,如下所示:
window.onscroll = function (e) {
// called when the window is scrolled.
}
您可能想看看:检测用户是否正在滚动浏览jquery 选项。
推荐阅读
- node.js - npm file-saver 的 saveAs() 在从 localhost 服务器传输文件时工作,但在从云机器传输文件时不起作用
- cassandra - Cassandra修复导致节点超时
- python-3.x - ValueError:形状(993,228)和(1,228)未对齐:228(dim 1)!= 1(dim 0)
- ios - iPhoneX 上的状态栏时钟已被截断
- yii2 - yii2中更新表单中的多个提交表单
- haskell - 过滤后在type1中转换Maybe type1
- c# - 我怎样才能使功能继续运行
- android - 如何提高相机的对焦速度?
- reactjs - 在移动屏幕上反应日期选择器切割日历
- javascript - 将字符串中的最后一个数字设置为负数