0

我使用以下脚本构建网站。有时这个 javascript 崩溃有时不会。奇怪的是,当我在sliderLeft 定义后的第三行添加警告框时,脚本永远不会崩溃。有人请帮忙。我为不同的输出使用了两次相同的功能。

即使我删除了第二个类似的功能,我仍然遇到错误。请帮我解决一下这个。

$(window).load(function() {             
    var sliderLeft=$('#thumbScroller .container').position();
    //alert(sliderLeft);
 //   padding=$('#outer_container').css('paddingRight').replace("px", "");
    var sliderWidth=$(window).width()
    $('#thumbScroller').css('width',sliderWidth);
    var totalContent=0;
    $('#thumbScroller .content').each(function () {
        totalContent+=$(this).innerWidth();
        $('#thumbScroller .container').css('width',totalContent);
    });
    //alert(sliderLeft);
    $('#thumbScroller').mousemove(function(e){
        if($('#thumbScroller  .container').width()>sliderWidth){
            var mouseCoords=(e.pageX - this.offsetLeft);
            var mousePercentX=mouseCoords/sliderWidth;
            var destX=-(((totalContent-(sliderWidth))-sliderWidth)*(mousePercentX));
            var thePosA=mouseCoords-destX;
            var thePosB=destX-mouseCoords;
            var animSpeed=600; //ease amount
            var easeType='easeOutCirc';
            if(mouseCoords==destX){
                $('#thumbScroller .container').stop();
            }
            else if(mouseCoords>destX){
                //$('#thumbScroller .container').css('left',-thePosA); //without easing
                $('#thumbScroller .container').stop().animate({left: -thePosA}, animSpeed,easeType); //with easing
            }
            else if(mouseCoords<destX){
                //$('#thumbScroller .container').css('left',thePosB); //without easing
                $('#thumbScroller .container').stop().animate({left: thePosB}, animSpeed,easeType); //with easing
            }
        }
    });
    $('#thumbScroller  .thumb').each(function () {
        $(this).fadeTo(fadeSpeed, 0.6);
    });
    var fadeSpeed=200;
    $('#thumbScroller .thumb').hover(
    function(){ //mouse over
        $(this).fadeTo(fadeSpeed, 1);
    },
    function(){ //mouse out
        $(this).fadeTo(fadeSpeed, 0.6);
    }
);
});
$(window).resize(function() {
    //$('#thumbScroller .container').css('left',sliderLeft); //without easing
    $('#thumbScroller .container').stop().animate({left: sliderLeft}, 400,'easeOutCirc'); //with easing
    $('#thumbScroller').css('width',$(window).width());
    sliderWidth=$(window).width();
});

我只是按照建议删除了第二个函数,但仍然无法删除几行和脚本崩溃并且不显示任何错误

4

2 回答 2

0

您有 2 个$(window).load(function() {...});函数定义。

尝试将它们组合成 1 个$(window).load(function() {...});电话。

于 2012-11-28T05:14:53.213 回答
0

只是一个想法...

很难说没有看到问题(正如 3dgoo 所说,尝试将您的代码放入 jsfiddle.net)但是您确定您的代码在 dom 准备好时执行吗?

它解释说,您在代码中添加的“警报”通过给它时间完成加载来“解决”问题。

于 2012-11-29T08:39:12.127 回答