2013-04-12 58 views
0

我有一個問題在文檔準備好時獲得計算變量。jquery獲得可變文件準備好

我用容器寬度除以一個數字,我需要存儲這個值然後用它來。

這裏的腳本:

$(document).ready(function() { 

var $layout = $('.container-scroll'); 
var $container = $('#container'); 
var $items = $('.element'); 
var $filter = $('#filter a'); 

function checkWidth() { 

var windowSize = $(window).width(); 

if (windowSize <= 960) {  
    var $layout = $('.container-scroll'); 
    var colW = Math.round(($layout.width())/3)-1;  
    var ElementWidth = colW; 
    var ElementHeight = Math.round(colW*0.6); 
    $(".element").height(ElementHeight); 
    $(".element").width(ElementWidth); 
    $(".element.square").height(ElementHeight * 2); 
    $(".element.square").width(ElementWidth * 2); 
    $(".element.wide").height(ElementHeight); 
    $(".element.wide").width(ElementWidth * 2); 
} 
else if (windowSize > 960 && windowSize <= 1280) { 
    var $layout = $('.container-scroll'); 
    var colW = Math.round(($layout.width())/4)-1;  
    var ElementWidth = colW; 
    var ElementHeight = Math.round(colW*0.6); 
    $(".element").height(ElementHeight); 
    $(".element").width(ElementWidth); 
    $(".element.square").height(ElementHeight * 2); 
    $(".element.square").width(ElementWidth * 2); 
    $(".element.wide").height(ElementHeight); 
    $(".element.wide").width(ElementWidth * 2); 
} 
else if (windowSize > 1280 && windowSize <= 1600) { 
    var $layout = $('.container-scroll'); 
    var colW = Math.round(($layout.width())/5)-1; 
    var ElementWidth = colW; 
    var ElementHeight = Math.round(colW*0.6); 
    $(".element").height(ElementHeight); 
    $(".element").width(ElementWidth); 
    $(".element.square").height(ElementHeight * 2); 
    $(".element.square").width(ElementWidth * 2); 
    $(".element.wide").height(ElementHeight); 
    $(".element.wide").width(ElementWidth * 2); 
} 
else if (windowSize > 1600) { 
    var $layout = $('.container-scroll'); 
    var colW = Math.round(($layout.width())/6)-1; 
    var ElementWidth = colW; 
    var ElementHeight = Math.round(colW*0.6); 
    $(".element").height(ElementHeight); 
    $(".element").width(ElementWidth); 
    $(".element.square").height(ElementHeight * 2); 
    $(".element.square").width(ElementWidth * 2); 
    $(".element.wide").height(ElementHeight); 
    $(".element.wide").width(ElementWidth * 2); 
} 
} 

function checkIsotope() { 
$container.isotope({ 
    perfectMasonry: { 
    columnWidth: colW, 
    rowHeight: colW, 
    }, 
    masonryHorizontal: { 
    rowHeight: colW, 
    } 
}); 
} 

checkWidth(); 
checkIsotope(); 
var colW; 
alert(colW) 

$(window).smartresize(function() { 
    checkWidth(); 
    checkIsotope() 
}) 

} 

我想VAR colW爲了definie我columnWidth中和rowHeight同位素佈局。

當我把警報在colW,colW是不確定的...

回答

2

var colW是一個局部變量

嘗試移動了這一點,你的函數

的這樣定義你的變種:

var colW; 
$(document).ready(function() { 
...... 

並將其用於您的功能

colW = Math.round(($layout.width())/4)-1; 

你現在應該可以使用這個var來提醒。

+0

謝謝你的答案。如果我刪除或移動'var colW'後沒有任何工作。 colW仍未定義。 – lolo

+0

非常感謝!它現在解決了我所有的問題。我的腳本很好,但沒有這個GLOBAL變量就無法運行! – lolo