Problem with a variable
I'm working on a simple if/else
statement to show a next or previous link, but somehow my checkup don't work. Here's the code
var newPosition = currPosition;
$(".imageNavLink").click(function(event){
event.preventDefault();
newPosition = (this.rel == "next") ? newPosition + 1 : newPosition - 1;
var newImageLink = this.title;
var clickAction = this.rel;
var newImage = new Image();
开发者_开发知识库 $(newImage).load(function(){
newWidth = this.width, newHeight = this.height+35;
if(newPosition == totalItems){
[.. Show 'previous' link ..]
}else if(newPosition == 1){
[.. Show 'next' link ..]
}else{
[.. Show 'previous' and 'next' link ..]
}
});
newImage.src = this.title;
});
currPosition
holds the current position (numeric) of the element to keep on track with the counttotalItems
is the numeric value of the total associated images
If i click on the first image i constantly see the previous link, when i start with the second image i constantly see both links and when i start with the last image i constantly see the previous link..
I dont know if this is it but,
newWidth = this.width, newHeight = this.height+35;
Shouldn't the ,
after this.width
be a ;
?
if(newPosition == totalItems){
[.. Show 'previous' link ..]
}else if(newPosition == 1){
[.. Show 'next' link ..]
}else{
[.. Show 'previous' and 'next' link ..]
}
rewrite to this:
if(newPosition>1 && newPosition<totalItems){
[.. Show 'previous' and 'next' link ..]
}else if(newPosition == 1){
[.. Show 'next' link ..]
}else if(newPosition == totalItems){
[.. Show 'previous' link..]
}
Only if newPosition
is between 1
and totalItems
will both prev
and next
be shown
精彩评论