1

我有一个问题,即第一次加载文件时脚本似乎没有完全加载(在清除缓存和 cookie 之后)。再次刷新页面使脚本运行良好。每次我清除缓存时都会发生这种情况,因此基本上它不会在第一次运行时加载。

我不知道如何验证我的 Jquery 是否没有造成不必要的问题。 http://jsfiddle.net/Lp6SX/1/

jQuery(document).ready(function ($) {
$(window).load(function () {

    var fitem = 0;
    var litem = 0;
    var mitem = 0;

    function mycarousel_initCallback(carousel) {
        jQuery('.productbox').bind('click', function () {
            clearInterval(pt);
            clearInterval(nt);

            $('#mycarousel-prevpause').hide();
            $('#mycarousel-nextpause').hide();
            $('#next').show();
            $('#prev').show();

            var clickedId = jQuery(this).parent().attr('jcarouselindex');
            var totaldiff = 0;
            console.log(clickedId + "==>" + mitem);
            if (clickedId == mitem) {
                var c_buy = jQuery(this).parent().attr('data-byitnow');
                //alert("as"+c_buy);
                window.open(c_buy, '_blank');
                window.focus();
                //window.open =c_buy;  
            }

            if (clickedId > mitem) {
                totaldiff = clickedId - mitem;
                //console.log('clickedId '+totaldiff);
                if (totaldiff > 0) {
                    carousel.stopAuto();
                    carousel.options.scroll = totaldiff;

                    carousel.next();

                }
            } else {
                totaldiff = mitem - clickedId;

                if (totaldiff > 0) {
                    carousel.stopAuto();
                    carousel.options.scroll = totaldiff;

                    carousel.prev();

                }
            }


            return false;
        });

        jQuery('.jcarousel-scroll select').bind('change', function () {
            carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
            return false;
        });

        jQuery('#mycarousel-next').bind('click', function () {
            $('#mycarousel-prevpause').hide();
            $('#mycarousel-nextpause').hide();
            $('#next').show();
            $('#prev').show();
            clearInterval(pt);
            clearInterval(nt);
            carousel.stopAuto();
            carousel.options.scroll = 5;
            carousel.next();
            return false;
        });

        jQuery('#mycarousel-prev').bind('click', function () {
            $('#mycarousel-prevpause').hide();
            $('#mycarousel-nextpause').hide();
            $('#next').show();
            $('#prev').show();
            clearInterval(pt);
            clearInterval(nt);
            carousel.stopAuto();
            carousel.options.scroll = 5;
            carousel.prev();
            return false;
        });

        jQuery('#mycarousel-prevpause').bind('click', function () {
            clearInterval(pt);
            clearInterval(nt);
            $('#prev').show();
            carousel.stopAuto();
            $(this).hide();
            $('#mycarousel-nextpause').hide();
            return false;
        });

        jQuery('#mycarousel-nextpause').bind('click', function () {
            clearInterval(pt);
            clearInterval(nt);
            $('#next').show();
            carousel.stopAuto();
            $(this).hide();
            $('#mycarousel-prevpause').hide();
            return false;
        });

        jQuery('#prev').bind('click', function () {
            $('#mycarousel-prevpause').show();
            $('#mycarousel-nextpause').hide();
            $('#next').show();
            carousel.stopAuto();
            continue_prev(carousel);
            $(this).hide();
            return false;
        });

        jQuery('#next').bind('click', function () {
            $('#mycarousel-nextpause').show();
            $('#mycarousel-prevpause').hide();
            $('#prev').show();
            carousel.stopAuto();
            continue_next(carousel);
            $(this).hide();
            return false;
        });
    };
    var nt;
    var pt;

    function continue_next(carousel) {
        clearInterval(pt);
        nt = setInterval(function () {
            carousel.options.scroll = 1;
            carousel.prev();
        }, 2000);
    }

    function continue_prev(carousel) {
        clearInterval(nt);
        pt = setInterval(function () {
            carousel.options.scroll = 1;
            carousel.next();
        }, 2000);
    }

    function mycarousel_reloadCallback(carousel) {
        //console.log('Carousel reloaded'+fitem+"==>"+litem);
        makeCenterActive();
    };

    /**
     * This is the callback function which receives notification
     * when an item becomes the first one in the visible range.
     */
    function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
        //console.log('Item #' + idx + ' is now the first item');
        fitem = idx;
    };

    /**
     * This is the callback function which receives notification
     * when an item becomes the first one in the visible range.
     */
    function mycarousel_itemLastInCallback(carousel, item, idx, state) {
        //console.log('Item #' + idx + ' is now the last item');
        litem = idx;
        makeCenterActive(carousel);
    };

    function makeCenterActive(carousel) {
        //console.log('Carousel reloaded'+fitem+"==>"+litem);

        mitem = ((litem - fitem) / 2) + fitem;
        var middleItemId = $('.jcarousel-item-' + mitem + '').attr('id');
        //console.log('Middle Item'+mitem+"==>"+middleItemId);


        $('.activeBox').each(function () {
            //$(this).animate({height: "75px",width:"75px",marginTop:"0px"}, '200').removeClass('activeBox');
            $(this).removeClass('activeBox');
            //$(this).transition({ scale: 1 });
            $(this).transition({
                height: "109px",
                width: "135px",
                marginTop: "0px"
            });
            $(this).children().children('img').transition({
                "max-width": "105px",
                "max-height": "105px"
            });
        });

        $('.jcarousel-item-' + mitem + '').each(function () {
            //$(this).animate({height: "98px",width:"89px",marginTop:"-10px",marginLeft:"0px"}, '200').addClass('activeBox');
            //$(this).animate({width: "89px !important", height: "105px !important"}, 'slow');
            $(this).addClass('activeBox');
            //$(this).transition({ scale: 1.3 });
            $(this).transition({
                height: "140px",
                width: "140px",
                marginTop: "-17px",
                marginLeft: "0px"
            });
            $(this).children().children('img').transition({
                "max-width": "105px",
                "max-height": "105px"
            });

        });


        var c_time = $('#' + middleItemId + '').attr('data-timeremain');
        var c_buy = $('#' + middleItemId + '').attr('data-byitnow');
        var c_price = $('#' + middleItemId + '').attr('data-price');
        $('#timeremain').html('Current' + c_time);
        $('#byitnow').html('<a href="' + c_buy + '">Price :</a>');
        $('#price').html(c_price);
        //carousel.reload();
    }

    // Ride the carousel...
    jQuery(document).ready(function () {
        jQuery("#mycarousel").jcarousel({
            scroll: 1,
            auto: 1,
            wrap: 'circular',
            initCallback: mycarousel_initCallback,
            reloadCallback: mycarousel_reloadCallback,
            itemFirstInCallback: mycarousel_itemFirstInCallback,
            itemLastInCallback: mycarousel_itemLastInCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null
        });
    });

})
});

它用于修改轮播滑块。任何人都可以检查代码,看看你是否发现它有任何问题?

谢谢你的时间。

4

2 回答 2

4

你有这个代码结构:

 $(window).load(function(){

    // code part 1 here ..

    jQuery(document).ready(function (){ 

        // code part 2 here .. 

    });
 });

零件内部window.load()。我认为这没有意义,你只需要一个。您可以将所有内容包装在文档就绪回调中,如下所示:

jQuery(document).ready(function(){
    // code part 1 here .. 
    // code part 2 here .. 
});

希望有帮助!

于 2013-06-26T19:14:06.640 回答
0

首先,您缺少一些右括号。您还将 $(window).load() 放在 jQuery(document).ready() 中,这样它就不会运行,然后将另一个 jQuery(document).ready() 放在这两个里面,这样它就不会运行了. 您应该只使用 1 个这样的语句:

jQuery(document).ready(function() {
    ...
});

并将所有内容放入其中(变量、函数声明和 jQuery $(..) 操作)。

而且我认为您应该决定统一命名约定并仅使用 jQuery 或 $ 美元符号,而不是同时使用两者。

于 2013-06-26T19:19:22.510 回答