代码之家  ›  专栏  ›  技术社区  ›  RandyMorris

向jquery$(window.load函数声明中添加多个函数的正确方法是什么?

  •  1
  • RandyMorris  · 技术社区  · 15 年前

    我使用以下javascript在jquery中使用nivo slider对象来设置两个幻灯片的动画:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>  
    <script src="jquery.nivo.slider.pack.js" type="text/javascript"></script>  
    <script type="text/javascript">  
    $(window).load(function() {  
        $('#sliderone').nivoSlider({  
            effect:'fade',  
            slices:15,  
            animSpeed:500,  
            pauseTime:7000,  
            startSlide:0, //Set starting Slide (0 index)  
            directionNav:false, //Next & Prev  
            directionNavHide:true, //Only show on hover  
            controlNav:false, //1,2,3...  
            controlNavThumbs:false, //Use thumbnails for Control Nav  
            controlNavThumbsSearch: '.jpg', //Replace this with...  
            controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src  
            keyboardNav:false, //Use left & right arrows  
            pauseOnHover:true, //Stop animation while hovering  
            manualAdvance:false, //Force manual transitions  
            captionOpacity:0.8, //Universal caption opacity  
            beforeChange: function(){},  
            afterChange: function(){},  
            slideshowEnd: function(){} //Triggers after all slides have been shown  
        });  
    });  
    $(window).load(function() {  
        $('#slidertwo').nivoSlider({  
            effect:'fade',  
            slices:15,  
            animSpeed:500,  
            pauseTime:7000,  
            startSlide:0, //Set starting Slide (0 index)  
            directionNav:false, //Next & Prev  
            directionNavHide:true, //Only show on hover  
            controlNav:false, //1,2,3...  
            controlNavThumbs:false, //Use thumbnails for Control Nav  
            controlNavThumbsSearch: '.jpg', //Replace this with...  
            controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src  
            keyboardNav:false, //Use left & right arrows  
            pauseOnHover:true, //Stop animation while hovering  
            manualAdvance:false, //Force manual transitions  
            captionOpacity:0.8, //Universal caption opacity  
            beforeChange: function(){},  
            afterChange: function(){},  
            slideshowEnd: function(){} //Triggers after all slides have been shown  
        });  
    });  
    </script>    
    

    任何关于如何正确地做到这一点的建议都将不胜感激。

    4 回复  |  直到 15 年前
        1
  •  2
  •   Felix Kling    15 年前

    由于两个回调的代码几乎相同,因此我将其重构为:

    $(window).load(function() {  
        $('#sliderone, #slidertwo').nivoSlider({  
            effect:'fade',  
            slices:15,  
            animSpeed:500,  
            pauseTime:7000,  
            startSlide:0, //Set starting Slide (0 index)  
            directionNav:false, //Next & Prev  
            directionNavHide:true, //Only show on hover  
            controlNav:false, //1,2,3...  
            controlNavThumbs:false, //Use thumbnails for Control Nav  
            controlNavThumbsSearch: '.jpg', //Replace this with...  
            controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src  
            keyboardNav:false, //Use left & right arrows  
            pauseOnHover:true, //Stop animation while hovering  
            manualAdvance:false, //Force manual transitions  
            captionOpacity:0.8, //Universal caption opacity  
            beforeChange: function(){},  
            afterChange: function(){},  
            slideshowEnd: function(){} //Triggers after all slides have been shown  
        });  
    });
    

    还可以考虑改用document ready事件: $(document).ready(function(){...}) 或短 $(function(){...}) .


    假设两个滑块的外观和工作方式完全相同,这段代码使得对它们进行更改更加容易,因为您只需要进行更改 一旦 (提高可维护性和可读性)。

        2
  •  2
  •   Robert    15 年前

    你只需要一份window.load 你可以把这两个函数都放进去。

    $(window).load(function() {  
        $('#sliderone').nivoSlider({  
            effect:'fade',  
            slices:15,  
            animSpeed:500,  
            pauseTime:7000,  
            startSlide:0, //Set starting Slide (0 index)  
            directionNav:false, //Next & Prev  
            directionNavHide:true, //Only show on hover  
            controlNav:false, //1,2,3...  
            controlNavThumbs:false, //Use thumbnails for Control Nav  
            controlNavThumbsSearch: '.jpg', //Replace this with...  
            controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src  
            keyboardNav:false, //Use left & right arrows  
            pauseOnHover:true, //Stop animation while hovering  
            manualAdvance:false, //Force manual transitions  
            captionOpacity:0.8, //Universal caption opacity  
            beforeChange: function(){},  
            afterChange: function(){},  
            slideshowEnd: function(){} //Triggers after all slides have been shown  
        });   
        $('#slidertwo').nivoSlider({  
            effect:'fade',  
            slices:15,  
            animSpeed:500,  
            pauseTime:7000,  
            startSlide:0, //Set starting Slide (0 index)  
            directionNav:false, //Next & Prev  
            directionNavHide:true, //Only show on hover  
            controlNav:false, //1,2,3...  
            controlNavThumbs:false, //Use thumbnails for Control Nav  
            controlNavThumbsSearch: '.jpg', //Replace this with...  
            controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src  
            keyboardNav:false, //Use left & right arrows  
            pauseOnHover:true, //Stop animation while hovering  
            manualAdvance:false, //Force manual transitions  
            captionOpacity:0.8, //Universal caption opacity  
            beforeChange: function(){},  
            afterChange: function(){},  
            slideshowEnd: function(){} //Triggers after all slides have been shown  
        });  
    });
    
        3
  •  1
  •   Starx    15 年前

    尝试使用类而不是id。看起来所有的选项都是一样的,所以你不需要使用相同的代码两次。尝试使用类并将多个窗口加载不要制造任何麻烦

    这样地

    $(window).load(function() {  
        $('.sliders').nivoSlider({  
            effect:'fade',  
            slices:15,  
            animSpeed:500,  
            pauseTime:7000,  
            startSlide:0, //Set starting Slide (0 index)  
            directionNav:false, //Next & Prev  
            directionNavHide:true, //Only show on hover  
            controlNav:false, //1,2,3...  
            controlNavThumbs:false, //Use thumbnails for Control Nav  
            controlNavThumbsSearch: '.jpg', //Replace this with...  
            controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src  
            keyboardNav:false, //Use left & right arrows  
            pauseOnHover:true, //Stop animation while hovering  
            manualAdvance:false, //Force manual transitions  
            captionOpacity:0.8, //Universal caption opacity  
            beforeChange: function(){},  
            afterChange: function(){},  
            slideshowEnd: function(){} //Triggers after all slides have been shown  
        });  
    }); 
    

    现在给你的两个滑块上课 sliders

        4
  •  1
  •   tarn    15 年前

    $(function() {
    
        $("#Slider1"). ..
    
        $("#Slider2"). ..
    
    });