Question

So I have the following HTML:

<ul id="nav">
    <li><a href="somepage1.php" class="class1">Link1</a></li>
    <li><a href="somepage2.php" class="class2">Link2</a></li>
</ul>

jquery function:

(function($) {
    var imgList = [];
    $.extend({
        preload: function(imgArr, option) {
            var setting = $.extend({
                init: function(loaded, total) {},
                loaded: function(img, loaded, total) {},
                loaded_all: function(loaded, total) {}
            }, option);
            var total = imgArr.length;
            var loaded = 0;

            setting.init(0, total);
            for(var i in imgArr) {
                imgList.push($("<img />")
                    .attr("src", imgArr[i])
                    .load(function() {
                        loaded++;
                        setting.loaded(this, loaded, total);
                        if(loaded == total) {
                            setting.loaded_all(loaded, total);
                        }
                    })
                );
            }

        }
    });
})(jQuery);

and document ready trigger (simplified):

$.preload([
    'path/to/image.jpg',
    'path/to/image2.jpg',
    'path/to/image3.jpg',
    'path/to/image4.jpg',
]);

It works really well, but I need to enhance it. When user click on anchor with class 'class1', preloader should read images which are assigned to specific array. And I would need to write '$.preload' only once.

So my idea was to create two different array like:

var arraysClass1 = [
    'path/to/image.jpg',
    'path/to/image2.jpg',
    'path/to/image3.jpg',
    'path/to/image4.jpg'
];

var arraysClass2 = [
    'path/to/image5.jpg',
    'path/to/image6.jpg',
    'path/to/image7.jpg',
    'path/to/image8.jpg'
];

then create document ready event like bellow. Im stuck here and code bellow have errors

$('#nav a').live("click", (function(event){
    event.preventDefault();
    var getClass = '.'+$(this).attr("class");
    $(function() {
        $.preload([
            if(getClass=='.class1'){
                arrayClass1;
            }
            elseif(getClass=='.class2'){
                arrayClass2;
            }

        ]);
    });

I dont know how to create 'IF' logic inside $.preload([...]);

Was it helpful?

Solution

Here is the block of code that will do what you're looking for:

$('#nav a').live("click", function(event){
    event.preventDefault();
    usearr='';
    if ($(this).attr("class")=="class1") {
        usearr=arraysClass1;
    } else if ($(this).attr("class")=="class2") {
        usearr=arraysClass2;
    }
    $.preload(usearr);
});

OTHER TIPS

Why dont you find out the appropiate object outside $.preload([]) like following.

$('#nav a').live("click", (function(event){
    event.preventDefault();
    var getClass = '.'+$(this).attr("class");
    var targetObj ;
     if(getClass=='.class1'){
                targetObj = arrayClass1;
     }
     else if(getClass=='.class2'){
                targetObj = arrayClass2;
     }  
        $.preload(targetObj);
});

and you dont need another $(function()) inside a click callback.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top