Logical algorithm for previous and next items

This can be used on pictures, news, shop items, etc. with JavaScript events for “previous” and “next”

    if (prev === null) {
      prev = current && next = current + 1
    } elseif (next === null) {
      next = current && prev = current -1
    } else {
      if (action == prev) {
        next = current
        prev = current - 1
      } elseif (action == next) {
        prev = current
        next = current + 1
      } else {
        prev = current - 1
        next = current + 1
      }
    }

Tags: , , ,

Leave a Reply