手机端js实现滑块推动

代码编写:(写的格式有点差,凑合看吧,但是功能是实现了的)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题</title>
<script type="text/javascript" src="./jquery-1.8.0.min.js"></script>
<style type="text/css">
.silder{
width: 80%;
height: 20PX;
margin: 0 auto;
margin-top: 100PX;
background-color: green;
position: relative;
}
.silderImg{
margin-top:-2PX;
margin-left: 0PX;
}
.silderLine{
height: 200PX;
margin-left: 12PX;
background-color: red;
width: 1PX;
}
#content{
position:absolute;
left:0PX;
top:0PX;
}
</style>
</head>
<body id="body">
<div class="silder">
<div id="content">
<img id="img" class="silderImg" src="./silder.png">
<div class="silderLine"></div>
</div>
</div>
<script type="text/javascript">
var oevent=new Function('e','if (!e.clientX) e = e.changedTouches[0];return e')
function touchMove(e){
e.preventDefault();
var parent=$(this).parent()[0];
var temp=$(parent).parent()[0].offsetLeft;
var x = oevent(e).clientX;
var oldLeft = ($(parent).css("left").replace("px","")/1);
var changeX = x-temp;
if(changeX <= -12){
changeX = -12;
}else if(changeX >= $(parent).parent().width()-12){
changeX = $(parent).parent().width()-12;
}
console.log(changeX);
$(parent).css("left",changeX);
}
document.getElementById('img').addEventListener("touchmove", touchMove, false);
</script>
</body>
</html>

  

你可能感兴趣的:(手机端js实现滑块推动)