我的Jquery幻灯片效果非常好。但是,幻灯片的宽度由<head>
中的Jquery代码控制,请参见示例
<script type="text/javascript">
$(document).ready(function(){
var currentPosition = 0;
var slideWidth = 720;
var slides = $('.slide');
var numberOfSlides = slides.length;
我需要做的是将宽度设置为100%,我该怎么做?每次我使用%符号幻灯片停止工作?
干杯, 安迪
这是完整的代码:
<script type="text/javascript">
$(document).ready(function(){
var currentPosition = 0; var slideWidth = 720; var slides = $('.slide'); var numberOfSlides = slides.length;
// Remove scrollbar in JS
$('#slidesContainer').css('overflow', 'hidden');
// Wrap all .slides with #slideInner div
slides
.wrapAll('<div id="slideInner"></div>')
// Float left to display horizontally, readjust .slides width
.css({
'float' : 'left',
'width' : slideWidth
});
// Set #slideInner width equal to total width of all slides
$('#slideInner').css('width', slideWidth * numberOfSlides);
// Insert controls in the DOM
$('#slideshow')
.prepend('<span class="control" id="leftControl">Clicking moves left</span>')
.append('<span class="control" id="rightControl">Clicking moves right</span>');
// Hide left arrow control on first load
manageControls(currentPosition);
// Create event listeners for .controls clicks
$('.control')
.bind('click', function(){
// Determine new position
currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
// Hide / show controls
manageControls(currentPosition);
// Move slideInner using margin-left
$('#slideInner').animate({
'marginLeft' : slideWidth*(-currentPosition)
});
});
// manageControls: Hides and Shows controls depending on currentPosition
function manageControls(position){
// Hide left arrow if position is first slide
if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
// Hide right arrow if position is last slide
if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
}
});
</script>
答案 0 :(得分:1)
试试这个:
var slideWidth = document.body.offsetWidth;
答案 1 :(得分:0)
试试这个:
<script type="text/javascript">
$(document).ready(function(){
var currentPosition = 0;
var slideWidth = getInt($("body").css("width"));
var slides = $('.slide');
var numberOfSlides = slides.length;
function getInt(num)
{
if (isNaN(num)) {
return num.replace(/[^-0-9]/g,"");
} else {
return num;
}
}
</script>
虽然它不是一个完美的解决方案,但它至少会在加载时将宽度设置为页面宽度。如果要在用户调整浏览器窗口大小时修复resize
变量,则必须为正文上的slideWidth
事件创建事件处理程序。