Javascript 如何在jquery中滑动后显示图像?

Javascript 如何在jquery中滑动后显示图像?,javascript,jquery,Javascript,Jquery,我制作了一个演示,使用next和previous按钮显示图像。 我尝试使用swipleft和swiperight 这是我的密码 我们可以使用swipeleft和swipe right执行相同的功能吗 var triggerElementID = null; // this variable is used to identity the triggering element var fingerCount = 0; var startX = 0; var startY = 0; var cu

我制作了一个演示,使用
next
previous
按钮显示图像。

我尝试使用
swipleft
swiperight
这是我的密码

我们可以使用swipeleft和swipe right执行相同的功能吗

var triggerElementID = null; // this variable is used to identity the triggering element
var fingerCount = 0;
var startX = 0;
var startY = 0;
var curX = 0;
var curY = 0;
var deltaX = 0;
var deltaY = 0;
var horzDiff = 0;
var vertDiff = 0;
var minLength = 72; // the shortest distance the user may swipe
var swipeLength = 0;
var swipeAngle = null;
var swipeDirection = null;

// The 4 Touch Event Handlers

// NOTE: the touchStart handler should also receive the ID of the triggering element
// make sure its ID is passed in the event call placed in the element declaration, like:
// <div id="picture-frame" ontouchstart="touchStart(event,'picture-frame');"  ontouchend="touchEnd(event);" ontouchmove="touchMove(event);" ontouchcancel="touchCancel(event);">

function touchStart(event,passedName) {
    // disable the standard ability to select the touched object
    event.preventDefault();
    // get the total number of fingers touching the screen
    fingerCount = event.touches.length;
    // since we're looking for a swipe (single finger) and not a gesture (multiple fingers),
    // check that only one finger was used
    if ( fingerCount == 1 ) {
        // get the coordinates of the touch
        startX = event.touches[0].pageX;
        startY = event.touches[0].pageY;
        // store the triggering element ID
        triggerElementID = passedName;
    } else {
        // more than one finger touched so cancel
        touchCancel(event);
    }
}

function touchMove(event) {
    event.preventDefault();
    if ( event.touches.length == 1 ) {
        curX = event.touches[0].pageX;
        curY = event.touches[0].pageY;
    } else {
        touchCancel(event);
    }
}

function touchEnd(event) {
    event.preventDefault();
    // check to see if more than one finger was used and that there is an ending coordinate
    if ( fingerCount == 1 && curX != 0 ) {
        // use the Distance Formula to determine the length of the swipe
        swipeLength = Math.round(Math.sqrt(Math.pow(curX - startX,2) + Math.pow(curY - startY,2)));
        // if the user swiped more than the minimum length, perform the appropriate action
        if ( swipeLength >= minLength ) {
            caluculateAngle();
            determineSwipeDirection();
            processingRoutine();
            touchCancel(event); // reset the variables
        } else {
            touchCancel(event);
        }   
    } else {
        touchCancel(event);
    }
}

function touchCancel(event) {
    // reset the variables back to default values
    fingerCount = 0;
    startX = 0;
    startY = 0;
    curX = 0;
    curY = 0;
    deltaX = 0;
    deltaY = 0;
    horzDiff = 0;
    vertDiff = 0;
    swipeLength = 0;
    swipeAngle = null;
    swipeDirection = null;
    triggerElementID = null;
}

function caluculateAngle() {
    var X = startX-curX;
    var Y = curY-startY;
    var Z = Math.round(Math.sqrt(Math.pow(X,2)+Math.pow(Y,2))); //the distance - rounded - in pixels
    var r = Math.atan2(Y,X); //angle in radians (Cartesian system)
    swipeAngle = Math.round(r*180/Math.PI); //angle in degrees
    if ( swipeAngle < 0 ) { swipeAngle =  360 - Math.abs(swipeAngle); }
}

function determineSwipeDirection() {

    if ( (swipeAngle <= 45) && (swipeAngle >= 0) ) {
        swipeDirection = 'left';
    } else if ( (swipeAngle <= 360) && (swipeAngle >= 315) ) {
        swipeDirection = 'left';
    } else if ( (swipeAngle >= 135) && (swipeAngle <= 225) ) {
        swipeDirection = 'right';
    } else if ( (swipeAngle > 45) && (swipeAngle < 135) ) {
        swipeDirection = 'down';
    } else {
        swipeDirection = 'up';
    }
}

function processingRoutine() {
    var swipedElement = document.getElementById(triggerElementID);
    if ( swipeDirection == 'left' ) {
        sliders.goToNext();         
    } else if ( swipeDirection == 'right' ) {
        sliders.goToPrev();
    } else if ( swipeDirection == 'up' ) {
        sliders.goToPrev();         
    } else if ( swipeDirection == 'down' ) {
        sliders.goToNext();         
    }
}
var triggerElementID=null;//此变量用于标识触发元素
var-fingerCount=0;
var startX=0;
var-startY=0;
var-curX=0;
var-curY=0;
增值税=0;
var-deltaY=0;
var-horzDiff=0;
var-vertDiff=0;
var minLength=72;//用户可以滑动的最短距离
var swipeLength=0;
var swipeAngle=null;
var swipeDirection=null;
//4个触摸事件处理程序
//注意:touchStart处理程序还应接收触发元素的ID
//确保在元素声明中放置的事件调用中传递其ID,如:
// 
函数touchStart(事件,passedName){
//禁用选择触摸对象的标准功能
event.preventDefault();
//获取触摸屏幕的手指总数
fingerCount=event.touchs.length;
//因为我们寻找的是一个滑动(单指)而不是一个手势(多指),
//检查是否只使用了一根手指
如果(fingerCount==1){
//获取触摸的坐标
startX=event.touchs[0].pageX;
startY=event.touchs[0].pageY;
//存储触发元素ID
triggerElementID=passedName;
}否则{
//多个手指被触碰,所以取消
触摸取消(事件);
}
}
功能触摸移动(事件){
event.preventDefault();
if(event.touchs.length==1){
curX=event.touchs[0].pageX;
curY=event.touchs[0].pageY;
}否则{
触摸取消(事件);
}
}
函数touchEnd(事件){
event.preventDefault();
//检查是否使用了多个手指,以及是否有结束坐标
如果(fingerCount==1&&curX!=0){
//使用距离公式确定滑动的长度
swipeLength=Math.round(Math.sqrt(Math.pow(curX-startX,2)+Math.pow(curY-startY,2));
//如果用户刷卡超过最小长度,请执行相应的操作
如果(瑞士长度>=最小长度){
caluculateAngle();
确定方向();
处理例程();
touchCancel(事件);//重置变量
}否则{
触摸取消(事件);
}   
}否则{
触摸取消(事件);
}
}
功能触摸取消(事件){
//将变量重置回默认值
指数=0;
startX=0;
startY=0;
curX=0;
curY=0;
deltaX=0;
deltaY=0;
horzDiff=0;
vertDiff=0;
swipeLength=0;
swipeAngle=null;
swipeDirection=null;
triggerElementID=null;
}
函数caluculateAngle(){
var X=startX curX;
var Y=curY startY;
var Z=Math.round(Math.sqrt(Math.pow(X,2)+Math.pow(Y,2));//距离-四舍五入-以像素为单位
var r=Math.atan2(Y,X);//以弧度表示的角度(笛卡尔坐标系)
swipeAngle=Math.round(r*180/Math.PI);//角度(度)
如果(swipeAngle<0){swipeAngle=360-Math.abs(swipeAngle);}
}
函数determineSwipeDirection(){
如果((旋转角度=0)){
swipeDirection='左';
}否则如果((旋转角度=315)){
swipeDirection='左';
}如果((旋转角度>=135)和&(旋转角度45)和&(旋转角度<135))出现其他情况{
swipeDirection=‘向下’;
}否则{
swipeDirection=‘向上’;
}
}
函数处理例程(){
var swipedElement=document.getElementById(triggerElementID);
如果(swipeDirection=='left'){
sliders.goToNext();
}else if(swipeDirection=='right'){
sliders.goToPrev();
}else if(swipeDirection=='up'){
sliders.goToPrev();
}else if(swipeDirection=='down'){
sliders.goToNext();
}
}

在使用jQuery时,也要使用具有
滑动功能的jQuery Mobile
eventid不想使用jQuery Mobile查看此