<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8; " >
<title> Mobile Touch Sliding </title>
<meta name= "Author" content= "Rainna"/>
<meta name= "keywords" content= "Rainna ' js Lib"/>
<meta name= "description" content= "Mobile Touch Slide"/>
<meta name= "viewport" content= "Target-densitydpi=320,width=640,user-scalable=no" >
<style>
*{margin:0;padding:0;}
Li{list-style:none;}
. m-slider{width:600px;margin:50px 20px;overflow:hidden;}
. M-slider. cnt{position:relative;left:0;width:3000px;}
. M-slider cnt li{float:left;width:600px;}
. M-slider cnt img{display:block;width:100%;height:450px;}
. M-slider cnt p{margin:20px 0;}
. M-slider. Icons{text-align:center;color: #000;}
. m-slider. Icons span{margin:0 5px;}
. m-slider. Icons. curr{color:red;}
. f-anim{-webkit-transition:left. 2s linear;}
</style>
<body>
<div class= "M-slider" >
<ul class= "cnt" id= "Slider" >
<li>
<p>20140813 Mirror of the world, after all, just reflection. See your shadow, but can't touch your future </p>
</li>
<li>
<p>20140812 Xilin Hot to Dongwuqi S101, a very beautiful railway. Below the railway is a small salt marsh, with a hint of the feeling of the sky. It was a pity that I had not seen a train pass for one hours and had to go to Dongwuqi. </p>
</li>
<li>
<p>20140811 water color Why so blue, I also wonder, anyway, the natural saturation and contrast pull is the color of the </p>
</li>
<li>
<p> Ocean Planet 3 chongqing Weather hot I want to rails suicide </p>
</li>
<li>
<p> These works are from two designers as the viewer, can you differentiate by design style </p>
</li>
</ul>
<div class= "Icons" id= "Icons" >
<span class= "Curr" >1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
</div>
<script>
var slider = {
Determine if the device supports touch events
Touch: (' ontouchstart ' in window) | | Window. Documenttouch && document Instanceof Documenttouch,
Slider:document.getElementById (' Slider '),
Event
events:{
index:0,//index of the display element
Slider:this.slider,//this as Slider object
Icons:document.getElementById (' Icons '),
Icon:this.icons.getElementsByTagName (' span '),
Handleevent:function (event) {
var = this; This refers to the events object
if (Event.type = = ' Touchstart ') {
Self.start (event);
}else if (Event.type = = ' Touchmove ') {
Self.move (event);
}else if (Event.type = = ' Touchend ') {
Self.end (event);
}
},
Swipe to start
Start:function (event) {
var touch = event.targettouches[0]; Touches array objects get all the touch on the screen, take the first touch
startpos = {x:touch.pagex,y:touch.pagey,time:+new Date}; Take the coordinate value of the first touch
isscrolling = 0; This parameter determines whether to scroll vertically or horizontally
This.slider.addEventListener (' Touchmove ', this,false);
This.slider.addEventListener (' Touchend ', this,false);
},
Move
Move:function (event) {
Do not perform a move operation when the screen has multiple touch or the page is being retracted
if (Event.targetTouches.length > 1 | | event.scale && event.scale!== 1) return;
var touch = event.targettouches[0];
Endpos = {X:TOUCH.PAGEX-STARTPOS.X,Y:TOUCH.PAGEY-STARTPOS.Y};
isscrolling = Math.Abs (endpos.x) < Math.Abs (ENDPOS.Y)? 1:0; Isscrolling is 1 o'clock, indicates longitudinal slide, 0 is transverse slide
if (isscrolling = = = 0) {
Event.preventdefault (); Prevent the default behavior of touch events, which is to prevent scrolling
This.slider.className = ' cnt ';
This.slider.style.left =-this.index*600 + endpos.x + ' px ';
}
},
Slide release
End:function (event) {
var duration = +new Date-startpos.time; Duration of slide
if (isscrolling = = = 0) {//when scrolling horizontally
This.icon[this.index].classname = ";
if (number (duration) > 10) {
Determines whether to move left or right, when the offset is greater than 10 o'clock
if (Endpos.x > 10) {
if (this.index!== 0) This.index-= 1;
}else if (Endpos.x <-10) {
if (This.index!== this.icon.length-1) This.index + = 1;
}
}
This.icon[this.index].classname = ' Curr ';
This.slider.className = ' cnt F-anim ';
This.slider.style.left =-this.index*600 + ' px ';
}
Unbind Event
This.slider.removeEventListener (' Touchmove ', this,false);
This.slider.removeEventListener (' Touchend ', this,false);
}
},
Initialization
Init:function () {
var = this; This refers to a slider object
if (!! Self.touch) Self.slider.addEventListener (' Touchstart ', self.events,false); AddEventListener The second argument can pass an object that invokes the Handleevent property of the object
}
};
Slider.init ();
</script>
</body>
Transferred from: http://blog.csdn.net/codingnoob/article/details/60964017
Mobile phone-side sliding banner code