gpt4 book ai didi

javascript - 当元素处于 View 中时运行脚本

转载 作者:行者123 更新时间:2023-11-28 18:13:30 24 4
gpt4 key购买 nike

有人可以修复此脚本,以便它在元素位于 View 中时运行。换句话说,它需要等到可见才能运行。谢谢! -丹

$('.amount-chart').each(function(){
var $chart = $(this),
size = parseFloat( $chart.outerWidth() ),
clearSet;

$chart.css({
height: size
})
.easyPieChart({
size: size,
animate: 2000,
onStep: function(from, to, currentValue) {
$(this.el).find('span.amount-counter').text(Math.round(currentValue));
}
});

$(window).on('resize', function(){

size = parseFloat( $chart.outerWidth() );

$chart.css({
height: size
});

//clearTimeout(clearSet);
//clearSet = setTimeout(function(){
$chart.removeData('easyPieChart').find('canvas').remove();
$chart.easyPieChart({
size: size,
animate: 1
});
//}, 100);
});

});

最佳答案

感谢您的完整描述!看demo

HTML

<div id="el">Pie Chart</div>    

JavaScript

var inited = false
function init() {
// init your chart
// code here run once on element with id="el" will in viewport
}

$(window).on('scroll', function() {
if ( inited ) {
return
}

if ( el.offsetTop >= window.innerHeight + document.body.scrollTop ) {
inited = true
init()
}
})

关于javascript - 当元素处于 View 中时运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41149758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com