2016-12-14 4 views
0

要素がビューにあるときに実行するようにこのスクリプトを修正できますか?言い換えれば、実行するまで表示されるまで待つ必要があります。ありがとう! -Dan要素が表示されているときにスクリプトを実行

$('.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); 
});  

});

+0

playcode.ioするためにそれを入れてください。あなたの要素はどこに見えませんか? –

+0

こんにちはアレックス、私は働くplaycode.ioを持っています... http://true.playcode.io ... http://playcode.io/true – user2636033

+0

私の答えはあなたの問題を解決する? –

答えて

1

詳細な説明をいただきありがとうございます。 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() 
    } 
}) 
+1

アレックスありがとう。あなたは男です! – user2636033

+0

答えが正しいとマークしてください。 –

関連する問題