The implementation of the carousel supports the mobile side

Source: Internet
Author: User

The first part is the HTML part, the outer parent element uses relative positioning, the child element uses the absolute top pair, changes their position by changing the Top,left value.
<div class= "Carousel" >
<div class= "IMG1" >

<p> Carousel 1</p>
</div>
<div class= "Img2" >

<p> Carousel 2</p>
</div>
<div class= "IMG3" >

<p> Carousel 3</p>
</div>
<div class= "Img4" >

<p> Carousel 4</p>
</div>
<div class= "Img5" >

<p> Carousel 5</p>
</div>
<p class= "prev" ></p>
<p class= "Next" ></p>
</div>

The second part is the implementation of the Code

<script>
1. Declare the variable, save the retrieved Dom node, and whether or not the timer has a touch enabled.

var carousel=document.getelementsbyclassname ("Carousel") [0];
var aitem=carousel.getelementsbytagname ("div");
var prev=document.getelementsbyclassname ("prev") [0];
var next=document.getelementsbyclassname ("Next") [0];
var autoplay=true;
var timer;
if (autoplay==true) {
Timer=setinterval (left,3000)
}
function Right () {
var fn=new Array (); To create a new array
for (Var i=0;i<aitem.length;i++) {
Fn.push (Aitem[i].classname)//Traverse the class name of each Li and save it to a new array
}
for (Var j=0;j<aitem.length;j++) {
if (j==aitem.length-1) {
AITEM[J].CLASSNAME=FN[0];
}
else{
aitem[j].classname=fn[j+1];//the right switch, traverse each Li, change their class class name, logically each Li class name to become the original class name of the previous, the last one to become the first class name
}
}
}
function Left () {
var fn=new Array ();//Create a new array
for (Var i=0;i<aitem.length;i++) {
Fn.push (Aitem[i].classname)//Traverse each Li's class name and save it to a new array
}
for (Var j=0;j<aitem.length;j++) {
if (j==0) {
AITEM[0].CLASSNAME=FN[AITEM.LENGTH-1];
}
else{
AITEM[J].CLASSNAME=FN[J-1];//Right switch, traverse each Li, change their class class name, logically each Li's class name to become the original class name of the previous, the first becomes the last class name
}
}
}
Prev.onclick=function () {
Right (); Perform right toggle
}
Next.onclick=function () {
Left ();//Switch
}
function Touch () {
var Startx,movex;

Monitoring events for Binding touch
Carousel.addeventlistener ("Touchstart", Touchstart,false);
Carousel.addeventlistener ("Touchmove", Touchmove,false);
Carousel.addeventlistener ("Touchend", Touchend,false);
function Touchstart (e) {
Clearinterval (timer)
sartx= e.changedtouches[0].clientx;//Gets the x-coordinate at the beginning of the touch

}
function Touchmove (e) {
Movex= E.changedtouches[0].clientx-sartx; Gets the distance the finger moves at the end of the touch
}

Determine the distance your finger moves, perform a right or left switch
function Touchend () {
if (movex>100) {
Right ()
}
if (movex<-100) {
Left ()
}
Timer=setinterval (left,3000)
}
}
Touch ()

</script>

The implementation of the carousel supports the mobile side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.