简体   繁体   English

使用纯 javascript 在滚动端进行页面捕捉(适用于 Cordova 应用程序)

[英]Page snapping on scroll-end with pure javascript (for Cordova App)

I have developed a mobile app that has 3 pages.我开发了一个有 3 页的移动应用程序。 The pages are laid out in a horizontal line.页面以水平线排列。 The user navigates between pages by swiping left or right on the div that contains all 3 pages.用户通过在包含所有 3 个页面的 div 上向左或向右滑动来在页面之间导航。

I have already written code that seems to work sometimes .我已经编写了有时似乎有效的代码。

Please keep in mind that the HTML and CSS i have listed below is simplified for you to get a quicker understanding.请记住,我在下面列出的 HTML 和 CSS 已经过简化,以便您更快地理解。 The width of each AppPage and the AppPages_Container will be set dynamically via javascript when the app loads.每个 AppPage 和 AppPages_Container 的宽度将在应用加载时通过 javascript 动态设置。

HTML HTML

<div id="AppDisplayArea">
  <div id="AppPages_Container" onscroll="get_PagePosition()">
    <div class="AppPage">Page 1</div>
    <div class="AppPage">Page 2</div>
    <div class="AppPage">Page 3</div>
  </div>
</div>

CSS CSS

#AppDisplayArea{position:absolute; top:0; left:0; width:100%; height:100%; overflow-x:scroll;} 
#AppPages_Container{position:absolute; z-index:1; top:0px; bottom:0px; left:0; margin:0; width:2160px; height:100%;}
.AppPage{position:relative; width:720px; height:100%; display:block; float:left;}

JAVASCRIPT爪哇脚本

This is the first function that is called via an onscroll event这是通过 onscroll 事件调用的第一个函数

function get_PagePosition()
{
 var Container = document.getElementById('AppPages_Container'); 
 var scroll_position = Container.scrollLeft;

 var FIRST = 0;
 var SECOND = window.innerwidth;
 var THIRD = window.innerwidth*2;

 var Page1_Position = (Math.abs(FIRST - scroll_position));
 var Page2_Position = (Math.abs(SECOND - scroll_position));
 var Page3_Position = (Math.abs(THIRD - scroll_position));

 var Nearest_Page = Math.min(Page1_Position, Page2_Position, Page3_Position);

 setTimeout(function(){ check_position(); },60);


   function check_position()
    {
      if(Page1_Position==Nearest_Page){SnapPage('Pages_Container',0);}
      if(Page2_Position==Nearest_Page)SnapPage('Pages_Container',1);}
      if(Page3_Position==Nearest_Page)SnapPage('Pages_Container',2);}
    }

}// End get_PagePosition

This is the second function that does the actual snapping (with a nested Animate() function)这是进行实际捕捉的第二个函数(使用嵌套的 Animate() 函数)

function SnapPage(_object,_position){   

var LEFT = (window.innerWidth*_position);                        
var Page   = document.getElementById(_object);
var Scroll_Speed = 10; 
var Scroll_Direction;
var Count = (Math.abs(Page.scrollLeft - LEFT));
var ScrollTimer = setInterval(function(){ Animate(); }, 1);

if(LEFT > Page.scrollLeft){Scroll_Direction=1;}
if(LEFT < Page.scrollLeft){Scroll_Direction=-1;}
if(LEFT == Page.scrollLeft){Scroll_Direction=0;}

 function Animate()
 { 
  if(Count<=0){clearInterval(ScrollTimer); Page.scrollLeft=LEFT; return;}
  Page.scrollLeft += (Scroll_Direction*Scroll_Speed);  
  Count-=Scroll_Speed; if(Count<0){Count=0;}
 }


return false;              
}//End SnapPage.

Is there an easier (more reliable) method for achieving page snapping without the use of a JavaScript Framework??有没有更简单(更可靠)的方法来实现页面捕捉而不使用 JavaScript 框架?? ...please help by giving suggestions or better solutions. ...请提供建议或更好的解决方案来提供帮助。 Thanks.谢谢。

Don't use the onscroll event, use the touchstart and touchmove events.不要使用onscroll事件,使用touchstarttouchmove事件。

Touch events will provide you with an event that includes an array of touch coordinates, so you can use them to detect swipe.触摸事件将为您提供一个包含一系列触摸坐标的事件,因此您可以使用它们来检测滑动。

Keep track of the current view, and change the current div according to the swipe.跟踪当前视图,并根据滑动更改当前 div。

So a simple implementation is:所以一个简单的实现是:

var xStart;
var currentScreen = 1;

function onTouchStart(e) {                                         
    xStart = e.touches[0].clientX;                                      
};                                                

function onTouchMove(e) {
    if (!xStart) return;

    if (xStart - e.touches[0].clientX > 0) {
        // Handle left swipe
        currentScreen++;
        if (currentScreen > 3) currentScreen = 1; 
    } else {
        // Handle right swipe
        currentScreen--;
        if (currentScreen < 1) currentScreen = 3; 
    }                       

    xStart = null;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM