我试图给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>
这是我如何引用变量。我的头文件中有脚本调用,所以一切都会好的。如果您可以提出任何建议,或者如果我需要澄清更多或提供更多信息,请评论。
您可以发布的jsfiddle? – Rich
你想参考什么?变量百分比? –
你确定它是一个字符串吗? – adeneo