M
M
mrrangerr2020-04-16 18:55:50
JavaScript
mrrangerr, 2020-04-16 18:55:50

How to make a hover effect for a list of images?

Good evening everyone. I need to make a hover effect for each image in an image list. How to do it please tell me

function ListPhotos() {
    const [photos, setPhotos] = useState([])
    const [perPage, setPerpage] = useState(1)
    const [hovered, setHovered] = useState(false)
   

    const getData = useCallback(() => {
        unsplash.photos
            .listPhotos(perPage, 15, "latest")
            .then(toJson)
            .then(json => {
                setPhotos([...photos, ...json])
                setPerpage(perPage + 1)
            })
    }, [photos, perPage])

    useEffect(() => {
        getData()
    }, [])


    const styles = {
        padding: "15px",
    }

    
    const childElements = photos.map(photo => (
        <li className="list-unstyled">
            <Link to={`/${photo.id}`}>
                <img
                    className="rounded"
                    style={styles}
                    src={photo.urls.small}
                    alt={photo.description}
                    onMouseOut={() => {
                        setHovered(true)
                    }}
                    onMouseOver={() => {
                        setHovered(false)  
                    }}
                />
            </Link>
        </li>
    ))

    
    return (
        <div className="container-fluid">
            <div className="row p-4">
                <div className="col-12">
                    <InfiniteScroll dataLength={photos.length}
                        next={getData}
                        hasMore={true}
                        loader={<div className="spinner-border" role="status">
                            <span className="sr-only">Loading...</span>
                        </div>}
                    >
                        <Masonry elementType={"ul"}>
                            {childElements}
                        </Masonry>
                    </InfiniteScroll>
                </div>
            </div>
        </div>
    )
}

export default ListPhotos

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-04-16
@mrrangerr

something like this

const childElements = photos.map(photo => (
        <li className="list-unstyled">
            <Link to={`/${photo.id}`}>
                <img
                    className={`rounded ${ hovered === photo.id ? 'hover-class' : '' }`}
                    style={styles}
                    src={photo.urls.small}
                    alt={photo.description}
                    onMouseOut={() => {
                        setHovered(null)
                    }}
                    onMouseOver={() => {
                        setHovered(photo.id)
                    }}
                />
            </Link>
        </li>
    ))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question