幻灯片库以纠正页面加载时的位置?

时间:2011-08-09 08:16:13

标签: jquery

在页面加载时,javascript会使菜单按钮处于活动状态。

    $('#menu ul li.menuItem:eq(1)').addClass('act').siblings().addClass('inact');

但图库上的图像不会滑动到相应的菜单按钮。画廊总是以第一张图片开头。我可以添加什么,以便我在页面加载时设置为活动的任何菜单按钮,无论是(eq(1),eq(2),eq3)..),都可以显示正确的图像

javascript文件位于

之下
    $(document).ready(function(){
var totWidth=0;
var positions = new Array();

$('#slides .slide').each(function(i){
    /* Loop through all the slides and store their accumulative widths in totWidth */
    positions[i]= totWidth;
    totWidth += $(this).width();

    /* The positions array contains each slide's commulutative offset from the left part of the container */

    if(!$(this).width())
    {
        alert("Please, fill in width & height for all your images!");
        return false;
    }
});

$('#slides').width(totWidth);

/* Change the cotnainer div's width to the exact width of all the slides combined */


$('#menu ul li a').click(function(e){

    /* On a thumbnail click */
    $('li.menuItem').removeClass('act').addClass('inact');
    $(this).parent().addClass('act');

    var pos = $(this).parent().prevAll('.menuItem').length;

    $('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
    /* Start the sliding animation */

    e.preventDefault();
    /* Prevent the default action of the link */

});

1 个答案:

答案 0 :(得分:0)

$(window).load(function(){
    $('#menu ul li.menuItem:eq(1)').addClass('act').siblings().addClass('inact');
    $('#menu ul li.menuItem:eq(1) a').trigger('click'); // this will trigger an click at page load  
});