手写swiper

<!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>
!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;
}()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的手写swiper实现: ```html <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide">Slide 1</div> <div class="swiper-slide">Slide 2</div> <div class="swiper-slide">Slide 3</div> <div class="swiper-slide">Slide 4</div> <div class="swiper-slide">Slide 5</div> </div> </div> ``` ```css .swiper-container { position: relative; height: 300px; overflow: hidden; } .swiper-wrapper { position: absolute; width: 100%; height: 100%; display: flex; transition: transform 0.3s ease-out; } .swiper-slide { width: 100%; height: 100%; flex-shrink: 0; margin-right: 20px; } ``` ```js const container = document.querySelector('.swiper-container'); const wrapper = document.querySelector('.swiper-wrapper'); const slides = document.querySelectorAll('.swiper-slide'); const slideWidth = slides[0].offsetWidth + 20; // 计算每个slide的宽度 let currentIndex = 0; let isMoving = false; // 移动到指定slide function moveTo(index) { if (index < 0 || index > slides.length - 1 || isMoving) return; isMoving = true; wrapper.style.transform = `translateX(-${slideWidth * index}px)`; currentIndex = index; setTimeout(() => { isMoving = false; }, 300); } // 绑定滑动事件 let startX, startY; container.addEventListener('touchstart', (e) => { startX = e.touches[0].clientX; startY = e.touches[0].clientY; }); container.addEventListener('touchmove', (e) => { const deltaX = e.touches[0].clientX - startX; const deltaY = e.touches[0].clientY - startY; if (Math.abs(deltaX) > Math.abs(deltaY)) { e.preventDefault(); wrapper.style.transform = `translateX(-${slideWidth * currentIndex - deltaX}px)`; } }); container.addEventListener('touchend', (e) => { const deltaX = e.changedTouches[0].clientX - startX; if (Math.abs(deltaX) > slideWidth / 3) { moveTo(currentIndex + (deltaX > 0 ? -1 : 1)); } else { moveTo(currentIndex); } }); // 初始化 wrapper.style.width = `${slideWidth * slides.length}px`; moveTo(0); ``` 以上代码实现了一个简单的手写swiper,支持滑动切换和左右箭头切换。需要注意的是,这里的样式和交互仅仅是做一个示例,实际使用中还需要考虑更多的细节问题,比如可配置性、性能优化等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微个日光日

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值