javascript - Change class on multiple img in array -
this first post here, found solutions on page, thank that.
i have problem .removeclass
, .addclass
in last program. load multiple pictures array frames
, want change (previous-image
) (current-image
) in frames[0]
. here code, change class on second image. here code:
function loadimage() { // creates new <li> var li = document.createelement("li"); // generates image file name using incremented "loadedimages" variable var imagename = "graphics/img/dodge_viper_srt10_2010_360_720_50-" + (loadedimages + 1) + ".jpg"; var imagename1 = "graphics/img/dodge_viper_srt10_2010_360_720_50-" + (loadedimages + 1) + ".jpg"; /* creates new <img> , sets src attribute point file name generated. hides image applying "previous-image" css class it. image added <li>. */ var image = $('<img>').attr('src', imagename).addclass("previous-image").appendto(li) && $('<img>').attr('src', imagename1).addclass("previous-image light-image").appendto(li); // add newly added image object (returned jquery) "frames" array. frames.push(image); // add <li> <ol> $images.append(li); /* adds "load" event handler new image. when event triggers calls "imageloaded" function. */ $(image).load(function() { imageloaded(); }); }; function imageloaded() { // increments value of "loadedimages" variable loadedimages++; // updates preloader percentage text $("#spinner span").text(math.floor(loadedimages / totalframes * 100) + "%"); // checks if loaded image last 1 in sequence... if (loadedimages == totalframes) { // ...if so, makes first image in sequence visible removing "previous-image" class , applying "current-image" on frames[0].removeclass("previous-image").addclass("current-image"); /* displays image slider using jquery "fadeout" animation , complete event handler. when preloader faded, stops preloader rendering , calls "showthreesixty" function display images. */ $("#spinner").fadeout("slow", function() { spinner.hide(); showthreesixty(); }); } else { // ...if not, loads next image in sequence loadimage(); } };
this is, how looks in browser:
<ol><li><img src="graphics/img/dodge_viper_srt10_2010_360_720_50-1.jpg" class="previous-image"><img src="graphics/img/dodge_viper_srt10_2010_360_720_50-1.jpg" class="light-image current-image"></li></ol>
this is, want:
<ol><li><img src="graphics/img/dodge_viper_srt10_2010_360_720_50-1.jpg" class="current-image"><img src="graphics/img/dodge_viper_srt10_2010_360_720_50-1.jpg" class="light-image current-image"></li></ol>
when change
var image = $('<img>').attr('src', imagename).addclass("previous-image").appendto(li) && $('<img>').attr('src', imagename1).addclass("previous-image light-image").appendto(li);
to this
var image = $('<img>').attr('src', imagename1).addclass("previous-image light-image").appendto(li) && $('<img>').attr('src', imagename).addclass("previous-image").appendto(li);
it still change second img. help?
var image = $('<img>').attr('src', imagename).addclass("previous-image").appendto(li) && $('<img>').attr('src', imagename1).addclass("previous-image light-image").appendto(li);
is not doing think is. it's using second element declare. both appended page (because appendto method runs), && logical operator, it's not used concatenation, variable "image" contains second image declared.
this work instead:
var image = $('<img>', { "src": imagename, "class": "previous-image" }); image = image.add($('<img>', { "src": imagename1, "class": "previous-image light-image" })); image.appendto(li);
Comments
Post a Comment