I am creating a vertical timeline project. On each side I show a card with info from my JSON file. I need to show only 5 cards at a single time. I will need two buttons:
The first button will need to show the next 5 cards (Can keep clicking until the last item in the JSON) The JSON will vary in size so it will have to be dynamic and take in account for all items
The second button will need to show the previous 5 cards (Can keep clicking until initial state)
I have a button already with the class ".show-next", and another button with the class ".show-previous"
This is my code currently:
$.ajax({
method: "GET",
url: "./timeline.json",
}).done(function (data) {
const obj = data;
Object.entries(obj).forEach((card) => {
const [key, value] = card;
console.log(card);
value.subYearOne.forEach((point, index) => {
const isOdd = index % 2;
let imgLink = point.imageLink;
let title = point.Title;
let year = point.Year;
let desc = point.Description;
let buttonLink = point.buttonLink;
const appendToHtml =
`
<div class="container ${isOdd ? "right" : "left"}">
<div class="content">
<img src="${imgLink}" width="100%;">
<h2>${title}</h2>
<p>${year}</p>
<p>${desc}</p>
<a href="${buttonLink}"><button>Learn More</button></a>
</div>
</div>
`;
$(".timeline").append(appendToHtml);
});
});
});
$(".show-next").click(function () {
console.log("clicked next")
});
$(".show-previous").click(function () {
console.log("clicked previous")
});
This is my JSON file:
{
"1990": {
"subYearOne": [{
"Year": 1990,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=1",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 1991,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=2",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 1992,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=3",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 1993,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=4",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 1994,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=5",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 1995,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=6",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 1996,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=7",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 1997,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=8",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 1998,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=9",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 1999,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=10",
"buttonLink": "https://www.youtube.com/"
}
]
},
"2000": {
"subYearTwo": [{
"Year": 2000,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=11",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 2001,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=12",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 2002,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=13",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 2003,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=14",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 2004,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=15",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 2005,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=16",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 2006,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=17",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 2007,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=18",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 2008,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=19",
"buttonLink": "https://www.youtube.com/"
},
{
"Year": 2009,
"Month": "Feb",
"Day": "12",
"Title": "This is a Title",
"Description": "Grinder doppio black iced id, body est latte shop body.",
"imageLink": "https://picsum.photos/500/300?random=20",
"buttonLink": "https://www.youtube.com/"
}
]
}
}
What I have tried:
I've tried making a function where I add a .slice()