【问题标题】:Random images and display it in a div随机图像并将其显示在 div 中
【发布时间】:2012-05-12 22:48:38
【问题描述】:

我发现了这个很棒的插件,它可以拉取随机图像并将其显示在一个 div 中。

Se 作者链接,

http://www.robwalshonline.com/posts/jquery-plugin-random-image-on-page-load/

是否可以让随机图像淡入?

(function($){

    $.randomImage = {
        defaults: {

            //you can change these defaults to your own preferences.
            path: 'images/', //change this to the path of your images
            myImages: ['containerBg.png', 'containerBg2.png'] //put image names in this bracket. ex: 'harold.jpg', 'maude.jpg', 'etc'

        }           
    }

    $.fn.extend({
            randomImage:function(config) {

                var config = $.extend({}, $.randomImage.defaults, config); 

                 return this.each(function() {

                        var imageNames = config.myImages;

                        //get size of array, randomize a number from this
                        // use this number as the array index

                        var imageNamesSize = imageNames.length;

                        var lotteryNumber = Math.floor(Math.random()*imageNamesSize);

                        var winnerImage = imageNames[lotteryNumber];

                        var fullPath = config.path + winnerImage;


                        //put this image into DOM at class of randomImage
                        // alt tag will be image filename.
                        $(this).attr( {
                                        src: fullPath,
                                        alt: winnerImage
                                    });


                }); 
            }

    });

如何修改代码?

【问题讨论】:

    标签: jquery


    【解决方案1】:

    尝试替换最后几行:

                        $(this).attr( {
                                        src: fullPath,
                                        alt: winnerImage
                                    });
    

    有了这个:

                        $(this).css('opacity',0.01).attr( {
                                        src: fullPath,
                                        alt: winnerImage
                                    }).fadeIn();
    

    【讨论】:

      【解决方案2】:

      我认为您不需要插件。 很短的FIDDLE

      【讨论】:

      • @Åke,请批准答案;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 2015-02-12
      • 2013-05-02
      相关资源
      最近更新 更多