使用Jquery Mobile在<div>标记上添加过渡动画

时间:2016-11-20 07:38:59

标签: javascript html css animation jquery-mobile

我可以在回答之前提醒您,我是Javascript的完全愚蠢。

我提供了一个脚本,可以通过Padilicious在我的Jquery Mobile Site上启用滑动导航。根据我的理解,它可以将这些函数放在div标签上:

<div data-role="page" data-theme="a" id="swipeBox" ontouchstart="touchStart(event,'swipeBox');"  ontouchend="touchEnd(event);" ontouchmove="touchMove(event);" ontouchcancel="touchCancel(event);" style="position:relative;width:100%;height:100%;">

现在我的问题是Jquery Mobile上的数据转换仅适用于标签或按钮。如何在标签上添加数据转换或至少进入和退出转换?

如果你们还需要JS用于滑动导航脚本,这里是:

// TOUCH-EVENTS SINGLE-FINGER SWIPE-SENSING JAVASCRIPT
        // Courtesy of PADILICIOUS.COM and MACOSXAUTOMATION.COM

        // this script can be used with one or more page elements to perform actions based on them being swiped with a single finger

        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' ) {
                // REPLACE WITH YOUR ROUTINES
                window.location.href = "gallery.html";
            } else if ( swipeDirection == 'right' ) {
                // REPLACE WITH YOUR ROUTINES
                window.location.href = "gallery.html";
            } else if ( swipeDirection == 'up' ) {
                // REPLACE WITH YOUR ROUTINES
                window.location.href = "gallery.html";
            } else if ( swipeDirection == 'down' ) {
                // REPLACE WITH YOUR ROUTINES
                window.location.href = "gallery.html";
            }

        }

请注意,以下是负责重定向到其他页面的人员:

function processingRoutine() {
            var swipedElement = document.getElementById(triggerElementID);
            if ( swipeDirection == 'left' ) {
                // REPLACE WITH YOUR ROUTINES
                window.location.href = "gallery.html";
            } else if ( swipeDirection == 'right' ) {
                // REPLACE WITH YOUR ROUTINES
                window.location.href = "gallery.html";
            } else if ( swipeDirection == 'up' ) {
                // REPLACE WITH YOUR ROUTINES
                window.location.href = "gallery.html";
            } else if ( swipeDirection == 'down' ) {
                // REPLACE WITH YOUR ROUTINES
                window.location.href = "gallery.html";
            }

我的主要议程仅在于在页面转换上添加滑动动画。对我应该怎么做并不重要。谢谢,爱你!

1 个答案:

答案 0 :(得分:0)

无需编写那么多代码。请尝试以下代码:

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>
	<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page" id="pageOne">
	<div role="main" class="ui-content">
		Page One
		<br>
		<a href="#pageTwo" data-transition="slide">Go to page Two</a> 
	</div>

</div>

<div data-role="page" id="pageTwo">
	<div role="main" class="ui-content">
		Page Two
		<br>		
		<a href="#pageOne" data-transition="slide">Go to page One</a> 
	</div>
</div>
</body>
</html>
&#13;
&#13;
&#13;