手写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;
}()
相关推荐
光影少年1 分钟前
vue2与vue3的全局通信插件,如何实现自定义的插件
前端·javascript·vue.js
As977_2 分钟前
前端学习Day12 CSS盒子的定位(相对定位篇“附练习”)
前端·css·学习
susu10830189114 分钟前
vue3 css的样式如果background没有,如何覆盖有background的样式
前端·css
Ocean☾6 分钟前
前端基础-html-注册界面
前端·算法·html
Rattenking6 分钟前
React 源码学习01 ---- React.Children.map 的实现与应用
javascript·学习·react.js
Dragon Wu8 分钟前
前端 Canvas 绘画 总结
前端
CodeToGym12 分钟前
Webpack性能优化指南:从构建到部署的全方位策略
前端·webpack·性能优化
~甲壳虫14 分钟前
说说webpack中常见的Loader?解决了什么问题?
前端·webpack·node.js
~甲壳虫18 分钟前
说说webpack proxy工作原理?为什么能解决跨域
前端·webpack·node.js
Cwhat19 分钟前
前端性能优化2
前端