听说按照标准,变量都需要在函数的顶部声明。
但是如果元素是在函数之间创建的,而另一个元素取决于创建元素的宽度或高度(动态),我们如何在顶部声明变量?
这是我的功能:
var stepSlider = function(holder,column){
$.each(holder, function(i,value) {
var container = $(value),
colCount = column,
boardViewer = container.find('.stepRangeCompound'),
boardHolder = container.find('.boardHolder'),
board = container.find('.indBoard'),
boardCount = board.size(),
boardWidth = board.outerWidth(true),
boardHeight = board.outerHeight(true),
initial=0;
//setting sizes
while(initial < boardCount){
if(initial % colCount === 0){
$('<div />',{
class:'boardColumn',
css:{
float:'left',
height:boardHeight*colCount
}
})
.appendTo(boardHolder) //after append only i am getting the width and element at all.
}
$('.boardColumn:last').append(board[initial]);
initial++;
}
var colWidth = $('.boardColumn').width(), //i am declaring variable here
coloSize = $('.boardColumn').size();
boardViewer.css({
width:colCount*colWidth // i am applying the values here.
});
boardHolder.css({
width:coloSize * colWidth // i am applying the values here.
});
})
}
如果我错了,有人会指导我以正确的方式仅在顶部声明所有变量吗?