javascript - 当它位于视口(viewport)中时,将图表饼加载到 div 中

标签 javascript jquery

我正在使用easypiechart plugin我希望它仅在用户向下滚动到 .skills div 时加载它。我正在使用viewport plugin .

我尝试过这样的:

<script src="js/viewport.js"></script>
<script>
    (function(){
    if ($(".skills:in-viewport")){
    jQuery.getScript("js/jquery.easypiechart.min.js").done(function() {
        $('.chart').easyPieChart({
            easing: 'easeOutBounce',
            onStep: function(from, to, percent) {
                $(this.el).find('.percent').text(Math.round(percent));
            }
        });
    });
    };
    });
    </script>

在视点中时不会显示。

谢谢!

最佳答案

无需视口(viewport)插件即可完成此操作。您可以使用 $(window).height() ,当用户向下滚动时,您检查您想要图表的 div 的距离是否小于滚动距离+ $(window).height(),如果是这样,你就画它。

这是代码:

var el = document.getElementById('your-div'),
    topDistance = el.getBoundingClientRect().top,
    drawn = false;

function drawPieChart() {
    jQuery.getScript("js/jquery.easypiechart.min.js").done(function() {
        $('.chart').easyPieChart({
            easing: 'easeOutBounce',
            onStep: function(from, to, percent) {
                $(this.el).find('.percent').text(Math.round(percent));
            }
        });
    });
}

function handleScroll() {
    if (document.body.scrollTop + $(window).height() > topDistance) {
        drawn || drawPieChart();
        drawn = true;
    }
}

关于javascript - 当它位于视口(viewport)中时,将图表饼加载到 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25125538/

相关文章:

javascript - 如何在jquery中的onclick中传递参数

javascript - 为什么我的对象中的数据没有设置到我的 React 类组件中的 setState

javascript - 滚动时调整 Logo 大小

javascript - chrome 是否限制请求帧时间

javascript - 带有 addEventListener 的移相器

jquery - div 以 jQuery 为中心

jquery循环,页面多个

javascript - 如何在nightwatchjs中执行for循环

jquery - 更改 jQuery 验证插件的错误消息样式

javascript - list.js 可排序表出错