Rotate through groups of ten items

For my app's home page, I want to display a rotating list of ten items at a time. The displayed items should switch to the next set of ten items after a few seconds.

Currently, I have code that does this for one item, but I'm struggling to make it work for all ten items simultaneously.

var j = 0;
var inbetween = 2000; //milliseconds   
function page() {
  var jmax = $("ul#list li").length - 1;
  var count = 10;

  $("ul#list li:eq(" + j + ")")
    .animate({
      "opacity": "1"
    }, 400)
    .delay(inbetween)
    .animate({
      "opacity": "0"
    }, 400, function() {
      (j == jmax) ? j = 0: j++;
      page();
    });
};
page();
 ul#list {
   width: 200px;
   border: solid;
   position: relative;
   overflow: hidden;
   height: 200px
 }
 ul#list li {
   font-size: 1.4em;
   padding: 20px;
   opacity: 0;
   position: absolute
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id='list'>
  <li>one</li>
  <li>two</li>
  <li>three</li>
  <li>four</li>
  <li>five</li>
  <li>six</li>
  <li>seven</li>
  <li>eight</li>
  <li>nine</li>
  <li>ten</li>
  <li>eliven</li>
  <li>twelve</li>
  <li>thirteen</li>
  <li>fourteen</li>
  <li>fifteen</li>
  <li>sixteen</li>
  <li>seventeen</li>
  <li>eighteen</li>
  <li>ninteen</li>
  <li>twenty</li>
</ul>

A jsFiddle demo is available here.

Any suggestions or assistance would be greatly appreciated!

Answer №1

It appears that using position: absolute may not achieve the desired outcome. Perhaps you are looking to display a rotating subset of list items at any given time. Here is a revised version:

ul#list {
  width: 200px;
  border: solid;
  height: 200px
}
ul#list li {
  font-size: 1.4em;
  display: none;
  margin: 0; padding: 0;
}

Accompanied by this JavaScript code:

var j = 1;
var inbetween = 2000; //milliseconds   
function page() {
  var jmax = $("ul#list li").length;
  var count = 10;

  var start = j;
  var end = j + count - 1;
  var complete = 0;
  console.log(j, start, end);

  var range = $('ul#list li:nth-child(n+'+ start + '):nth-child(-n+'+  end +')');
  range
    .show(400)
    .delay(inbetween)
    .hide(400, 'swing', function() {
      if (j++ >= jmax) {
        j = 1;
      }

      if (++complete >= count) {
        page();  
      }
    });
};
page();

For an example, please visit: http://codepen.io/anon/pen/XJWpzK

The key modification is the use of the `nth-child` selector to target a specific range of elements based on the variable j. I hope this solution proves helpful.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript event in Chrome extension triggers a browser notification and allows for modification using a specific method

I am currently developing a Chrome extension that is designed to take specific actions when system notifications appear, with the main goal being to close them automatically. One example of such a notification is the "Restore pages?" prompt: https://i.sta ...

What impact does changing the Device Language have on a heading?

At the top of an html page, I have the word "Color" in a heading. If a user's device is set to British English, I would like the text to automatically switch to "Colour". What is the best way to accomplish this with minimal Javascript? ...

Using material community icons in conjunction with react-native-vector-icons

I am facing an issue with displaying the store-front icon from the Material Community Icons library in my React Native app. Here is the code snippet causing the problem: import { StatusBar } from "expo-status-bar"; import React from "react&q ...

Adjusting the border color when two checkboxes are chosen (and simultaneously deactivating the others)

I'm struggling to understand how to disable the remaining checkboxes after two are selected and change the border color of the "checkbox_group" to red. Can someone help me with this? Here is the JavaScript code I have so far: $(document).ready(funct ...

Toggle between weekends and weekdays on FullCalendar

Is there a way to customize Arshaw's FullCalendar by toggling the display of weekends and changing the timeslot interval dynamically? To elaborate: //Clicking a button to toggle the display of weekends $('#weekend-yes').click(function( ...

What causes my child element to be positioned with respect to the body even though its parent is set to relative positioning?

Here is the HTML code I am working with: <html> <head></head> <body> <div id="board"> <div class="person"></div> </div> </body> </html> And here is the ...

The Jquery code is failing to execute the AJAX GET request

I've included jQuery in my code, but despite that, it seems like my script is not functioning properly. I suspect the issue lies with how I am loading jQuery rather than with my ajax request. There are no error messages appearing and the console.log ...

Node JS Callback function does not produce a return value

After diving into the world of node js Development, I recently acquired knowledge about node js. In my journey, I have created a router file that looks something like this: import express from 'express'; import storyController from '../../c ...

Ways to apply attributes to specific images using a JQuery array

I'm struggling to add attributes to multiple images using a jQuery array. Can anyone provide some assistance? <div id="result"> <img src="Image1" width="300"/> <img src="Image2" width="300"/> <img src="Image3" width="300"/> ...

Are you experimenting with an AngularJS directive?

What would be your approach to testing this angular testing directive? $scope.cancel = function () { $('#pp_bsktexit').modal('show'); }; Something similar to this can be tested, but it doesn't seem too complex: var ...

Updating meta tags dynamically for Facebook sharing using Angular 6

I am struggling to dynamically update meta tags such as og:title, og:description, and og:image for sharing on Facebook. I have attempted various methods without success. Initially, I tried setting meta tags with JavaScript like this: var meta = document. ...

What is the best way to postpone the display of progress if the AJAX request finishes in fewer than X seconds

When I have multiple AJAX calls on a webpage, some complete quickly while others take longer depending on the action clicked. I am looking to implement a "loader" that shows up after a certain number of seconds when AJAX is processing results. I have man ...

Can I utilize a script tag within an SVG element in a React application?

I have come across some SVG images that contain scripts within them (specifically for handling the onClick event), like this: <svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs ...

Implement an event handler in Jquery for a link that includes a specific hash

Is there a way to add an event to a link with a specific hash? For example: <a href="index.php/#about">About</a> Then select this link in jQuery and add a click event, for example: $('a[href$="#about"]').click(function() { ...

How can I adjust the font size in a TextField when it is in focus?

As a novice in ReactJS, I am currently utilizing materia-ui to design a page. I am looking to make a custom change on a TextField where the font size adjusts when text is entered. However, adjusting the font size creates too much space between the label a ...

React Tour displays incorrect positions when coupled with Slide transitions in Material UI Dialog

Currently, I am utilizing the react-tour library to incorporate a guidance feature into my project. The issue arises in the initial step which involves a small component within a <Dialog /> that requires highlighting. However, due to a transition ef ...

Retrieving data using jQuery Mobile

What is the most efficient method for aggregating data from various websites or pages into a single application that updates automatically? For example, if I wanted to display articles from Yahoo, BBC, and The Times in a listview format that constantly ref ...

align a pair of HTML elements

One of the features on my website is a text area where users can input text. <textarea id="realAreaWhereUserTypes"></textarea> In addition to the text area, there is also a hidden input field like this: <input id="hiddenUserInput" /> ...

Attempting to display HTML entities within my Express EJS application

Revealing the page that needs to be displayed: <% include ../partials/boilerplate %> <div class="triviaContainer"> <h1>GAME TIME!!! </h1> <% var i =0; data.forEach(question =>{ i+=1 %> <div cla ...

Ensuring Your IMG is Perfectly Centered in DIV Across all Browsers

I have tried all the tips from the top Google results, but I am still struggling to center an image within a div. For example, the popular tricks mentioned in this link: or http://www.w3.org/Style/Examples/007/center.en.html do not seem to work in IE 8. ...