touch事件滑动图片

冬天太冷 2017-04-11 02:43:49


问题比较模糊,标题不好描述,直接上代码,请问有人知道解决吗?

css代码:

* {
margin:0;
padding:0;
}

body {
font-size:12px;
font-family:宋体,arial, helvetica, sans-serif;
color:#282828;
margin:0 auto;
}
.apply {
margin-top: 20px;
overflow: hidden;
width:1000px;
border:1px solid yellow;
margin-left:100px;
}
.apply_nav{
position: relative;
width: 996px;
float: left;
height: 252px;
overflow: hidden;
border:1px solid green;
}
.apply_w {
position: absolute;
margin-top: 22px;
float: left;
border:1px solid red;

}
.apply_array {
width: 198px;
float: left;
height: 212px;
border: #cdcdcd 1px solid;
padding-top: 16px;
}

html 代码:

<div>
<div class="apply">
<div class="apply_nav">
<div class="apply_w">
<div class="apply_array" id="inp">
<div class="apply_img"><img src="imgs/sinaminiblog.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/kaixin001.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/renren.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/baiducang.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/qq.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/qqshuqian.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/qqzone.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/sinavivi.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/sohubai.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/douban.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/zhuaxia.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/gmail.gif"></div>
</div>
<div class="apply_array">
<div class="apply_img"><img src="imgs/delicious.gif"></div>
</div>
</div>
</div>
</div>
</div>

js 代码:

$(function() {
$li1 = $(".apply_nav .apply_array"); //获取到里面每一个div
$window1 = $(".apply .apply_w"); //获取包裹最里面div的大div
$window1.css("width", $li1.length*200); //设置包裹图片的外层div的宽度等于 图片的张数*每张图片的宽度 = 外层div总宽度;
$window1.on({
touchstart: function(e){
var el=$(this);
var os = el.offset();
dx =e.originalEvent.targetTouches[0].pageX-os.left ; //e.originalEvent.targetTouches[0].pageX 鼠标位置 os.left向左边偏移量
$(document).on('touchmove.drag', function(e){
el.offset({ left: e.originalEvent.targetTouches[0].pageX-dx});
});

},
touchend: function(e){ $(document).off('touchmove.drag'); }
});
});

我的运行效果图:

鼠标滑上带图片的div是可以左右拖动的。但是到第一张和最后一张居然也可以拖动。

不知道怎么解决,有人知道吗,怎么样让他在第一张图片的时候不能往左移了,最后一张不能往后移
...全文
256 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
#麻辣小龙虾# 2017-06-07
  • 打赏
  • 举报
回复
引用 1 楼 CodingNoob 的回复:
这个是我之前自己写的一个移动端手机触屏滑动效果的banner。你可以参考下看对你有没有帮助不!
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta content="telephone=yes" name="format-detection" />
    <meta name="apple-mobile-web-app-status-bar-style" content="white">
    <meta name="x5-fullscreen" content="true">
    <meta name="apple-touch-fullscreen" content="yes">
    <title>Document</title>
    <style>
		*{margin:0;padding:0;}
        .box{
            height:200px;
            width:100%;
            overflow: hidden;
        }
        .movebox{
            height:200px;
            width:9000px;
            padding:0;
            position:relative;
            left:0;
        }
        .movebox li{
            height:200px;
            float:left;
            list-style:none;
            font-size:30px;
			color:#fff;
        }
       
    </style>
    <script>
        window.onload = function(){
		
            var moveX,		//手指滑动距离
				endX, 		//手指停止滑动时X轴坐标
				cout = 0,   //滑动计数器
				moveDir;	//滑动方向
            var movebox = document.querySelector(".movebox");	//滑动对象
			var Li = movebox.querySelectorAll("li");	//滑动对象item
			var width = parseInt(window.getComputedStyle(movebox.parentNode).width);	//滑动对象item的宽度
			
			movebox.style.width = (width*4) + "px"; //设置滑动盒子width
			for(var i = 0; i < Li.length; i++){
				Li[i].style.width = width + "px";	//设置滑动item的width,适应屏幕宽度
			}
			
			//触摸开始
            function boxTouchStart(e){
                var touch = e.touches[0];	//获取触摸对象
                startX = touch.pageX;	//获取触摸坐标
                endX = parseInt(movebox.style.webkitTransform.replace("translateX(", ""));	//获取每次触摸时滑动对象X轴的偏移值
            }

            function boxTouchMove(e){
                var touch = e.touches[0];
                moveX = touch.pageX - startX;   //手指水平方向移动的距离
					
				if(cout == 0 && moveX > 0){		//刚开始第一次向左滑动时
					return false;
				}
				
				if(cout == 3 && moveX < 0){		//滑动到最后继续向右滑动时
					return false;
				}
				
				movebox.style.webkitTransform = "translateX(" + (endX + moveX) + "px)";	//手指滑动时滑动对象随之滑动
            }
			
            function boxTouchEnd(e){
                moveDir = moveX < 0 ? true : false;		//滑动方向大于0表示向左滑动,小于0表示向右滑动
                //手指向左滑动
				if(moveDir){
					
                    if(cout<3){
                        movebox.style.webkitTransform = "translateX(" + (endX-width) + "px)";
                        cout++;
                    }
				//手指向右滑动
                }else{
					//滑动到初始状态时返回false
                    if(cout == 0){
                        return false;
                    }else{
						movebox.style.webkitTransform = "translateX(" + (endX+width) + "px)";
						cout--;
					}
                }
            }

			//滑动对象事件绑定
            movebox.addEventListener("touchstart", boxTouchStart, false);
            movebox.addEventListener("touchmove", boxTouchMove, false);
            movebox.addEventListener("touchend", boxTouchEnd, false);
        }
    </script>
</head>

<body style="position:absolute;width:100%;overflow:hidden;">
    <div class="box">
        <ul class="movebox" style="transition-duration:0.2s;transform: translateX(-0px);">
            <li style="background:red;">1</li>
            <li style="background:yellow">2</li>
            <li style="background:blue">3</li>
            <li style="background:green">4</li>
        </ul>
    </div>
</body>
</html>
实在不行你就用swiper插件 http://www.swiper.com.cn/
冬天太冷 2017-06-07
  • 打赏
  • 举报
回复
引用 1 楼 CodingNoob 的回复:
这个是我之前自己写的一个移动端手机触屏滑动效果的banner。你可以参考下看对你有没有帮助不!
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta content="telephone=yes" name="format-detection" />
    <meta name="apple-mobile-web-app-status-bar-style" content="white">
    <meta name="x5-fullscreen" content="true">
    <meta name="apple-touch-fullscreen" content="yes">
    <title>Document</title>
    <style>
		*{margin:0;padding:0;}
        .box{
            height:200px;
            width:100%;
            overflow: hidden;
        }
        .movebox{
            height:200px;
            width:9000px;
            padding:0;
            position:relative;
            left:0;
        }
        .movebox li{
            height:200px;
            float:left;
            list-style:none;
            font-size:30px;
			color:#fff;
        }
       
    </style>
    <script>
        window.onload = function(){
		
            var moveX,		//手指滑动距离
				endX, 		//手指停止滑动时X轴坐标
				cout = 0,   //滑动计数器
				moveDir;	//滑动方向
            var movebox = document.querySelector(".movebox");	//滑动对象
			var Li = movebox.querySelectorAll("li");	//滑动对象item
			var width = parseInt(window.getComputedStyle(movebox.parentNode).width);	//滑动对象item的宽度
			
			movebox.style.width = (width*4) + "px"; //设置滑动盒子width
			for(var i = 0; i < Li.length; i++){
				Li[i].style.width = width + "px";	//设置滑动item的width,适应屏幕宽度
			}
			
			//触摸开始
            function boxTouchStart(e){
                var touch = e.touches[0];	//获取触摸对象
                startX = touch.pageX;	//获取触摸坐标
                endX = parseInt(movebox.style.webkitTransform.replace("translateX(", ""));	//获取每次触摸时滑动对象X轴的偏移值
            }

            function boxTouchMove(e){
                var touch = e.touches[0];
                moveX = touch.pageX - startX;   //手指水平方向移动的距离
					
				if(cout == 0 && moveX > 0){		//刚开始第一次向左滑动时
					return false;
				}
				
				if(cout == 3 && moveX < 0){		//滑动到最后继续向右滑动时
					return false;
				}
				
				movebox.style.webkitTransform = "translateX(" + (endX + moveX) + "px)";	//手指滑动时滑动对象随之滑动
            }
			
            function boxTouchEnd(e){
                moveDir = moveX < 0 ? true : false;		//滑动方向大于0表示向左滑动,小于0表示向右滑动
                //手指向左滑动
				if(moveDir){
					
                    if(cout<3){
                        movebox.style.webkitTransform = "translateX(" + (endX-width) + "px)";
                        cout++;
                    }
				//手指向右滑动
                }else{
					//滑动到初始状态时返回false
                    if(cout == 0){
                        return false;
                    }else{
						movebox.style.webkitTransform = "translateX(" + (endX+width) + "px)";
						cout--;
					}
                }
            }

			//滑动对象事件绑定
            movebox.addEventListener("touchstart", boxTouchStart, false);
            movebox.addEventListener("touchmove", boxTouchMove, false);
            movebox.addEventListener("touchend", boxTouchEnd, false);
        }
    </script>
</head>

<body style="position:absolute;width:100%;overflow:hidden;">
    <div class="box">
        <ul class="movebox" style="transition-duration:0.2s;transform: translateX(-0px);">
            <li style="background:red;">1</li>
            <li style="background:yellow">2</li>
            <li style="background:blue">3</li>
            <li style="background:green">4</li>
        </ul>
    </div>
</body>
</html>
您好,按照您给的代码改动了下,但是还是会有点问题,当下面没有图片了也可以滑动。 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta content="telephone=yes" name="format-detection" /> <meta name="apple-mobile-web-app-status-bar-style" content="white"> <meta name="x5-fullscreen" content="true"> <meta name="apple-touch-fullscreen" content="yes"> <title>Document</title> <style> *{margin:0;padding:0;} .box{ height:720px; width:200px; overflow: hidden; border: 1px solid red; } .movebox{ height:720px; width:200px; padding:0; position:relative; left:0; border: 1px solid yellow; } .movebox li{ height:180px; float:left; list-style:none; font-size:30px; color:#fff; width:200px; display: inline-block; } </style> <script> window.onload = function(){ var moveY, //手指滑动距离 endY, //手指停止滑动时X轴坐标 cout = 0, //滑动计数器 moveDir; //滑动方向 var movebox = document.querySelector(".movebox"); //滑动对象 ul标签 var Li = movebox.querySelectorAll("li"); //滑动对象item li标签 var width = parseInt(window.getComputedStyle(movebox.parentNode).height); //滑动对象item的宽度 width = (Li[0].offsetHeight*Li.length) + "px"; //设置滑动盒子width 将box的宽度乘以4 g给Ul //触摸开始 function boxTouchStart(e){ var touch = e.touches[0]; //获取触摸对象 startY = touch.pageY; //获取触摸坐标 endY = parseInt(movebox.style.webkitTransform.replace("translateY(", "")); //获取每次触摸时滑动对象X轴的偏移值 } function boxTouchMove(e){ var touch = e.touches[0]; moveY = touch.pageY - startY; //手指水平方向移动的距离 if(cout == 0 && moveY > 0){ //刚开始第一次向上滑动时 return false; } if(cout == 3 && moveY < 0){ //滑动到最后继续向下滑动时 return false; } movebox.style.webkitTransform = "translateY(" + (endY + moveY) + "px)"; //手指滑动时滑动对象随之滑动 } function boxTouchEnd(e){ moveDir = moveY < 0 ? true : false; //滑动方向大于0表示向左滑动,小于0表示向右滑动 //手指向左滑动 if(moveDir){ if(cout<4){ movebox.style.webkitTransform = "translateY(" + (endY-width) + "px)"; cout++; } //手指向右滑动 }else{ //滑动到初始状态时返回false if(cout == 0){ return false; }else{ movebox.style.webkitTransform = "translateY(" + (endY+width) + "px)"; cout--; } } } //滑动对象事件绑定 movebox.addEventListener("touchstart", boxTouchStart, false); movebox.addEventListener("touchmove", boxTouchMove, false); movebox.addEventListener("touchend", boxTouchEnd, false); } </script> </head> <body style="position:absolute;width:100%;overflow:hidden;"> <div class="box"> <ul class="movebox" style="transition-duration:0.2s;transform: translateY(-0px);"> <li style="background:red;">1</li> <li style="background:yellow">2</li> <li style="background:blue">3</li> <li style="background:green">4</li> <li style="background:black">5</li> </ul> </div> </body> </html> 是怎么回事
#麻辣小龙虾# 2017-04-12
  • 打赏
  • 举报
回复
这个是我之前自己写的一个移动端手机触屏滑动效果的banner。你可以参考下看对你有没有帮助不!
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta content="telephone=yes" name="format-detection" />
    <meta name="apple-mobile-web-app-status-bar-style" content="white">
    <meta name="x5-fullscreen" content="true">
    <meta name="apple-touch-fullscreen" content="yes">
    <title>Document</title>
    <style>
		*{margin:0;padding:0;}
        .box{
            height:200px;
            width:100%;
            overflow: hidden;
        }
        .movebox{
            height:200px;
            width:9000px;
            padding:0;
            position:relative;
            left:0;
        }
        .movebox li{
            height:200px;
            float:left;
            list-style:none;
            font-size:30px;
			color:#fff;
        }
       
    </style>
    <script>
        window.onload = function(){
		
            var moveX,		//手指滑动距离
				endX, 		//手指停止滑动时X轴坐标
				cout = 0,   //滑动计数器
				moveDir;	//滑动方向
            var movebox = document.querySelector(".movebox");	//滑动对象
			var Li = movebox.querySelectorAll("li");	//滑动对象item
			var width = parseInt(window.getComputedStyle(movebox.parentNode).width);	//滑动对象item的宽度
			
			movebox.style.width = (width*4) + "px"; //设置滑动盒子width
			for(var i = 0; i < Li.length; i++){
				Li[i].style.width = width + "px";	//设置滑动item的width,适应屏幕宽度
			}
			
			//触摸开始
            function boxTouchStart(e){
                var touch = e.touches[0];	//获取触摸对象
                startX = touch.pageX;	//获取触摸坐标
                endX = parseInt(movebox.style.webkitTransform.replace("translateX(", ""));	//获取每次触摸时滑动对象X轴的偏移值
            }

            function boxTouchMove(e){
                var touch = e.touches[0];
                moveX = touch.pageX - startX;   //手指水平方向移动的距离
					
				if(cout == 0 && moveX > 0){		//刚开始第一次向左滑动时
					return false;
				}
				
				if(cout == 3 && moveX < 0){		//滑动到最后继续向右滑动时
					return false;
				}
				
				movebox.style.webkitTransform = "translateX(" + (endX + moveX) + "px)";	//手指滑动时滑动对象随之滑动
            }
			
            function boxTouchEnd(e){
                moveDir = moveX < 0 ? true : false;		//滑动方向大于0表示向左滑动,小于0表示向右滑动
                //手指向左滑动
				if(moveDir){
					
                    if(cout<3){
                        movebox.style.webkitTransform = "translateX(" + (endX-width) + "px)";
                        cout++;
                    }
				//手指向右滑动
                }else{
					//滑动到初始状态时返回false
                    if(cout == 0){
                        return false;
                    }else{
						movebox.style.webkitTransform = "translateX(" + (endX+width) + "px)";
						cout--;
					}
                }
            }

			//滑动对象事件绑定
            movebox.addEventListener("touchstart", boxTouchStart, false);
            movebox.addEventListener("touchmove", boxTouchMove, false);
            movebox.addEventListener("touchend", boxTouchEnd, false);
        }
    </script>
</head>

<body style="position:absolute;width:100%;overflow:hidden;">
    <div class="box">
        <ul class="movebox" style="transition-duration:0.2s;transform: translateX(-0px);">
            <li style="background:red;">1</li>
            <li style="background:yellow">2</li>
            <li style="background:blue">3</li>
            <li style="background:green">4</li>
        </ul>
    </div>
</body>
</html>

87,918

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧