手写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;
}()
相关推荐
hackeroink44 分钟前
【2024版】最新推荐好用的XSS漏洞扫描利用工具_xss扫描工具
前端·xss
迷雾漫步者2 小时前
Flutter组件————FloatingActionButton
前端·flutter·dart
向前看-3 小时前
验证码机制
前端·后端
燃先生._.4 小时前
Day-03 Vue(生命周期、生命周期钩子八个函数、工程化开发和脚手架、组件化开发、根组件、局部注册和全局注册的步骤)
前端·javascript·vue.js
高山我梦口香糖5 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
m0_748235245 小时前
前端实现获取后端返回的文件流并下载
前端·状态模式
m0_748240256 小时前
前端如何检测用户登录状态是否过期
前端
black^sugar6 小时前
纯前端实现更新检测
开发语言·前端·javascript
寻找沙漠的人6 小时前
前端知识补充—CSS
前端·css