TWEEN动画、JQUERY、ES6 — 2、轮播图-渐隐渐现版本

2023-05-16

分析

目录

编译并压缩less(需要先npm安装less)

banner.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <!--development-->
    <!--<link rel="stylesheet/less" href="css/banner.less">
    <script src="js/less-2.5.3.min.js"></script>-->

    <!--production-->
    <link rel="stylesheet" href="css/banner.min.css">
</head>
<body>
<div class="container" id="container">
    <!--WRAPPER:控制轮播图切换的区域-->
    <ul class="wrapper">
        <!--<li class="slide">
            <img src="" data-img="img/banner1.jpg" alt="">
        </li>-->
    </ul>
    <!--FOCUS:控制焦点-->
    <ul class="focusBox clearfix">
        <!--<li class="select"></li>
        <li></li>
        <li></li>
        <li class="last"></li>-->
    </ul>
    <!--ARROW:控制切换的箭头-->
    <a href="javascript:;" class="arrow arrowLeft"></a>
    <a href="javascript:;" class="arrow arrowRight"></a>
</div>

<!--IMPORT JS-->
<script src="js/utils.min.js"></script>
<script src="js/animate.min.js"></script>
<script src="js/banner.js"></script>
</body>
</html>
复制代码

banner.less

@import "reset";

/*--BANNER--*/
.container {
  position: relative;
  margin: 20px auto;
  width: 1000px;
  height: 300px;
  overflow: hidden;
  text-align: center;

  .wrapper {
    position: relative;
    height: 300px;
    background: url("../img/default.gif") no-repeat center center #EEE;

    .slide {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;

      //=>init style
      opacity: 0;
      filter: alpha(opacity=0);
      z-index: 0;

      img {
        display: none;
        width: 100%;
        height: 100%;
      }
    }
  }

  .focusBox {
    position: relative;
    top: -40px;
    z-index: 999;
    display: inline-block;
    padding: 4px 8px;
    height: 12px;
    background: rgba(0, 0, 0, .2);
    border-radius: 12px;

    li {
      float: left;
      margin-right: 10px;
      width: 12px;
      height: 12px;
      border-radius: 50%;
      background: #FFF;
      cursor: pointer;

      &.last {
        margin-right: 0;
      }

      &.select {
        background: #DB192A;
      }
    }
  }

  .arrow {
    display: none;
    position: absolute;
    top: 50%;
    z-index: 999;
    margin-top: -30px;
    width: 30px;
    height: 60px;
    background: url("../img/pre.png") no-repeat;
    opacity: 0.3;
    filter: alpha(opacity=30);

    &:hover {
      opacity: 1;
      filter: alpha(opacity=100);
    }

    &.arrowLeft {
      left: 0;
      background-position: 0 0;
    }

    &.arrowRight {
      right: 0;
      background-position: -48px 0;
    }
  }
}
复制代码

reset.less

body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, button, input, textarea, th, td {
  margin: 0;
  padding: 0
}

body {
  font-size: 12px;
  font-style: normal;
  font-family: "\5FAE\8F6F\96C5\9ED1", Helvetica, sans-serif
}

small {
  font-size: 12px
}

h1 {
  font-size: 18px
}

h2 {
  font-size: 16px
}

h3 {
  font-size: 14px
}

h4, h5, h6 {
  font-size: 100%
}

ul, ol {
  list-style: none
}

a {
  text-decoration: none;
  background-color: transparent
}

a:hover, a:active {
  outline-width: 0;
  text-decoration: none
}

table {
  border-collapse: collapse;
  border-spacing: 0
}

hr {
  border: 0;
  height: 1px
}

img {
  border-style: none
}

img:not([src]) {
  display: none
}

svg:not(:root) {
  overflow: hidden
}

html {
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%
}

input, textarea, button, a {
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0)
}

article, aside, details, figcaption, figure, footer, header, main, menu, nav, section, summary {
  display: block
}

audio, canvas, progress, video {
  display: inline-block
}

audio:not([controls]), video:not([controls]) {
  display: none;
  height: 0
}

progress {
  vertical-align: baseline
}

mark {
  background-color: #ff0;
  color: #000
}

sub, sup {
  position: relative;
  font-size: 75%;
  line-height: 0;
  vertical-align: baseline
}

sub {
  bottom: -0.25em
}

sup {
  top: -0.5em
}

button, input, select, textarea {
  font-size: 100%;
  outline: 0
}

button, input {
  overflow: visible
}

button, select {
  text-transform: none
}

textarea {
  overflow: auto
}

button, html [type="button"], [type="reset"], [type="submit"] {
  -webkit-appearance: button
}

button::-moz-focus-inner, [type="button"]::-moz-focus-inner, [type="reset"]::-moz-focus-inner, [type="submit"]::-moz-focus-inner {
  border-style: none;
  padding: 0
}

button:-moz-focusring, [type="button"]:-moz-focusring, [type="reset"]:-moz-focusring, [type="submit"]:-moz-focusring {
  outline: 1px dotted ButtonText
}

[type="checkbox"], [type="radio"] {
  box-sizing: border-box;
  padding: 0
}

[type="number"]::-webkit-inner-spin-button, [type="number"]::-webkit-outer-spin-button {
  height: auto
}

[type="search"] {
  -webkit-appearance: textfield;
  outline-offset: -2px
}

[type="search"]::-webkit-search-cancel-button, [type="search"]::-webkit-search-decoration {
  -webkit-appearance: none
}

::-webkit-input-placeholder {
  color: inherit;
  opacity: .54
}

::-webkit-file-upload-button {
  -webkit-appearance: button;
  font: inherit
}

.clearfix:after {
  display: block;
  height: 0;
  content: "";
  clear: both
}
复制代码

banner.json

[
  {
    "id": 1,
    "img": "img/banner1.jpg",
    "desc": "海,你好",
    "link": "http://www.baidu.cn/"
  },
  {
    "id": 2,
    "img": "img/banner2.jpg",
    "desc": "梦想起飞",
    "link": "http://www.baidu.cn/"
  },
  {
    "id": 3,
    "img": "img/banner3.jpg",
    "desc": "把握未来,掌握先机",
    "link": "http://www.baidu.cn/"
  },
  {
    "id": 4,
    "img": "img/banner4.jpg",
    "desc": "好好学习",
    "link": "http://www.baidu.cn/"
  }
]
复制代码

banner.js

// var bannerRender = (function () {


//     return {
//         init: function () {

//         }
//     }
// })();

// bannerRender.init();





var bannerRender = (function () {
    var container = document.getElementById('container'),
        wrapper = utils.getElementsByClassName('wrapper', container)[0],
        focusBox = utils.getElementsByClassName('focusBox', container)[0],
        arrow = utils.children(container, 'a'),
        arrowLeft = arrow[0],
        arrowRight = arrow[1];
    var bannerData = null,
        wrapperList = null,
        focusList = null,
        wrapperImgList = null;

    //=>1、ajax获取数据
    function queryData() {
        var xhr = new XMLHttpRequest();
        xhr.open('get', 'json/banner.json', false);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                bannerData = utils.toJSON(xhr.responseText);
            }
        };
        xhr.send(null);
    }

    //=>2、绑定数据
    function bindData() {
        if (!bannerData) return;
        var str = ``,
            strFocus = ``;
        for (var i = 0; i < bannerData.length; i++) {
            var item = bannerData[i];
            str += `<li class="slide">
                <img src="" data-img="${item.img}" alt="">
            </li>`;

            strFocus += `<li class="${i === bannerData.length - 1 ? 'last' : ''}"></li>`;
        }
        wrapper.innerHTML = str;
        focusBox.innerHTML = strFocus;

        //=>5、数据绑定完需要获取所有的LI以及IMG,后面会用到
        wrapperList = wrapper.getElementsByTagName('li');
        focusList = focusBox.getElementsByTagName('li');
        wrapperImgList = wrapper.getElementsByTagName('img');
    }

    //=>6、设置默认展示
    function initDefault(index) {
        index = index || 0;
        utils.css(wrapperList[index], {
            opacity: 1,
            zIndex: 1
        });
        focusList[index].className += ' select';//=>一定是加等于(因为部分LI有自己的原有样式)
    }

    //=>8、图片延迟加载
    function computed() {
        if (!wrapperImgList) return;
        for (var i = 0; i < wrapperImgList.length; i++) {
            var curImg = wrapperImgList[i];
            if (curImg.isLoad) continue;
            lazyImg(curImg);
        }
    }

    function lazyImg(curImg) {
        curImg.isLoad = true;
        var tempImg = new Image;//创建一个临时
        tempImg.onload = function () {
            curImg.src = this.src;
            curImg.style.display = 'block';
            tempImg = null;
        };
        tempImg.src = curImg.getAttribute('data-img');
    }

    //=>10、实现轮播图的自动切换
    var step = 0,//->记录当前展示的是第几个SLIDE(索引)
        prevStep = 0,//->记录上一个展示的SLIDE索引
        autoInterval = 2000,//->自动切换的时间因子
        autoTimer = null,//->自动切换的定时器
        count = 0;//->总共的SLIDE数量

    
    function autoMove() {
        step++;
        if (step === count) {
            step = 0;
        }
        change();
    }

    //=>轮播图公共切换方法
    function change() {
        if (step === prevStep) return;
        var curSlide = wrapperList[step],
            preSlide = wrapperList[prevStep];

        //->当前展示的SLIDE层级变为1 & 上一个展示的SLIDE层级变为0
        utils.css(curSlide, 'zIndex', 1);
        utils.css(preSlide, 'zIndex', 0);

        //->让当前展示的SLIDE透明度从0~1(动画)
        animate({
            curEle: curSlide,
            target: {opacity: 1},
            duration: 200,
            callBack: function () {
                //=>上一个SLIDE隐藏(透明度为0)
                utils.css(preSlide, 'opacity', 0);
            }
        });

        //->当前展示的这一个SLIDE就是下一次切换的上一次SLIDE
        prevStep = step;

        //=>14、切换的时候要执行selectFocus让其焦点对齐
        selectFocus();
    }

    //=>13、焦点对齐的方法(这里用的是循环所有的焦点给当前索引加样式);还有种方式:可以用清除上一个焦点样式给当前焦点加样式
    function selectFocus() {
        for (var i = 0; i < focusList.length; i++) {
            var item = focusList[i];
            if (i === focusList.length - 1) {//最后一个焦点有last做特殊处理
                item.className = i === step ? 'last select' : 'last';
                continue;
            }
            item.className = i === step ? 'select' : '';
        }
    }

    //=>15、鼠标划入划出BANNER区域控制自动切换的暂停和开始 用onmouseout和onmouseover有问题,后面会写
    function bindMouseEvent() {
        container.onmouseenter = function () {
            clearInterval(autoTimer);
            arrowLeft.style.display = arrowRight.style.display = 'block';//鼠标滑进来左右箭头显示
        };
        container.onmouseleave = function () {
            autoTimer = setInterval(autoMove, autoInterval);
            arrowLeft.style.display = arrowRight.style.display = 'none';//鼠标离开左右箭头隐藏
        };
    }

    //=>16、滑过焦点实现切换
    function bindFocus() {
        for (var i = 0; i < focusList.length; i++) {
            focusList[i].myIndex = i;
            focusList[i].onmouseenter = function () {
                step = this.myIndex;
                change();
            }
        }
    }

    //=>17、点击左右按钮切换
    function bindArrow() {
        arrowLeft.onclick = function () {
            step--;
            if (step < 0) {
                step = count - 1;
            }
            change();
        };
        arrowRight.onclick = autoMove;
    }

    return {
        init: function () {
            // 9、执行computed延迟加载
            // setTimeout(computed,500);//可以看延迟加载效果
            window.onload = computed;//=>当页面加载完成后做图片的延迟加载(放在上面也无所谓,因为事件绑定是异步编程:写在上面,也是需要等下面的代码都加载完成,页面才加载完成,才会触发LOAD事件)
            // 3、执行queryData()
            queryData();
            // 4、执行bindData()
            bindData();
            // 7、执行
            initDefault(step);

            //=>实现轮播图的切换
            count = bannerData.length;//11、获取总共的SLIDE数量
            autoTimer = setInterval(autoMove, autoInterval);//12、执行autoMove

            //=>18、其它切换方式
            bindMouseEvent();
            bindFocus();
            bindArrow();
        }
    }
})();
bannerRender.init();
复制代码

其余文件同之前封装的库(动画库animate和dom库utils)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TWEEN动画、JQUERY、ES6 — 2、轮播图-渐隐渐现版本 的相关文章

  • Jquery,取消绑定鼠标滚轮事件,然后在操作完成后重新绑定它?

    我已经为此苦苦挣扎了一段时间了 我正在使用此代码来监视鼠标滚轮 以便它可以用于使用我拥有的滑块滚动 但是 它有一个问题 即操作排队 因此如果你快速滚动鼠标滚轮 就像任何人通常会做的那样 它们就会堆积并导致错误行为 我知道如何用动画处理此类问
  • 限制可选 DOM 复选框

    我试图限制用户可以选择的复选框数量 这些复选框是为数组中的每个项目生成的 DOM 输入对象 我目前对此没有运气 因此非常感谢任何帮助 谢谢 在这里小提琴 http jsfiddle net vVxM2 222 http jsfiddle n
  • 如何向 jquery-file-upload basic-plugin 添加取消上传按钮

    我正在使用 jquery file upload 和 Rails 4 我从https github com tors jquery fileupload rails paperclip example https github com to
  • 在 jQuery 可排序中对多个选定项目进行排序?

    我试图在 jQuery 可排序集中选择多个项目 然后将选定的项目一起移动 这是我的弱点开始尝试使其发挥作用 http jsfiddle net benstenson CgD8Y 这是代码 HTML div class container d
  • 如何调用 google 的 getBasicProfile() 来仅单击按钮即可登录 google?

    我在我的网站上使用谷歌登录
  • 获取点击的的DOM路径

    HTML div class lol a class rightArrow href a div 伪代码 rightArrow click function rightArrowParents this dom dom is the pse
  • jQuery - 系列之外的 HighCharts 标签(条形图)

    function container highcharts chart type bar backgroundColor null width 360 title text null style display none subtitle
  • Jquery 移动应用程序的奇怪行为

    我创建了一个应用程序 其中包含多个主页按钮 单击其中一个按钮 我的应用程序将重定向到某个视图 其中包含 JQM 表单 JQM 日历 文本字段 按钮和数据库等 我的问题是 当我在 Android 设备上测试我的应用程序时 即使我没有使用任何图
  • 检查外部 JS 库是否已加载[重复]

    这个问题在这里已经有答案了 我当前的设置是用户单击链接来动态加载内容 其中还包括加载脚本 我希望能够测试是否加载了外部脚本 特别是 Google Maps JS API 如果没有加载 则继续执行此操作 这是我的代码 if href cont
  • Arbor Js - 节点 Onclick?

    我在用着arbor js http arborjs org 创建图表 我如何创建一个onclick节点的事件 或者在单击时在某处创建节点链接 Arborjs org 主页的节点在单击时链接到外部页面 我如何复制它 或者使节点在单击时调用 j
  • 像在 tumblr 注册页面上那样鬼写

    有谁知道如何创建一个类似 tumblr 的字段 http www tumblr com http www tumblr com 这是一个 URL 字段 其中有一些褪色的文本 然后当您单击它并键入时 它会附加一些文本 tumblr com 您
  • 当覆盖设置为 null 时,通过外部单击关闭 fancybox

    我正在使用 fancybox 2 1 4 插件 它工作得很好 但我有一个问题 我想将覆盖设置为空 并且当用户单击 fancybox 容器外部 时关闭 fancybox 我已经尝试过以下代码 但它不起作用 因为没有可供单击的覆盖层 fancy
  • 如何使用 jquery-validate 本地化

    有没有办法使用存储库中现有的翻译来动态 即从 JS 代码 设置 更改错误消息的语言 非解决方案 1 加载本地化脚本
  • 自定义 jQuery 验证 .addMethod

    我有一个表单 可以根据最小 最大长度验证邮政编码 我需要将所有国家 地区的邮政编码最小设置为 5 位数字 澳大利亚除外 澳大利亚需要为 4 位数字 这是我遇到的问题 validator addMethod AusZip function v
  • 如何在通过 .ajaxForm() 提交表单之前执行一些操作?

    我正在使用 ajaxForm 框架来发送我的数据 而无需重新加载我的页面 ReplayForm ajaxForm success function data alert Success 现在 我想在提交表单之前检查一些条件 如果条件为假 则
  • 在 jQuery .live() 方法中模拟“焦点”和“模糊”

    Update 从 jQuery 1 4 开始 live 现在支持focusin and focusout events jQuery http www jquery com currently1 doesn t support blur o
  • Ajax - 限制列表的加载,然后在滚动上加载其余部分

    我有一家商店 在一个页面上显示某个类别的所有产品 这是店主喜欢的方式 因此不能选择分页 为了缩短某些重类别的加载时间 我希望实现一个可以加载许多产品的脚本 li s然后在页面滚动上加载另一组 页面就是用这个结构生成的 div ul clas
  • jQuery输入文件点击方法和IE上拒绝访问

    我尝试仅使用一个按钮作为输入文件 它在 Firefox Chrome Safari 中工作正常 但在 IE 中不行 提交表单时我总是收到 访问被拒绝 的消息 代码 input file click 有真正的解决方法吗 我在谷歌上浪费了大约2
  • 销毁 JQuery 可拖动对象

    我有一组可拖动的元素 如何删除可拖动功能 draggable draggable disable 对我来说不是一个选择 draggable draggable destroy 回报Uncaught TypeError Cannot read
  • 如何动态调整jqgrid到当前窗口大小?

    如何动态调整jqgrid到当前窗口大小 基于javascript jQuery 最好的例子在这里 TinyMCE 去 http www tinymce com tryit full php http www tinymce com tryi

随机推荐

  • java 网站用户在线和客服聊天

    注 xff1a 本文来源于 java 网站用户在线和客服聊天 这是应用到项目中的一个例子 实现原理是将信息存储到Application域里面 然后使用Struts2 Action 用json格式的数据进行前后台交互 截图 xff1a 前台用
  • Linux中文乱码问题终极解决方法

    方法一 xff1a 修改 root bash profile文件 xff0c 增加export LANG 61 zh CN GB18030 该文件在用户目录下 xff0c 对于其他用户 xff0c 也必须相应修改该文件 使用该方法时putt
  • C#将Excel数据表导入SQL数据库的两种方法(转)

    最近用写个winform程序想用excel 文件导入数据库中 xff0c 网上寻求办法 xff0c 找到了这个经过尝试可以使用 方法一 实现在c 中可高效的将excel数据导入到sqlserver数据库中 很多人通过循环来拼接sql xff
  • 最难学的10大编程语言排行榜,Java只排第三,第一出乎意料

    2018年12月的TIOBE编程语言排行榜已经出炉 xff0c Python重回前三 xff0c Go语言跌出前十 xff0c Visual Basic NET涨幅明显 xff0c 保持第五名 TIOBE排行榜是根据互联网上有经验的程序员
  • 网络安全设计方案

    IDC网络系统安全实施方案 1 吉通上海 IDC网络安全功能需求 1 1 吉通上海公司对于网络安全和系统可靠性的总体设想 xff08 1 xff09 网络要求有充分的安全措施 xff0c 以保障网络服务的可用性和网络信息的完整性 要把网络安
  • Altium_Designer-怎么将“原理图的更改”更新到“pcb图”?

    打开原理图 xff0c 直击菜单栏 gt gt Design xff0c 选择第一项 xff0c gt gt Update PCB Document 在弹出的对话框里面选择执行更改即可将原理图更新到工程下面对应的PCB 也可以先点生效更改看
  • Permutations II

    Given a collection of numbers that might contain duplicates return all possible unique permutations For example 1 1 2 ha
  • 【沧海拾昧】C# .Net 基本控件介绍

    C0201 沧海茫茫千钟粟 xff0c 且拾吾昧一微尘 沧海拾昧集 64 CuPhoenix 阅前敬告 沧海拾昧集仅做个人学习笔记之用 xff0c 所述内容不专业不严谨不成体系 如有问题必是本集记录有谬 xff0c 切勿深究 写在前面 本文
  • Super Ugly Number

    Write a program to find the nth super ugly number Super ugly numbers are positive numbers whose all prime factors are in
  • DirectUI中模态对话框和菜单的原理

    经常有人问关于模态对话框和系统菜单内部实现原理方面的问题 xff0c 因为系统通过API隐藏了太多细节 xff0c 这2个问题确实令初学者甚至是有经验的开发者困扰 xff0c 下面是我个人的一些经验总结 先说模态对话框 xff0c 外部看模
  • AD16PCB如何快速删除走线

    工具 xff08 Tools xff09 取消布线 xff08 Un Route xff09 全部 xff08 AII xff09 ad pcb画图 xff0c 如果想整体去掉一条线 xff0c 只要是连接在一起的 xff0c 不管在哪一层
  • 如何正确的选择云数据库?

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 本文由云 43 社区发表 作者 xff1a 数据库 江湖传说在选择和使用云数据库过程中 10个人有9个会遇到以下问题 xff1a 数据库正常使用过程中莫名卡顿 经常遭遇主从
  • C和C++的区别

    C 43 43 几乎完全兼容了C xff0c 但增加的特性差不多比整个C语言还多 C是一种简单的语言 它真正提供的只有有宏 指针 结构 数组和函数 xff1b 而C 43 43 还有私有和保护型成员 函数重载 缺省参数 构造和析构函数 自定
  • Pixhawk 添加超声波定高

    记录一下自己在Pixhawk上添加超声波模块进行定高的过程 xff1a 硬件 xff1a Pixhawk2 4 8 xff08 APM3 5固件 xff09 STM32开发板 几块钱一个的超声波模块 软件 xff1a Mission Pla
  • 使用VNC远程登录Linux主机

    一 安装 tigervnc yum install tigervnc server 这是服务端软件 yum install tigervnc 这是客户端软件 xff0c 安装完后 xff0c 程序在 应用程序 gt Internet gt
  • 重读 JVM

    秋招开始了 xff0c 前面由于做别的事耽误了半个月 xff0c 以前学的东西不用就很容易忘记 所以 xff0c 这次重新阅读 深入理解 JVM 虚拟机 时 xff0c 想做一个记录 将碎片的知识整合 xff0c 方便自己以后阅读 xff0
  • MA5680T跨板聚合

    跨板聚合可以提高上行带宽 xff0c 又可以给上行做冗余在出现单板故障时保持正常通信 MA5680T 0 19 0 与0 20 0做跨板聚合命令 xff1a link aggregation 0 19 0 0 20 0 egress ing
  • Deep Learning for Computer Vision with Python.

    Welcome back This is the fourth post in the deep learning development environment configuration series which accompany m
  • xargs

    功能说明 xff1a 向其他命令传递命令行参数的一个过滤器 xff0c 能够将管道或者标准输入传递的数据转换成xargs命令后跟随的命令的命令行参数 选项说明 xff1a n 指定每行的最大参数量 xff0c 可以将标准输入的文本划分为多行
  • TWEEN动画、JQUERY、ES6 — 2、轮播图-渐隐渐现版本

    分析 目录 编译并压缩less xff08 需要先npm安装less xff09 banner html lt DOCTYPE html gt lt html gt lt head gt lt meta charset 61 span cl