<!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中文网其他相关文章!

要将React集成到HTML中,需遵循以下步骤:1.在HTML文件中引入React和ReactDOM。2.定义一个React组件。3.使用ReactDOM将组件渲染到HTML元素中。通过这些步骤,可以将静态HTML页面转化为动态、交互式的体验。

React受欢迎的原因包括其性能优化、组件复用和丰富的生态系统。1.性能优化通过虚拟DOM和diffing机制实现高效更新。2.组件复用通过可复用组件减少重复代码。3.丰富的生态系统和单向数据流增强了开发体验。

React是构建动态和交互式用户界面的首选工具。1)组件化与JSX使UI拆分和复用变得简单。2)状态管理通过useState钩子实现,触发UI更新。3)事件处理机制响应用户交互,提升用户体验。

React是前端框架,用于构建用户界面;后端框架用于构建服务器端应用程序。React提供组件化和高效的UI更新,后端框架提供完整的后端服务解决方案。选择技术栈时需考虑项目需求、团队技能和可扩展性。

HTML和React的关系是前端开发的核心,它们共同构建现代Web应用的用户界面。1)HTML定义内容结构和语义,React通过组件化构建动态界面。2)React组件使用JSX语法嵌入HTML,实现智能渲染。3)组件生命周期管理HTML渲染,根据状态和属性动态更新。4)使用组件优化HTML结构,提高可维护性。5)性能优化包括避免不必要渲染,使用key属性,保持组件单一职责。

React是构建交互式前端体验的首选工具。1)React通过组件化和虚拟DOM简化UI开发。2)组件分为函数组件和类组件,函数组件更简洁,类组件提供更多生命周期方法。3)React的工作原理依赖虚拟DOM和调和算法,提高性能。4)状态管理使用useState或this.state,生命周期方法如componentDidMount用于特定逻辑。5)基本用法包括创建组件和管理状态,高级用法涉及自定义钩子和性能优化。6)常见错误包括状态更新不当和性能问题,调试技巧包括使用ReactDevTools和优

React是一个用于构建用户界面的JavaScript库,其核心是组件化和状态管理。1)通过组件化和状态管理简化UI开发。2)工作原理包括调和和渲染,优化可通过React.memo和useMemo实现。3)基本用法是创建并渲染组件,高级用法包括使用Hooks和ContextAPI。4)常见错误如状态更新不当,可使用ReactDevTools调试。5)性能优化包括使用React.memo、虚拟化列表和CodeSplitting,保持代码可读性和可维护性是最佳实践。

React通过JSX与HTML结合,提升用户体验。1)JSX嵌入HTML,使开发更直观。2)虚拟DOM机制优化性能,减少DOM操作。3)组件化管理UI,提高可维护性。4)状态管理和事件处理增强交互性。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版
好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器