2013-08-19 55 views
2

我试图给div元素设置动画,以便它改变宽度。使用jquery引用javascript变量时遇到问题

问题是我希望它将宽度更改为存储在.js文件中的变量,我不确定如何使其识别它。

我的HTML代码:

<section style="width:100%; height:120px; clear:both;" > 
    <section class="campaign_statistics" style="background-color:#EFEFEF;"> 

     <?php include('progress_chart.php'); ?> 

    </section> 
</section> 

用途包括为PHP文件,其中包含:

if ($blog_id == 9) 
    echo 
    ' 
    <script> 
    var percent = String(businessProgress.getPercent()); 
    document.write(businessProgress.toString()); 
    </script> 
    ' 
; 

我定义的变量%到jQuery的引用。 .js文件有一个对象,我用它存储了一些变量,并且让getters在需要时获取它们的信息。

toString()方法:

this.toString = function(){ 
    var string = '<div class="campaign_progress" style="width:0%;"> <div class="campaign_percentage_bar">' + String(this.getPercent()) + '% Percent Unit Progress</div> <div class="campaign_dollars">$' + mySlice(this.getCurrent()) + '<span class="goal"> of $' + mySlice(this.getGoal()) + '</span></div></div>'; 
return string; 
} 

基本上建立,我想动画div元件。

宽度设置为0%当我的jQuery的是所谓的标题:

<script> 
    $(document).ready(function() { 
     $(".campaign_percentage_bar").animate({width:String(percent)+'%')}, 5000); 
    }); 
</script> 

这是我如何引用变量。我的头文件中有脚本调用,所以一切都会好的。如果您可以提出任何建议,或者如果我需要澄清更多或提供更多信息,请评论。

+0

您可以发布的jsfiddle? – Rich

+0

你想参考什么?变量百分比? –

+0

你确定它是一个字符串吗? – adeneo

回答

1

试试这个

$(".campaign_percentage_bar").animate({width:percent+'%'}, 5000); 
+0

这正是我所期待的。你对这个额外的括号是正确的。我也将课程改为campaign_progress,我之前使用的是不正确的。谢谢 – WhyAyala