0

我在准备好文档上的计算变量时遇到问题。

我将容器宽度除以一个数字,我需要存储这个值,然后再使用它。

这里的脚本:

$(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 以便在同位素布局中定义我的 columnWidth 和 rowHeight。

当我在 colW 上发出警报时,colW 未定义......

4

1 回答 1

2

var colW 是一个局部变量

尝试将其移出您的功能

像这样定义你的var:

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

并在你的函数中使用它

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

您现在应该可以使用此 var 进行警报了。

于 2013-04-12T22:24:31.687 回答