首页 > 其他分享 >点击左右箭头,中间内容切换

点击左右箭头,中间内容切换

时间:2024-10-23 17:47:41浏览次数:1  
标签:indicator const 箭头 点击 切换 carousel 10px border currentIndex

<div class="carousel-container">  
        <div class="carousel">  
            <div class="carousel-item active">Item 1</div>  
            <div class="carousel-item">Item 2</div>  
            <div class="carousel-item">Item 3</div>  
        </div>  
        <button class="carousel-control prev">Prev</button>  
        <button class="carousel-control next">Next</button>  
        <div class="carousel-indicators">  
            <span class="indicator active" data-index="0"></span>  
            <span class="indicator" data-index="1"></span>  
            <span class="indicator" data-index="2"></span>  
        </div>  
</div>  
body {  
    font-family: Arial, sans-serif;  
    display: flex;  
    justify-content: center;  
    align-items: center;  
    height: 100vh;  
    margin: 0;  
    background-color: #f0f0f0;  
}  
  
.carousel-container {  
    position: relative;  
    width: 80%;  
    max-width: 600px;  
    overflow: hidden;  
    border: 2px solid #ccc;  
    border-radius: 10px;  
    background-color: #fff;  
}  
  
.carousel {  
    display: flex;  
    transition: transform 0.5s ease-in-out;  
}  
  
.carousel-item {  
    min-width: 100%;  
    box-sizing: border-box;  
    text-align: center;  
    padding: 20px;  
    border-right: 1px solid #ccc;  
}  
  
.carousel-item:last-child {  
    border-right: none;  
}  
  
.carousel-control {  
    position: absolute;  
    top: 50%;  
    transform: translateY(-50%);  
    background-color: rgba(0, 0, 0, 0.5);  
    color: #fff;  
    border: none;  
    padding: 10px;  
    cursor: pointer;  
    z-index: 10;  
}  
  
.carousel-control.prev {  
    left: 10px;  
}  
  
.carousel-control.next {  
    right: 10px;  
}  
  
.carousel-indicators {  
    display: flex;  
    justify-content: center;  
    margin-top: 10px;  
}  
  
.indicator {  
    width: 10px;  
    height: 10px;  
    background-color: #ccc;  
    border-radius: 50%;  
    margin: 0 5px;  
    cursor: pointer;  
}  
  
.indicator.active {  
    background-color: #333;  
}
document.addEventListener('DOMContentLoaded', () => {  
    const carousel = document.querySelector('.carousel');  
    const items = document.querySelectorAll('.carousel-item');  
    const indicators = document.querySelectorAll('.indicator');  
    const prevButton = document.querySelector('.carousel-control.prev');  
    const nextButton = document.querySelector('.carousel-control.next');  
    let currentIndex = 0;  
  
    function updateCarousel() {  
        items.forEach((item, index) => {  
            item.classList.toggle('active', index === currentIndex);  
        });  
  
        indicators.forEach((indicator, index) => {  
            indicator.classList.toggle('active', index === currentIndex);  
        });  
  
        carousel.style.transform = `translateX(${-currentIndex * 100}%)`;  
    }  
  
    function showNextItem() {  
        currentIndex = (currentIndex + 1) % items.length;  
        updateCarousel();  
    }  
  
    function showPrevItem() {  
        currentIndex = (currentIndex - 1 + items.length) % items.length;  
        updateCarousel();  
    }  
  
    prevButton.addEventListener('click', () => {  
        showPrevItem();  
    });  
  
    nextButton.addEventListener('click', () => {   
        showNextItem();  
    });  
  
    indicators.forEach(indicator => {  
        indicator.addEventListener('click', (event) => {  
            const targetIndex = parseInt(event.target.getAttribute('data-index'));  
            currentIndex = targetIndex;  
            updateCarousel();   
        });  
    });  
});

 

标签:indicator,const,箭头,点击,切换,carousel,10px,border,currentIndex
From: https://www.cnblogs.com/srqsl/p/18497947

相关文章

  • 点击触发事件
    <%--CreatedbyIntelliJIDEA.User:26945Date:2024/10/23Time:16:57TochangethistemplateuseFile|Settings|FileTemplates.--%><%@pagecontentType="text/html;charset=UTF-8"language="java"%><html......
  • 产品有了模式切换功能,众口不再难调!
    在科技飞速发展的今天,我们的产品迎来了重大突破——模式切换功能。这一创新之举,让众口不再难调。无论你是追求高效便捷的商务人士,还是喜欢沉浸式体验的娱乐爱好者,亦或是注重简约实用的日常用户,都能在不同模式中找到最适合自己的状态。轻松切换,满足你的各种需求。从此,不再为......
  • 小结---安装nvm解决node版本不兼容的问题(node版本切换)
    1、卸载node(如果电脑上没有安装node略过即可)在控制面板找到node.js卸载并将c盘的node文件等全部删除最后打开高级设置,找到高级系统设置将配置的node字段全部删除 2、nvm安装官网下载nvm包https://github.com/coreybutler/nvm-windows/releases2.安装n......
  • React实现画布——可绘制矩形和箭头
    目录思路代码效果本文将使用React、JSX、Rough.js实现一个简单的画布,可以绘制矩形和箭头。思路每一个图形包括:绘制的类型、起点的x坐标、起点的y坐标、宽、高。调用rough的generator()函数传入图形信息进行绘制,其中对于箭头需要进一步处理:根据宽高确定终点,并且定义角度等生......
  • Vue拍照上传组件(重拍、切换已有摄像头)
    背景由于业务需求,需要进行拍照上传,百度了一遍组件都不太合适。自己结合已有案例封装了一下,可以把这个组件嵌套到el-dialog里面就可以使用。实现功能实时加载预览画面点击拍照截取照片不满意可以重拍,不会中断之前的视频流加载当前设备的所有摄像头,可以进行选择切换依赖E......
  • EAS_WEB如何查找点击前台按钮后,调用的后台方法,
    第一种方法:正常有说明的可以直接从后台实现找到第二种方法,找不到的,类似如下,我们可以通过debugger的方式,找到对应的实现,具体路径org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#registerHandlerMethod,没有registerHandlerMethod方法的话,可......
  • Unity 切换UI坐标和世界坐标
    usingUnityEngine;//这个脚本实现了,本脚本所在的游戏物体能够被拖拽publicclassDragObjectT:MonoBehaviour{privateVector3screenPoint;//存储物体在屏幕上的位置privateVector3offset;//存储鼠标点击位置与物体实际位置的偏移量privatebool......
  • Unity 切换鼠标光标图标
    在Unity中,可以通过检测鼠标左键的按下和弹起事件来切换鼠标光标。这可以通过在Update方法中检查Input.GetMouseButtonDown(0)和Input.GetMouseButtonUp(0)来实现。以下是一个示例代码,展示如何在左键按下时切换到一个自定义光标,在左键弹起时恢复到另一个光标或默认光标:示......
  • PbootCMS系统管理员点击文章评论的状态按钮提示权限不足
    1.开启后台菜单登录后台:打开浏览器,输入你的PbootCMS后台地址,登录后台管理系统。进入系统设置:在后台管理界面,进入“系统设置”->“菜单管理”。开启后台菜单:如果你还没有开启后台菜单,可以参考这篇教程:如何开启PbootCMS后台菜单。2.修改会员中心的文章评论......
  • pbootcms程序做的网站 点击页面上的所有链接打开都是首页是什么原因?
    如果您的PBootCMS网站突然出现所有链接都无法正常跳转,点击任何链接都打开的是网站首页,这可能是由多种原因引起的。虽然“网站被黑”是一个可能的原因,但也有可能是其他技术问题导致的。以下是一些常见的排查步骤和解决方案:1.检查伪静态规则Apache服务器检查 .htaccess 文......