js轮播图

                                   

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>轮播</title>
    <style>
        .container{width: 600px;position: absolute;height: 400px;margin: auto;top:0;left: 0;bottom: 0;right: 0;overflow: hidden}
        .list{width: 4200px;height: 400px;position: absolute;}
        .list img { float: left;}
        #buttons { position: absolute; height: 12px; width: 80px; z-index: 2; bottom: 20px; left: 262px;}
        #buttons span { cursor: pointer; float: left; border: 1px solid #fff; width: 10px; height: 10px; border-radius: 50%; background: #333; margin-right: 4px;}
        #buttons .on {  background: orangered;}
        .arrow{cursor:pointer;width: 40px;height: 40px;display:none;background-color: rgba(255,2255,255,0.1);z-index: 2;line-height: 35px;top:180px;text-align: center;position: absolute;color: #eee;font-weight: bold;font-size: 36px}
        .arrow:hover{background-color: rgba(255,2255,255,0.3)}
        .container:hover .arrow{display: block}
        #pre{left: 20px}
        #next{right: 20px}
    </style>
</head>
<body>
    <div class="container">
        <div class="list" style="left: -600px">
            <img src="https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/8124d690-8fb0-11e8-920d-44032ca554a5.jpeg" alt="">
            <img src="https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/4de3a402-8fb0-11e8-ae84-44032ca554a5.jpeg" alt="">
            <img src="https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/5d17e394-8fb0-11e8-8d7e-44032ca554a5.jpeg" alt="">
            <img src="https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/69c9519a-8fb0-11e8-9fb0-44032ca554a5.jpeg" alt="">
            <img src="https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/779910c0-8fb0-11e8-a3f4-44032ca554a5.jpeg" alt="">
            <img src="https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/8124d690-8fb0-11e8-920d-44032ca554a5.jpeg" alt="">
            <img src="https://sxzimgs.oss-cn-shanghai.aliyuncs.com/yingxiao/page/4de3a402-8fb0-11e8-ae84-44032ca554a5.jpeg" alt="">
        </div>
        <div id="buttons">
            <span class="on"></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
        <div href="javascript:;" id="pre" class="arrow"><</div>
        <div href="javascript:;" id="next" class="arrow">></div>
    </div>
    <script>
        window.onload=function () {
            var container=document.getElementsByClassName("container")[0];
            var pre=document.getElementById("pre");
            var next=document.getElementById("next");
            var list=document.getElementsByClassName("list")[0];
            var buttons=document.getElementById("buttons").getElementsByTagName("span")
            var index=1;
            var animated=false;
            var timer;

            function autoPlay() {                            //自动轮播
                timer=setInterval(function () {
                    next.onclick();
                },3000)
            }
            autoPlay()
            function stop() {           //结束自动轮播
                clearInterval(timer)
            }
            container.onmouseover=function () {
                stop();
            }
            function showButton() {                 //改变小圆点颜色
                for(var i=0;i<buttons.length;i++) {
                    buttons[i].className = ""
                }
                buttons[index-1].className='on'
            }
            function animate(offset) {   //图片跳转动作
                var newLeft=parseInt(list.style.left) + offset
                var time=300
                var interval=10
                var speed=offset/(time/interval)
                function go() {
                    if((speed<0 && parseInt(list.style.left)>newLeft)||(speed>0 && parseInt(list.style.left)<newLeft)){
                        animated=true
                        list.style.left=parseInt(list.style.left)+speed+'px'
                        setTimeout(go,interval)
                    }else{
                        animated=false
                        list.style.left=newLeft + 'px'
                        if(newLeft > -600){
                            list.style.left=-3000 + 'px'
                        }
                        if(newLeft < -3000){
                            list.style.left=-600 + 'px'
                        }
                    }
                }
                go();
            }
            pre.onclick=function () {       //左箭头点击
                if(!animated){
                    animate(600);
                }
                if(index==1){
                    index=5
                }else{
                    index-=1
                }
                showButton()
            }
            next.onclick=function () {             //右箭头点击
                if(!animated){
                    animate(-600);
                }
                if(index==5){
                    index=1
                }else{
                    index+=1;
                }
                showButton()
            }
            for (var i=0;i<buttons.length;i++){      //小圆点点击
                buttons[i].index=i
                buttons[i].onclick=function () {
                    if(this.className=='on'){
                        return
                    }
                    var distance=(this.index+1-index)*(-600)
                    if(!animated){
                        animate(distance);
                    }
                    index=this.index+1
                    showButton()
                }
            }
        }
    </script>
</body>
</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript轮播图可以通过以下步骤来实现: 1. 创建一个HTML结构,包含轮播图容器和轮播图项。 2. 使用CSS样式来设置轮播图容器和轮播图项的样式,如宽度、高度、位置、层级等。 3. 在JavaScript中获取轮播图容器和轮播图项,并根据需要设置初始状态。 4. 使用定时器或事件监听器来控制轮播图的切换,如定时切换、按钮点击切换、鼠标悬停切换等。 5. 切换时需要设置轮播图项的样式,如透明度、位置、过渡效果等。 6. 可以添加一些特效或功能,如自动播放、无限循环、指示器、缩略图等。 以下是一个简单的JavaScript轮播图示例: ```html <div class="carousel"> <div class="carousel-item active"><img src="1.jpg"></div> <div class="carousel-item"><img src="2.jpg"></div> <div class="carousel-item"><img src="3.jpg"></div> <div class="carousel-item"><img src="4.jpg"></div> </div> ``` ```css .carousel { position: relative; width: 600px; height: 400px; overflow: hidden; } .carousel-item { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 0.5s ease-in-out; } .carousel-item.active { opacity: 1; } ``` ```javascript var carousel = document.querySelector('.carousel'); var items = carousel.querySelectorAll('.carousel-item'); var index = 0; var timer = null; function showItem(index) { for (var i = 0; i < items.length; i++) { items[i].classList.remove('active'); } items[index].classList.add('active'); } function nextItem() { index++; if (index >= items.length) { index = 0; } showItem(index); } timer = setInterval(nextItem, 3000); carousel.addEventListener('mouseover', function() { clearInterval(timer); }); carousel.addEventListener('mouseout', function() { timer = setInterval(nextItem, 3000); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值