原生js组件化开发简单轮播图实例代码
<!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 { overflow: hidden; position: relative; } ul, ol, li { padding: 0; margin: 0; left: 0; top: 0; list-style: none; } .trans{ transition: all .3s ease; } .banner-wraper { position: absolute; left: 0; top: 0; } .banner-wraper .banner-item { float: left; width: 900px; height: 400px; background: yellow; } .banner-wraper .banner-item:nth-child(3) { background: blue; } .banner-wraper .banner-item:nth-child(2n) { background: green; } .navigation { position: absolute; left: 50%; transform: translateX(-50%); top: 90%; } .nav-item { width: 10px; height: 10px; background: #000; display: inline-block; margin: 0 5px; } .nav-item:hover { cursor: pointer; opacity: 0.4; } .navigation .active { opacity: 0.4; } </style> </head> <body> <p class="banner" id="swiper"> <ul class="banner-wraper trans"> <li class="banner-item"></li> <li class="banner-item"></li> <li class="banner-item"></li> <li class="banner-item"></li> <li class="banner-item"></li> <li class="banner-item"></li> </ul> <ol class="navigation"> </ol> </p> <!-- <script src="./jquery.js"></script> --> <script> (function (win, doc) { function Swiper(options) { return new Swiper.prototype.init(options); } Swiper.prototype.init = function (options) { // 初始化方法配置 this.options = options; this.options.delay = options.delay || 4000; this.options.autoPlay = options.autoPlay || false; this.banner = doc.querySelector(this.options.el); this.bannerWraper = this.banner.querySelector('.banner-wraper'); this.bannerItem = this.banner.querySelectorAll('.banner-item'); this.bannerW = this.bannerItem[0].offsetWidth;//轮播图宽度; this.banner.style.width = this.bannerW + 'px';//根据item大小定义轮播图容器宽度 this.banner.style.minHeight = this.bannerItem[0].offsetHeight + 'px';//根据item大小定义轮播图容器高度 this.bannerWraper.style.width = this.bannerW * this.bannerItem.length + 'px'; this.bannerItem[0].className = 'banner-item active'; this.navigationItem = null; // 定义当前页码索引 this.index = 0; this.speed = null; //定时器 this.timer = null; this.init(this.options); } Swiper.fn = Swiper.prototype.init.prototype; Swiper.fn.init = function () { var speed = getComputedStyle(this.bannerWraper, false).transition; speed = speed.split(' ')[1]; this.speed = (+speed.substring(0,speed.length-1)) * 1000; // console.log(this.speed,'speed'); // 初始化功能函数 this.initNavigation(); this.changeActive(); // 鼠标移入清除自动播放 this.clearTimer(); if (this.options.loop) this.loop(); if (this.options.autoPlay) this.autoPlay(); } //初始化分页按钮 Swiper.fn.initNavigation = function () { var navigation = doc.querySelector('.navigation'); var navigationItem = ''; for (var i = 0, len = this.bannerItem.length; i < len; ++i) { if (i === 0) { navigationItem += '<p class="nav-item active"></p>'; } else { navigationItem += '<p class="nav-item"></p>'; } } navigation.innerHTML = navigationItem; this.navigationItem = doc.querySelectorAll('.nav-item'); } Swiper.fn.changeActive = function () { var _this = this; for (var i = 0, len = _this.navigationItem.length; i < len; ++i) { (function (i) {// 闭包保存i值 _this.navigationItem[i].onclick = function () { _this.index = i; _this.changeMain(this); } })(i); } } Swiper.fn.changeMain = function (This) { // console.log(this.index); var _this = this; if(this.options.loop && this.index == 0) { this.index = this.navigationItem.length; }; for (var j = 0, len = this.navigationItem.length; j < len; ++j) { this.navigationItem[j].className = 'nav-item' } This.className = 'nav-item active';// 当前_this指向 li this.bannerWraper.style.left = - this.bannerW * this.index + 'px'; // console.log(this.index,'this.index'); if(this.options.loop && this.index == this.navigationItem.length){ setTimeout(function(){ _this.bannerWraper.className = 'banner-wraper'; _this.bannerWraper.style.left = 0; setTimeout(function(){ _this.bannerWraper.className = 'banner-wraper trans'; }, _this.speed/2); }, _this.speed); } } //自动播放 Swiper.fn.autoPlay = function () { var _this = this; var len = _this.navigationItem.length; this.options.loop === true ? len : len = len - 1; this.timer = setInterval(function () { if (_this.index >= len) { _this.index = 0; } else { _this.index++; } _this.changeMain(_this); if(_this.options.loop && _this.index === len){ _this.index = 0; _this.navigationItem[_this.index].className = 'nav-item active'; setTimeout(function(){ _this.bannerWraper.className = 'banner-wraper'; console.log(1) _this.bannerWraper.style.left = 0; setTimeout(function(){ console.log(2) _this.bannerWraper.className = 'banner-wraper trans'; }, _this.speed/2); }, _this.speed); return; } _this.navigationItem[_this.index].className = 'nav-item active'; }, this.options.delay); } // 鼠标移入/移出 => 清除/重启定时器 Swiper.fn.clearTimer = function () { var _this = this; this.banner.onmouseover = function () { clearInterval(_this.timer); _this.timer = null; } this.banner.onmouseout = function () { if (_this.options.autoPlay) _this.autoPlay(); } } Swiper.fn.loop = function() { var htmlObjConvertStr = function(htmlObj){ var pContainer = document.createElement('p'); pContainer.appendChild(htmlObj); return pContainer.innerHTML; }; var _bannerHtml = this.bannerWraper.innerHTML; this.bannerWraper.innerHTML = _bannerHtml + htmlObjConvertStr(this.bannerItem[0]); this.bannerItem = this.banner.querySelectorAll('.banner-item'); this.bannerWraper.style.width = this.bannerW * this.bannerItem.length + 'px';// 修正banenrWraper宽度 this.bannerItem[this.bannerItem.length - 1].className = 'banner-item';// 修正末尾banner-item类名 } Swiper.fn.constructor = Swiper;// 修正contructor指向 win.Swiper = Swiper;// 将swiper挂载到window对象 })(window, document); Swiper({ el: '#swiper', autoPlay: true, // 默认为false不自动播放 delay: 3000, // 默认为4s loop: true }); </script> </body> </html> 本篇介绍了原生js组件化开发简单轮播图实例代码,更多相关内容请关注php中文网。 相关推荐:
<a href="http://www.php.cn/website-design-ask-402906.html" target="_blank">前端调用微信支付接口</a><a href="http://www.php.cn/website-design-ask-402908.html" target="_blank"><br/><br/><br/></a><br/>
以上是原生js组件化开发简单轮播图实例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

如何使用JS和百度地图实现地图平移功能百度地图是一款广泛使用的地图服务平台,在Web开发中经常用于展示地理信息、定位等功能。本文将介绍如何使用JS和百度地图API实现地图平移功能,并提供具体的代码示例。一、准备工作使用百度地图API前,首先需要在百度地图开放平台(http://lbsyun.baidu.com/)上申请一个开发者账号,并创建一个应用。创建完成

人脸检测识别技术已经是一个比较成熟且应用广泛的技术。而目前最为广泛的互联网应用语言非JS莫属,在Web前端实现人脸检测识别相比后端的人脸识别有优势也有弱势。优势包括减少网络交互、实时识别,大大缩短了用户等待时间,提高了用户体验;弱势是:受到模型大小限制,其中准确率也有限。如何在web端使用js实现人脸检测呢?为了实现Web端人脸识别,需要熟悉相关的编程语言和技术,如JavaScript、HTML、CSS、WebRTC等。同时还需要掌握相关的计算机视觉和人工智能技术。值得注意的是,由于Web端的计

如何使用PHP和JS创建股票蜡烛图股票蜡烛图是股票市场中常见的一种技术分析图形,通过绘制股票的开盘价、收盘价、最高价和最低价等数据,帮助投资者更直观地了解股票的价格波动情况。本文将教你如何使用PHP和JS创建股票蜡烛图,并附上具体的代码示例。一、准备工作在开始之前,我们需要准备以下环境:1.一台运行PHP的服务器2.一个支持HTML5和Canvas的浏览器3

股票分析必备工具:学习PHP和JS绘制蜡烛图的步骤,需要具体代码示例随着互联网和科技的快速发展,股票交易已经成为许多投资者的重要途径之一。而股票分析是投资者决策的重要一环,其中蜡烛图被广泛应用于技术分析中。学习如何使用PHP和JS绘制蜡烛图将为投资者提供更多直观的信息,帮助他们更好地做出决策。蜡烛图是一种以蜡烛形状来展示股票价格的技术图表。它展示了股票价格的

如何使用JS和百度地图实现地图点击事件处理功能概述:在Web开发中,经常需要使用地图功能来展示地理位置和地理信息。而地图上的点击事件处理是地图功能中常用且重要的一部分。本文将介绍如何使用JS和百度地图API来实现地图的点击事件处理功能,并给出具体的代码示例。步骤:导入百度地图的API文件首先,要在HTML文件中导入百度地图API的文件,可以通过以下代码实现:

如何使用JS和百度地图实现地图热力图功能简介:随着互联网和移动设备的迅速发展,地图成为了一种普遍的应用场景。而热力图作为一种可视化的展示方式,能够帮助我们更直观地了解数据的分布情况。本文将介绍如何使用JS和百度地图API来实现地图热力图的功能,并提供具体的代码示例。准备工作:在开始之前,你需要准备以下事项:一个百度开发者账号,并创建一个应用,获取到相应的AP

随着互联网金融的迅速发展,股票投资已经成为了越来越多人的选择。而在股票交易中,蜡烛图是一种常用的技术分析方法,它能够显示股票价格的变化趋势,帮助投资者做出更加精准的决策。本文将通过介绍PHP和JS的开发技巧,带领读者了解如何绘制股票蜡烛图,并提供具体的代码示例。一、了解股票蜡烛图在介绍如何绘制股票蜡烛图之前,我们首先需要了解一下什么是蜡烛图。蜡烛图是由日本人

js和vue的关系:1、JS作为Web开发基石;2、Vue.js作为前端框架的崛起;3、JS与Vue的互补关系;4、JS与Vue的实践应用。
