day35-Image Carousel(图片轮播图简易版)

news/2024/7/6 13:02:01

50 天学习 50 个项目 - HTMLCSS and JavaScript

day35-Image Carousel(图片轮播图简易版)

效果

在这里插入图片描述

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Image Carousel</title>
    <link rel="stylesheet" href="style.css" />
</head>

<body>
    <!-- 轮播图 -->
    <div class="carousel">
        <!-- 图片容器 -->
        <div class="image-container" id="imgs">
            <img src="https://images.unsplash.com/photo-1599394022918-6c2776530abb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1458&q=80"
                alt="first-image" />
            <img src="https://images.unsplash.com/photo-1593642632559-0c6d3fc62b89?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80"
                alt="second-image" />
            <img src="https://images.unsplash.com/photo-1599423300746-b62533397364?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80"
                alt="third-image" />
            <img src="https://images.unsplash.com/photo-1599561046251-bfb9465b4c44?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1492&q=80"
                alt="fourth-image" />
        </div>
        <!-- 按钮-->
        <button id="left" class="btn">&lt;</button>
        <button id="right" class="btn">&gt;</button>
    </div>

    <script src="script.js"></script>
</body>

</html>

style.css

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');

* {
    box-sizing: border-box;
}

body {
    /* background: url('https://source.unsplash.com/random/1000x600') no-repeat center/100% 100%; */
    font-family: 'Roboto', sans-serif;
    /* 居中 */
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
}

img {
    width: 500px;
    height: 500px;
    /* 元素的内容会被等比例缩放,以便完全覆盖容器的尺寸。这意味着图像或视频的某些部分可能会被裁剪,以使内容填充整个容器,并保持其纵横比。 */
    object-fit: cover;
}

/* 轮播图 */
.carousel {
    box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
    height: 500px;
    width: 500px;
    /* 超出部分隐藏 */
    overflow: hidden;
    position: relative;
}

/* 图片容器 */
.image-container {
    display: flex;
    transform: translateX(0);
    transition: transform 0.5s ease-in-out;
}

/* 按钮 */
.btn {
    background-color: rgba(0, 0, 0, 0.3);
    color: black;
    border: none;
    cursor: pointer;
    width: 2rem;
    outline: none;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    padding: 0.5rem 0;
}
#left{
    padding-right: 0.5rem;
    border-radius: 0 50% 50% 0;
    left: 0;
}
#right {
    padding-left: 0.5rem;
    border-radius: 50% 0 0 50%;
    right: 0;
}
.btn:hover {
    opacity: 0.9;
}

script.js

// 重点 transform transition
// 1.获取元素节点
const imgs = document.getElementById('imgs')//图片容器
const leftBtn = document.getElementById('left')//上一张按钮
const rightBtn = document.getElementById('right')//下一张按钮

const img = document.querySelectorAll('#imgs img')//所有图片
// 当前图片索引
let idx = 0
// 间隔器
let interval = setInterval(run, 2000)
// 滑动
function run() {
    idx++
    // 改变图片
    changeImage()
}

// 改变图片
function changeImage() {
    if (idx > img.length - 1) {
        idx = 0
    } else if (idx < 0) {
        idx = img.length - 1
    }
    imgs.style.transform = `translateX(${-idx * imgs.offsetWidth}px)`
    imgs.style.transition = `transform 0.5s ease-in-out`
}
// 重置
function resetInterval() {
    clearInterval(interval)
    interval = setInterval(run, 2000)
}
// 下一张
rightBtn.addEventListener('click', () => {
    idx++
    changeImage()
    resetInterval()
})
// 上一张
leftBtn.addEventListener('click', () => {
    idx--
    changeImage()
    resetInterval()
})

http://lihuaxi.xjx100.cn/news/1384682.html

相关文章

Goby 漏洞发布|Metabase JDBC 远程代码执行漏洞(CVE-2023-38646)

漏洞名称&#xff1a;Metabase JDBC 远程代码执行漏洞&#xff08;CVE-2023-38646&#xff09; English Name&#xff1a;Metabase JDBC Remote Code Execution Vulnerability (CVE-2023-38646) CVSS core: 9.8 影响资产数&#xff1a;66604 漏洞描述&#xff1a; Metabas…

记录浙政钉的消息通知的一次开发实战记录

先忍不住吐槽下钉钉的开发文档&#xff0c;实在是不敢恭维&#xff0c;首先每个术语描述都是不统一的&#xff0c;比如有些地方写“”群聊“”&#xff0c;有些地方写“会话”&#xff0c;有些地方写“钉消息”&#xff0c;总之他们自己想怎么写&#xff0c;怎么写&#xff0c;…

Vue+element Ui的el-select同时获取value和label的方法总结

1.通过ref的形式&#xff08;推荐) <template><div class"root"><el-selectref"optionRef"change"handleChange"v-model"value"placeholder"请选择"style"width: 250px"><el-optionv-for&q…

【Ubuntu18.04安装FileZilla】

Ubuntu18.04安装FileZilla 1 FileZilla简介2 安装方式3 使用方式3.1 连接FTP服务器3.1.1 快速连接3.1.2 通过站点管理器 1 FileZilla简介 FileZilla是自由开源、快速、可信赖的FTP客户端以及服务器端应用&#xff0c;具有多种特色、直观的接口。 特点&#xff1a;可控性、有条…

【Docker】Docker安全性与安全实践(五)

前言&#xff1a; Docker安全性的作用和意义在于确保容器化应用程序和镜像的隔离性、保护数据和系统资源、防止恶意攻击&#xff0c;以及提高应用的整体安全性。 文章目录 1. Docker安全性1.1 隔离性1.2 镜像安全1.3 特权访问1.4 数据保护 2. Docker安全实践2.1 使用官方镜像或…

素描基础知识

素描基础入门 1.基础线条 1.1 握笔姿势及长线条 2.排线 2.1 不同姿势画排线 2.1.1 姿势画排线 2.1.2 用手腕画排线 2.1.3 小拇指画排线 2.1.4 叠加排线 2.1.5交叉排线 2.2 纸张擦法 2.3 排线学习榜样 2.4 四种常见的排线 3、定向连线 4、一点透视 4.1 透视的规律 4.2 焦点透视…

文创产品火出圈,熊猫伴伴受到大家喜爱

在各种IP火遍网络的同时&#xff0c;其衍生出来的各种文创产品也凭借着自身的特点火到出圈&#xff0c;不仅爆红于网络上&#xff0c;就连线下的各种商店也有着其具象化的商品售卖。例如最近深受年轻人喜爱的熊猫伴伴&#xff0c;就凭借着其可爱的外表&#xff0c;受到了很多人…

抖音seo短视频矩阵系统源代码开发技术分享

抖音SEO短视频矩阵系统是一种通过优化技术&#xff0c;提高在抖音平台上视频的排名和曝光率的系统。以下是开发该系统的技术分享&#xff1a; 熟悉抖音平台的算法 抖音平台的算法是通过分析用户的兴趣爱好和行为习惯&#xff0c;对视频进行排序和推荐。因此&#xff0c;开发人员…