2015-07-10 38 views
0

我正在使用名爲'Circliful'的插件來根據百分比爲半圓圈設置動畫。動畫效果很好。我想在屏幕向下滾動到動畫點後觸發動畫。在HTML代碼我試過如下:Circliful jquery插件的基於滾動的動畫問題

<!doctype html> 

<html lang="en"> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script src="js/jquery.circliful.js"></script> 

<style> 
.one {background:black; height:300px; color:white;} 
.two {background:green; height:300px; color:white;} 

</style> 

</head> 
<body> 
<div class="one">300px Height</div> 
<div class="two">300px Height</div> 
<div class="one">300px Height</div> 

<div id="myStathalf1" data-dimension="250" data-width="30" data-fontsize="38" data-percent="95" data-fgcolor="#fab702" data-bgcolor="#eee" data-type="half" data-icon="fa-task"></div> 
<div id="myStathalf2" data-dimension="250" data-width="30" data-fontsize="38" data-percent="90" data-fgcolor="#fab702" data-bgcolor="#eee" data-type="half" data-icon="fa-task"></div> 

<script> 

$(document).ready (function(){ 
    $(window).scroll(function(){ 
     var y=$(this).scrollTop(); 
     if(y>=100) {     
      $('#myStathalf1').circliful(); 
      $('#myStathalf2').circliful(); 
     } 
    }); 


}) 
</script> 
</body> 
</html> 

插件的代碼如下所示:

"use strict"; 

(function ($) { 

    $.fn.circliful = function (options, callback) { 

     var settings = $.extend({ 
      // These are the defaults. 
      startdegree: 0, 
      fgcolor: "#000000", 
      bgcolor: "#eee", 
      fill: false, 
      width: 15, 
      dimension: 200, 
      fontsize: 15, 
      percent: 50, 
      animationstep: 0.5, 
      iconsize: '20px', 
      iconcolor: '#999', 
      border: 'default', 
      complete: null, 
      bordersize: 10 
     }, options); 

     return this.each(function() { 

      var customSettings = ["fgcolor", "bgcolor", "fill", "width", "dimension", "fontsize", "animationstep", "endPercent", "icon", "iconcolor", "iconsize", "border", "startdegree", "bordersize"]; 

      var customSettingsObj = {}; 
      var icon = ''; 
      var percent; 
      var endPercent = 0; 
      var obj = $(this); 
      var fill = false; 
      var text, info; 

      obj.addClass('circliful'); 

      checkDataAttributes(obj); 


       if (obj.data('type') != undefined) { 
        type = $(this).data('type'); 

        if (type == 'half') { 
         addCircleText(obj, 'circle-text-half', (customSettingsObj.dimension/1.45)); 
        } 
       } 
       if ($(this).data("percent") != undefined) { 
        percent = $(this).data("percent")/100; 
        endPercent = $(this).data("percent"); 
       } else { 
        percent = settings.percent/100; 
       } 

      var size = customSettingsObj.dimension, 
       canvas = $('<canvas></canvas>').attr({ 
        width: size, 
        height: size 
       }).appendTo($(this)).get(0); 

      var context = canvas.getContext('2d'); 

      var container = $(canvas).parent(); 
      var x = size/2; 
      var y = size/2; 
      var degrees = customSettingsObj.percent * 360.0; 
      var radians = degrees * (Math.PI/180); 
      var radius = size/2.5; 
      var startAngle = 2.3 * Math.PI; 
      var endAngle = 0; 
      var counterClockwise = true; 
      var curPerc = customSettingsObj.animationstep === 0.0 ? endPercent : 0.0; 
      var curStep = Math.max(customSettingsObj.animationstep, 0.0); 
      var circ = Math.PI * 2; 
      var quart = Math.PI/2; 
      var type = ''; 
      var fireCallback = true; 
      if ($(this).data('type') != undefined) { 
       type = $(this).data('type'); 

       if (type == 'half') { 
        startAngle = 2.0 * Math.PI; 
        endAngle = 3.13; 
        circ = Math.PI; 
        quart = Math.PI/0.996; 
       } 
      } 


      /** 
      * adds text to circle 
      * 
      * @param obj 
      * @param cssClass 
      * @param lineHeight 
      */ 
      function addCircleText(obj, cssClass, lineHeight) { 
       $("<span></span>") 
        .appendTo(obj) 
        .addClass(cssClass) 
        .html(text) 
        .prepend(icon) 
        .css({ 
         'line-height': lineHeight + 'px', 
         'font-size': customSettingsObj.fontsize + 'px' 
        }); 
      } 


      /** 
      * checks which data attributes are defined 
      * @param obj 
      */ 
      function checkDataAttributes(obj) { 
       $.each(customSettings, function (index, attribute) { 
        if (obj.data(attribute) != undefined) { 
         customSettingsObj[attribute] = obj.data(attribute); 
        } else { 
         customSettingsObj[attribute] = $(settings).attr(attribute); 
        } 

        if (attribute == 'fill' && obj.data('fill') != undefined) { 
         fill = true; 
        } 
       }); 
      } 

      /** 
      * animate foreground circle 
      * @param current 
      */ 
      function animate(current) { 

       context.clearRect(0, 0, canvas.width, canvas.height); 

       context.beginPath(); 
       context.arc(x, y, radius, endAngle, startAngle, false); 

       context.lineWidth = customSettingsObj.width + 1; 

       context.strokeStyle = customSettingsObj.bgcolor; 
       context.stroke(); 

       context.beginPath(); 
       context.arc(x, y, radius, -(quart), ((circ) * current) - quart, false); 

       context.strokeStyle = customSettingsObj.fgcolor; 
       context.stroke();    

       if (curPerc < endPercent) { 
        curPerc += curStep; 
        requestAnimationFrame(function() { 

         animate(Math.min(curPerc, endPercent)/100); 
        }, obj); 
       } 
      } 

      animate(curPerc/100); 

     }); 
    }; 
}(jQuery)); 

這使得無限類型的循環,其不斷循環,直到我滾動的,我理解的原因那。 我已經嘗試過更改插件本身的代碼,但似乎沒有任何工作。

我希望半圓的灰色背景顏色默認出現,前景色(橙色)動畫在我滾動一些像素併到達點後開始。 有人可以幫我嗎?

+0

我很抱歉,但我沒有正確理解它。它是否繼續創造新的'circliful'對象,並且是你不想發生的事情? –

+0

那就對了。我想要的是當我通過滾動到達** circliful **對象時動畫應該開始。 –

+0

我明白了。像** [this](http://jsfiddle.net/tahirahmed/hk3bko93/)**。它在某種程度上有幫助嗎? –

回答

1

好的。

所以我覺得你可以做的是添加一個簡單的布爾標誌,由hasCreatedObjects的名字說,最初設置爲false但只要時機成熟,將其設置true並使用此布爾值添加任何進一步的circliful()對象。

的jsfiddle其中可以發現here和JavaScript,具體如下:

$(document).ready(function() { 
    var hasCreatedObjects = false; 
    $(window).scroll(function() { 
     var y = $(this).scrollTop(); 
     if (y >= 100) { 
      if (!hasCreatedObjects) { 
       hasCreatedObjects = true; 
       $('#myStathalf1').circliful(); 
       $('#myStathalf2').circliful(); 
      } 
     } 
    }); 
});