图片循环轮播,悬停停止

时间:2024-11-09 19:06:02

 可直接执行HTML 代码     template 代码在下面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Swiper Animation</title>
    <style>
        @keyframes swiper {
            to {
                transform: translateX(-50%);
            }
        }

        .swiperContent {
            width: 80%;
            height: 100vh;
            display: flex;
            overflow: hidden;
            margin: auto;
            mask-image: linear-gradient(90deg, transparent, black 10%, black 90%, transparent);
        }

        .picImg {
            margin: auto;
            width: max-content;
            display: flex;
            align-items: center;
            animation: swiper 10s linear infinite;

            /* Stops animation on hover */
            &:hover {
                animation-play-state: paused;
            }
        }

        .item {
            width: 260px;
            height: 150px;
            background-color: skyblue;
            border-radius: 10px;
            margin-right: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
            font-size: 20px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="swiperContent">
        <div class="picImg">
            <div class="item">Item 1</div>
            <div class="item">Item 2</div>
            <div class="item">Item 3</div>
            <div class="item">Item 4</div>
            <div class="item">Item 5</div>
            <div class="item">Item 6</div>
            <div class="item">Item 7</div>
            <div class="item">Item 8</div>
            <div class="item">Item 1</div>
            <div class="item">Item 2</div>
            <div class="item">Item 3</div>
            <div class="item">Item 4</div>
            <div class="item">Item 5</div>
            <div class="item">Item 6</div>
            <div class="item">Item 7</div>
            <div class="item">Item 8</div>
        </div>
    </div>
</body>
</html>

<template> 

<template>
    <div class="swiperContent">
        <div class="picImg">
            <div class="item">Item 1</div>
            <div class="item">Item 2</div>
            <div class="item">Item 3</div>
            <div class="item">Item 4</div>
            <div class="item">Item 5</div>
            <div class="item">Item 6</div>
            <div class="item">Item 7</div>
            <div class="item">Item 8</div>
            <div class="item">Item 1</div>
            <div class="item">Item 2</div>
            <div class="item">Item 3</div>
            <div class="item">Item 4</div>
            <div class="item">Item 5</div>
            <div class="item">Item 6</div>
            <div class="item">Item 7</div>
            <div class="item">Item 8</div>
        </div>
    </div>
</template>

<style scoped lang="scss">
@keyframes swiper {
    to {
        transform: translateX(-50%);
    }
}

.swiperContent {
    width: 80%;
    height: 100vh;
    display: flex;
    overflow: hidden;
    margin: auto;
    mask: linear-gradient(90deg,
            transparent,
            black 10%,
            black 90%,
            transparent);

    .picImg {
        margin: auto;
        width: max-content;
        display: flex;
        align-items: center;
        animation: swiper 10s linear infinite;

        .item {
            width: 260px;
            height: 150px;
            background-color: skyblue;
            border-radius: 10px;
            margin-right: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
            font-size: 20px;
            font-weight: bold;
        }

        &:hover {
            animation-play-state: paused;
        }
    }
}
</style>