左右箭头滑动列表

//slideshow 左右箭头滑动一组li焦点图
	autoSlide();
	function autoSlide(){
		clearAutoS=setInterval(autoFunS,5000);
	}
	function autoFunS(){
		var loc=$(".slideshow-box ul").css("left");
		if(loc=="-2370px"){
			loc="1185";
		}
		var newloc=parseInt(loc)-1185;
		newloc=newloc.toString()+"px";
		$(".slideshow-box ul").animate({
		left:newloc},300);
	}
	$(".slideshow-box").hover(function(){
		clearInterval(clearAutoS);		
	},function(){
		autoSlide();
	});
	$(".arrow-left").click(function(){
		var loc=$(".slideshow-box ul").css("left");
		//console.log(loc);
		if(loc=="0px"){
			return false;
		}else{
			var newloc=parseInt(loc)+1185;
			newloc=newloc.toString()+"px";
			$(".slideshow-box ul").animate({
			left:newloc},300);
		}
	});
	$(".arrow-right").click(function(){
		var loc=$(".slideshow-box ul").css("left");
		if(loc=="-2370px"){
			return false;
		}else{
			var newloc=parseInt(loc)-1185;
			newloc=newloc.toString()+"px";
			$(".slideshow-box ul").animate({
			left:newloc},300);
		}
	});

  

转载于:https://www.cnblogs.com/joy-cnblogs/p/4388514.html

这可以通过JavaScript和CSS实现。首先,需要在HTML中创建一个具有滑动效果的容器,例如一个div元素,并为其添加CSS样式,使其可以滑动。然后,使用JavaScript监听用户的滑动手势事件,并根据用户的滑动方向来移动容器的位置。 以下是一个示例代码,实现左右滑动效果: HTML代码: ``` <div class="slider"> <div class="slides"> <img src="image1.jpg"> <img src="image2.jpg"> <img src="image3.jpg"> </div> </div> ``` CSS代码: ``` .slider { width: 100%; overflow: hidden; } .slides { display: flex; width: 300%; } .slides img { width: 33.33%; float: left; } ``` JavaScript代码: ``` var slider = document.querySelector('.slider'); var slides = document.querySelector('.slides'); var slideIndex = 0; // 监听用户的滑动手势事件 slider.addEventListener('touchstart', handleTouchStart, false); slider.addEventListener('touchmove', handleTouchMove, false); var x1 = null; var y1 = null; // 处理滑动手势开始事件 function handleTouchStart(event) { x1 = event.touches[0].clientX; y1 = event.touches[0].clientY; } // 处理滑动手势移动事件 function handleTouchMove(event) { if (!x1 || !y1) { return; } var x2 = event.touches[0].clientX; var y2 = event.touches[0].clientY; var xDiff = x2 - x1; var yDiff = y2 - y1; if (Math.abs(xDiff) > Math.abs(yDiff)) { // 水平滑动 event.preventDefault(); if (xDiff > 0) { // 向右滑动 slideIndex--; if (slideIndex < 0) { slideIndex = slides.children.length - 1; } } else { // 向左滑动 slideIndex++; if (slideIndex >= slides.children.length) { slideIndex = 0; } } slides.style.transform = 'translateX(-' + slideIndex * 100 / slides.children.length + '%)'; } x1 = null; y1 = null; } ``` 这段代码中,我们使用了CSS的flex布局来创建一个具有滑动效果的容器,其中图片元素的宽度设为33.33%。然后,我们监听了滑动手势事件,并根据用户的滑动方向来移动容器的位置,从而实现了左右滑动效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值