首页 > 其他分享 >简易轮播图

简易轮播图

时间:2023-03-24 19:36:19浏览次数:37  
标签:function index 轮播 height 简易 imgs Banner left

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .banner{width:1000px;height:300px;margin: 20px auto;position: relative;overflow: hidden;}
        /* .imgbox{} */
        .imgbox img{width: 1000px;height:300px;position: absolute;left:0;top:0;overflow: hidden}
        .imgbox img:nth-child(1){z-index: 1}
        /* .imgbox a img{width: 1000px;height: 300px} */

        .btns input{position: absolute;top:130px;width:40px;height:40px;border: none;background: rgba(200,200,200,0.5);z-index: 9999999;}
        #left{left:0}
        #right{right:0}
    </style>
</head>
<body>
    <div class="banner">
        <div class="imgbox">
            <img src="img/1.jpg" alt="">
            <img src="img/2.jpg" alt="">
            <img src="img/3.jpg" alt="">
            <img src="img/4.jpg" alt="">
            <img src="img/5.jpg" alt="">
        </div>
        <div class="btns">
            <input type="button" id="left" value="<<<">
            <input type="button" id="right" value=">>>">
        </div>
    </div>
</body>
<script src="../move.js"></script>
<script>
    function Banner(){
        // 1.选择元素
        this.imgs = document.querySelector(".imgbox").children;
        this.left = document.querySelector("#left");
        this.right = document.querySelector("#right");

        // 自定义的默认显示的图片:索引
        this.index = 0;

        // 为了显示图片设置的层级
        this.i = 2;

        // 2.绑定点击事件
        this.init()
    }
    Banner.prototype.init = function(){
        var that = this;
        // 绑定事件...
        this.right.onclick = function(){
            // 3.计算索引
            that.changeIndex();
        }
        this.left.onclick=function () {
            that.changeIndex2();//为什么不用this?因为这里的this绑定的是点击的元素,如果用this指的是实例

        }
    }

    Banner.prototype.changeIndex = function(){
        // 计算索引
        if(this.index === this.imgs.length-1){
            this.index = 0;
        }else{
            this.index++;
        }
        // 4.根据索引,显示图片
        this.display()
    }
    Banner.prototype.display = function(){
        
        // 显示图片
        this.imgs[this.index].style.zIndex = this.i++;


        this.imgs[this.index].style.width = "0";
        this.imgs[this.index].style.height = "0";
        move(this.imgs[this.index],{
            width:1000,
            height:150
        },function(){
            move(this.imgs[this.index],{height:300})
        }.bind(this))
    }
    Banner.prototype.changeIndex2=function () {
        if(this.index==0){
            this.index=this.imgs.length-1
        }
        else{
            this.index--;//点击的时候索引就加1

        }
        this.display2()

    };
    Banner.prototype.display2=function () {
        this.imgs[this.index].style.zIndex=this.i++;
        this.imgs[this.index].style.left = "2000px";
        move(this.imgs[this.index],{
            left:0
        });
    }
    new Banner();
    function move(ele,json,callback){
        clearInterval(ele.t);
        ele.t = setInterval(() => {
            var onoff = true;
        for(var i in json){
            var iNow = parseInt(getStyle(ele,i));
            var speed = (json[i] - iNow)/6;
            speed = speed<0 ? Math.floor(speed) : Math.ceil(speed);
            if(iNow != json[i]){
                onoff = false;
            }
            ele.style[i] = iNow + speed + "px";
        }
        if(onoff){
            clearInterval(ele.t);
            callback && callback();
        }
    }, 30);
    }
    function getStyle(ele,attr){
        if(ele.currentStyle){
            return ele.currentStyle[attr];
        }else{
            return getComputedStyle(ele,false)[attr];
        }
    }
</script>
</html>

 

长风破浪会有时,直挂云帆济沧海



标签:function,index,轮播,height,简易,imgs,Banner,left
From: https://blog.51cto.com/u_15961676/6147972

相关文章

  • 无缝轮播图1
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><scriptsrc="../move.js"></script><style>.banner{w......
  • 实现一个简易的antd表格拖拽
    <a-table:columns="columns":data-source="dataSource"><templateslot="icon"slot-scope="record"><divstyle="cursor:pointer;":drag......
  • ant design 轮播图左右两边加箭头
    <Carouselautoplayarrows={true}prevArrow={<LeftOutlined/>}nextArrow={<RightOutlined/>}></Carousel>.ant-carousel.slick-prev,.ant-carousel.s......
  • 大爽Python入门教程 7-6 面向对象编程 实例演示 简易方格游戏(二维)
    大爽Python入门公开课教案点击查看教程总目录1介绍将上一节,一维的面板拓展成二维的。面板数据从txt中读取出来玩家从左上进入,输入一串移动命令,展示中间过程玩家......
  • Vue3 + Swiper开发轮播列表组件
    Vue3+Swiper开发轮播列表组件前端开发中,轮播列表的场景并不少见,通常使用Vue+Swiper来实现,上手比较快。安装依赖直接执行npm命令npmiswiper组件编写Html模板......
  • Vue axios简易封装
    1.安装axiosnpminstallaxios-g 2.创建utils文件夹,新建文件request.js对axios进行封装3.设置请求超时通过axios.defaults.timeout设置默认的请求超时时间。例......
  • Java实现简易酒店管理系统
    实现订房退房查房退出四个基本功能酒店有三楼,每楼5个房间,房间号按照101,102,...201,202·有双人间,单人间,地铺间能实现订房退房查房等基本功能在控制台中实现,操作便捷一......
  • CentOS7系统编译安装简易Zabbix6.0
    系统环境设置系统交换分区(本文使用云主机安装)设置交换分区,现在很多云主机,默认都是关闭交换分区,关于交换分区的优缺点,可以自百度,这一步非必须,可以跳过。1、设置交换......
  • JS+CSS 轮播通知效果
    使用JS+CSS实现一个简单的轮播通知效果图html<div><ulid="notification"><li>通知1</li><li>通知2</li><li>通知3</li><li>通知4</li......
  • uni-app 实现轮播图组件外轮廓背景色随图片主题色改变
    uni-app实现轮播图组件外轮廓背景色随图片主题色改变<template><viewclass="container"><canvascanvas-id="getThemeColorCanvas"id="getThemeCo......