Implementado https://www.npmjs.com/package/vanilla-lazyload:
Instalar con el comando:
npm i vanilla-lazyload
CarouselItem.js:
import h from 'hyperscript'
import moment from 'moment'
const relativeDate = dateStr => moment(dateStr, 'YYYY-MM-DD').fromNow()
const Controls = ({ slug, youtubeVideoId }) =>
h(
'div',
h(
'a',
{
href: `https://www.youtube.com/watch?v=${youtubeVideoId}`,
title: 'Watch trailer',
target: '_blank',
rel: 'noreferrer',
},
h('img.carousel-item__details--img', {
src: 'assets/play-icon.png',
alt: 'Play',
})
),
h(
'a',
{
href: `https://kitsu.io/explore/anime/${slug}`,
title: 'See more',
target: '_blank',
rel: 'noreferrer',
},
h('img.carousel-item__details--img', {
src: 'assets/plus-icon.png',
alt: 'More info',
})
)
)
const CarouselItem = ({
imageUrl,
title,
subtitle,
slug,
youtubeVideoId,
startDate,
}) =>
h(
'div.carousel-item',
h('img.carousel-item__img.lazy', { 'data-src': imageUrl, alt: '' }),
h(
'div.carousel-item__details',
Controls({ slug, youtubeVideoId }),
h('p.carousel-item__details--title', title),
h('p.carousel-item__details--subtitle', subtitle),
h(
'p.carousel-item__details--date',
`Released: ${relativeDate(startDate)}`
)
)
)
export default CarouselItem
index.js:
import h from 'hyperscript'
import { fetchPopular, fetchHighestRated, fetchTrending } from './api'
import CarouselItem from './CarouselItem'
import LazyLoad from 'vanilla-lazyload'
var lazyLoadInstance = new LazyLoad({
// Your custom settings go here
})
const SectionTitle = title => h('h3.carousel__title', title)
const Carousel = ({ itemsList = [] }) =>
h(
'section.carousel',
h(
'div.carousel__container',
itemsList.map(
({
attributes: { titles, posterImage, slug, youtubeVideoId, startDate },
}) =>
CarouselItem({
imageUrl: posterImage.medium,
title: titles.en,
subtitle: titles.ja_jp,
slug,
youtubeVideoId,
startDate,
})
)
)
)
!(async function(document) {
const mountReference = document.querySelector('.main').lastElementChild
if (!mountReference) {
return 0
}
const trending = await fetchTrending()
const popular = await fetchPopular()
const highestRated = await fetchHighestRated()
mountReference
.insertAdjacentElement('afterend', SectionTitle('Trending Anime'))
.insertAdjacentElement(
'afterend',
Carousel({
itemsList: trending,
})
)
.insertAdjacentElement('afterend', SectionTitle('Highest Rated Anime'))
.insertAdjacentElement(
'afterend',
Carousel({
itemsList: highestRated,
})
)
.insertAdjacentElement('afterend', SectionTitle('Most Popular Anime'))
.insertAdjacentElement(
'afterend',
Carousel({
itemsList: popular,
})
)
lazyLoadInstance.update()
})(document, window)
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?