2011-09-20 48 views
0

我正在使用自定義滾動div jQuery插件。我希望div根據瀏覽器窗口大小填充其父級寬度的100%以更改大小。如何將jQuery中的寬度參數作爲像素整數更改爲百分比?

該腳本獲取給定的width參數,並在整個腳本中使用該參數。腳本的自定義參數似乎只允許我指定像素width,作爲一個整數:

$.fn.hoverscroll.params = { 

vertical: false,  // Display the list vertically or not 
width:  1280,  // Width of the list 
height:  230,   // Height of the list 
arrows:  true,  // Display arrows to the left and top or the top and bottom 
arrowsOpacity: 0.4, // Maximum opacity of the arrows if fixedArrows 
fixedArrows: false,  // Fix the displayed arrows to the side of the list 
rtl:  false,  // Set display mode to "Right to Left" 
debug:  false  // Display some debugging information in firebug console 

}; 

我想引用百分比,'100%',但沒有奏效。有沒有在這裏使用百分比的方法?

如果沒有,有沒有辦法使用下面的腳本,它決定了窗口的寬度,使腳本輸出的像素寬度,並使用該整數作爲jQuery腳本中的參數width

<script type="text/javascript"> 
    function alertSize() { 
    var myWidth = 0; 
    if(typeof(window.innerWidth) == 'number') { 

    //Non-IE 
    myWidth = window.innerWidth; 
    } else if(document.documentElement && (document.documentElement.clientWidth)) { 

    //IE 6+ in 'standards compliant mode' 
    myWidth = document.documentElement.clientWidth; 
    } else if(document.body && (document.body.clientWidth)) { 

    //IE 4 compatible 
    myWidth = document.body.clientWidth; 
    } 
    //window.alert(myWidth); 
} 
</script> 

以供參考,在這裏是插件的完整劇本,我使用: http://rascarlito.free.fr/hoverscroll/js/hoverscroll/jquery.hoverscroll.js

回答

0

如果你想hoverscroll區域的寬度是在準備一個容器的一定百分比,你總是可以使用jQuery。包裝你的啓動程序在ready()表達:

$(document).ready(function() { 
    var w = $(window).width(); 
    var p = 0.95 /* Your percentage */ 
    $.fn.hoverscroll.params = { 
     width: w * p, // Width of the list 
     ... 
    }; 
    // Start your hoverscroll like normal; 
}); 

...只是有表示「其他的東西放在這裏。」我不想用重複的代碼填充答案。

雖然在正常情況下,你永遠不會修改到位的默認值,但它們作爲參數傳遞:

$(document).ready(function() { 
    var w = $(window).width(); 
    var p = 0.95 /* Your percentage */ 
    $('#mylist').hoverscroll({ 
     width: w * p // Width of the list 
    }); 
}); 

請注意,我只路過的寬度;其他一切都使用默認值。您只需設置與默認設置不同的設置,在本例中,我只設置了寬度。