手写swiper

html 复制代码
<!DOCTYPE html>
<html>
<head>
	<title>swiper</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" type="text/css" href="./index.css">
	<style type="text/css">
		.swiper-wrap{
			width: 100%;
			position: relative;
		}
		.swiper-box{
			width: 100%;
			height: 100px;
			position: relative;
		}
		.swiper-item{
			height: 100px;
			width: 100%;
		}
		.swiper-horizontal{
			position: absolute;

		}
	</style>
</head>
<body>
	<div class="swiper-wrap" id="swiper-1">
		<div class="swiper-box">
			<div class="swiper-item" style="background-color: red;">
				
			</div>
			<div class="swiper-item" style="background-color: green;">
				
			</div>
			<div class="swiper-item" style="background-color: blue;">
				
			</div>
		</div>
	</div>
	<p></p>
	<div class="swiper-wrap" id="swiper-2">
		<div class="swiper-box">
			<div class="swiper-item" style="background-color: red;">
				
			</div>
			<div class="swiper-item" style="background-color: green;">
				
			</div>
			<div class="swiper-item" style="background-color: blue;">
				
			</div>
		</div>
	</div>
<script type="text/javascript" src="./index.js"></script>
<script type="text/javascript">
	var swiper = new Swiper({
		direction: 'horizontal',

	});
</script>
</body>
</html>
javascript 复制代码
!function(){
	function Swiper(options){
		this.init();
	}

	var p = Swiper.prototype;

	p.config = {
		swiper_wrap_select: '.swiper-wrap',
		direction: 'horizontal',
	}

	p.content_width = undefined;

	p.elements = {
		$swiper_wrap: null,
		$swiper_box: null,
		$swiper_slides: null
	}

	p.init = function() {
		this.getElements();
		this.layout();
	}

	p.getElements = function() {
		var swiper_wrap_select = this.config.swiper_wrap_select;
		if(swiper_wrap_select.indexOf('.') >= 0) {
			var swiper_wraps = document.getElementsByClassName(swiper_wrap_select.replace('.',''));
			if(swiper_wraps.length > 0) {
				this.elements.$swiper_wrap = swiper_wraps[0];
				this.elements.$swiper_box = this.elements.$swiper_wrap.children[0];
				this.elements.$swiper_slides = this.elements.$swiper_box.children;
				console.log(this.elements);
			}
			
			// for(item in this.elements.$swiper_wrap){
			// 	console.log(item)
			// }
		}
		//this.layout();
	}

	p.layout = function() {
		var $swiper_slides = this.elements.$swiper_slides;
		for(var index in $swiper_slides) {
			if($swiper_slides[parseInt(index)]){
				this.layoutSlideItem($swiper_slides[parseInt(index)]);	
			}	
		}
		//this.layoutSlideItem();	
	}

	p.layoutSlideItem = function($item) {
		if(this.config.direction == 'horizontal'){
			$item.className += ' swiper-horizontal';
		}
		console.log($item);

	}

	window.Swiper = Swiper;
}()
相关推荐
namehu几秒前
前端性能优化之:图片缩放 🚀
前端·性能优化·微信小程序
rit84324993 分钟前
ES6 箭头函数:告别 `this` 的困扰
开发语言·javascript·es6
摸鱼的春哥6 分钟前
【编程】是什么编程思想,让老板对小伙怒飙英文?Are you OK?
前端·javascript·后端
尘世中一位迷途小书童7 分钟前
版本管理实战:Changeset 工作流完全指南(含中英文对照)
前端·面试·架构
尘世中一位迷途小书童11 分钟前
VitePress 文档站点:打造专业级组件文档(含交互式示例)
前端·架构·前端框架
甜瓜看代码11 分钟前
666
前端
吃饺子不吃馅16 分钟前
【八股汇总,背就完事】这一次再也不怕webpack面试了
前端·面试·webpack
Amos_Web24 分钟前
Rust实战教程--文件管理命令行工具
前端·rust·全栈
li理32 分钟前
鸿蒙相机开发入门篇(官方实践版)
前端
webxin66633 分钟前
页面动画和延迟加载动画的实现
前端·javascript