2016-08-03 3 views
0

スワイプのアクション(左、右、上、下)を追加して、スワイプの方向を警告します。このためスワイプが間違って表示される

コード:

var touchStartCoords = {'x':-1, 'y':-1}, // X and Y coordinates on mousedown or touchstart events. 
 
    touchEndCoords = {'x':-1, 'y':-1},// X and Y coordinates on mouseup or touchend events. 
 
    direction = 'undefined',// Swipe direction 
 
    minDistanceXAxis = 10,// Min distance on mousemove or touchmove on the X axis 
 
    maxDistanceYAxis = 10,// Max distance on mousemove or touchmove on the Y axis 
 
    maxAllowedTime = 1000,// Max allowed time between swipeStart and swipeEnd 
 
    startTime = 0,// Time on swipeStart 
 
    elapsedTime = 0,// Elapsed time between swipeStart and swipeEnd 
 
    targetElement = document.getElementById('el');// Element to delegate 
 

 
function swipeStart(e) { 
 
    e = e ? e : window.event; 
 
    e = ('changedTouches' in e)?e.changedTouches[0] : e; 
 
    touchStartCoords = {'x':e.pageX, 'y':e.pageY}; 
 
    startTime = new Date().getTime(); 
 
    targetElement.textContent = " "; 
 
} 
 

 
function swipeMove(e){ 
 
    e = e ? e : window.event; 
 
    e.preventDefault(); 
 
} 
 

 
function swipeEnd(e) { 
 
    e = e ? e : window.event; 
 
    e = ('changedTouches' in e)?e.changedTouches[0] : e; 
 

 
    touchEndCoords = {'x':e.pageX - touchStartCoords.x, 'y':e.pageY - touchStartCoords.y}; 
 

 
    elapsedTime = new Date().getTime() - startTime; 
 

 
    if (elapsedTime <= maxAllowedTime){ 
 
    if (Math.abs(touchEndCoords.x) >= minDistanceXAxis && Math.abs(touchEndCoords.y) <= maxDistanceYAxis){ 
 
     
 
     direction = (touchEndCoords.x < 0)? 'left' : 'right'; 
 

 
     switch(direction){ 
 
     case 'left': 
 
      alert("left"); 
 
      document.getElementById("el").innerHTML="<img src = http://www.worthwild.com/assets/img-mas-02-73410a2b7c925f01866e68f4651510a0.jpg>"; 
 
      break; 
 
     case 'right': 
 
      alert("right"); 
 
      document.getElementById("el").innerHTML="<img src = http://www.worthwild.com/assets/img-mas-02-73410a2b7c925f01866e68f4651510a0.jpg>"; 
 
      break; 
 
     } 
 

 
     
 
    } 
 

 
    if (Math.abs(touchEndCoords.x) >= minDistanceXAxis && Math.abs(touchEndCoords.y) <= maxDistanceYAxis){ 
 
     
 
     direction2 = (touchEndCoords.y > 0)? 'top' : 'bottom'; 
 

 
     switch(direction2){ 
 
     case 'top': 
 
      alert("top"); 
 
      document.getElementById("el").innerHTML="<img src = http://www.worthwild.com/assets/img-mas-02-73410a2b7c925f01866e68f4651510a0.jpg>"; 
 
      break; 
 
     case 'bottom': 
 
      alert("bottom"); 
 
     document.getElementById("el").innerHTML="<img src = http://www.worthwild.com/assets/img-mas-02-73410a2b7c925f01866e68f4651510a0.jpg>"; 
 
      break; 
 
     } 
 

 
     
 
    } 
 

 
    } 
 

 
} 
 

 
function addMultipleListeners(el, s, fn) { 
 
    var evts = s.split(' '); 
 
    for (var i=0, iLen=evts.length; i<iLen; i++) { 
 
    el.addEventListener(evts[i], fn, false); 
 
    } 
 
} 
 

 
addMultipleListeners(targetElement, 'mousedown touchstart', swipeStart); 
 
addMultipleListeners(targetElement, 'mousemove touchmove', swipeMove); 
 
addMultipleListeners(targetElement, 'mouseup touchend', swipeEnd);
@import url(http://fonts.googleapis.com/css?family=Open+Sans:300,400,600); 
 
@import url(http://weloveiconfonts.com/api/?family=fontawesome); 
 
[class*="fontawesome-"]:before { 
 
    font-family: 'FontAwesome', sans-serif; 
 
} 
 

 
*, *:before, *:after { 
 
    box-sizing: border-box; 
 
} 
 

 
a { 
 
    text-decoration: none; 
 
    color: #333; 
 
} 
 

 
body { 
 
    margin-top: -22px; 
 
    margin-bottom: 0px; 
 
    margin-right: 0px; 
 
    margin-left: 0px; 
 
} 
 

 
#container { 
 
    width: 360px; 
 
    height: 640px; 
 
    background-color: #ff5a5a; 
 
} 
 

 
.square-box{ 
 
    margin: 0 auto; 
 
    position: relative; 
 
    width: 60%; 
 
    height: 15%; 
 
    overflow: hidden; 
 
    background: #4679BD; 
 
    top: 50px; 
 
} 
 
.square-box:before{ 
 
    content: ""; 
 
    display: block; 
 
    padding-top: 100%; 
 
} 
 
.square-content{ 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
    color: white; 
 
} 
 
.square-content div { 
 
    display: table; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 
.square-content span { 
 
    display: table-cell; 
 
    text-align: center; 
 
    text-transform: uppercase; 
 
    vertical-align: middle; 
 
    font-size: 20px; 
 
    font-weight: bold; 
 
    color: white; 
 
    font-family: 'Lato', sans-serif; 
 
} 
 

 
.button-panel 
 
{  
 
    display: table; 
 
    margin: 0 auto; 
 
    width: 60%; 
 
    
 
} 
 

 
.buttonlogin{ 
 
    margin-top: 30%; 
 
    display: block; 
 
    background: #27adf1; 
 
    color: white; 
 
    border: none; 
 
    width: 100%; 
 
    height: 50px; 
 
} 
 

 
.button { 
 
    margin-top: 40%; 
 
    background: #27adf1; 
 
    color: white; 
 
    border: none; 
 
    width: 100%; 
 
    height: 50px; 
 
} 
 

 
.button2 { 
 
    margin-top: .25em; 
 
    border: 1px solid #27adf1; 
 
    color: #27adf1; 
 
    background: white; 
 
    width: 100%; 
 
    height: 50px; 
 
} 
 

 
#status{ 
 
    margin: 0 auto; 
 
    width: 216px; 
 
    height: 50px; 
 
    color: #27adf1; 
 
    margin-top: 4.25em; 
 
    display: block; 
 
    background: white; 
 
} 
 

 
h1, p { 
 
    text-align: center; 
 
} 
 

 
#el { 
 
    margin: auto; 
 
    position: relative; 
 
    top: 0; 
 
    left: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
    width: 250px; 
 
    height: 250px; 
 
    line-height: 250px; 
 
    text-align: center; 
 
} 
 

 
#el img{ 
 
    margin: auto; 
 
    height: 250px; 
 
    height: 250px; 
 
    line-height: 250px; 
 
    text-align: center; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="container"> 
 

 
\t \t <div class="main"> 
 

 
\t \t  <div class="button-panel"> 
 
\t \t   <button class="backbutton" 
 
\t \t   style="margin-top: 15%; 
 
\t \t \t  display: block; 
 
\t \t \t  background: #27adf1; 
 
\t \t \t  color: white; 
 
\t \t \t  border: none; 
 
\t \t \t  width: 100%; 
 
\t \t \t  height: 50px;" 
 
\t \t   onclick="window.location.href='index.html'">Back to Menu</button> 
 
\t \t  </div> 
 

 
\t \t </div> 
 

 
\t \t <div id="wrapper"> 
 

 
\t \t  <div id="el"> 
 
\t \t \t  <script type="text/javascript"> 
 
\t \t \t  \t document.write("<img src = http://www.worthwild.com/assets/img-mas-02-73410a2b7c925f01866e68f4651510a0.jpg>"); 
 
\t \t \t  </script> 
 
\t \t  </div> 
 

 
\t \t </div> 
 

 
\t </div>

問題は上部と下部方向です。左右方向の警告が表示されます...

しかし、私はすべて別々にしたいです。

左にスワイプすると、左に警告が表示されます。

私が右にスワイプすると、右だけが警告されます。

トップとボトムに同じ...

このコードにはどのような問題がありますか?

答えて

1

これは私がスワイプを検出するために使用するコードです:あなたはその後、例えば代わり​​に、文書全体の要素に、これが特定したい場合は

Here's a fiddle showing it in action (switch to mobile in dev mode)

var el = document.getElementById('myID'); 

el.addEventListener('touchstart', handleTouchStart, false); 
el.addEventListener('touchmove', handleTouchMove, false); 

これは、ドキュメント全体のためである

document.addEventListener('touchstart', handleTouchStart, false); 
document.addEventListener('touchmove', handleTouchMove, false); 

var xDown = null; 
var yDown = null; 

function handleTouchStart(evt) { 
    xDown = evt.touches[0].clientX; 
    yDown = evt.touches[0].clientY; 
}; 

function handleTouchMove(evt) { 
    if (!xDown || !yDown) { 
    return; 
    } 

    var xUp = evt.touches[0].clientX; 
    var yUp = evt.touches[0].clientY; 

    var xDiff = xDown - xUp; 
    var yDiff = yDown - yUp; 

    if (Math.abs(xDiff) > Math.abs(yDiff)) { /*most significant*/ 
    if (xDiff > 0) { 
     /* left */ 
     alert('left'); 
    } else { 
     /* right */ 
     alert('right'); 
    } 
    } else { 
    if (yDiff > 0) { 
     /* up */ 
     alert('up'); 
    } else { 
     /* down */ 
     alert('down'); 
    } 
    } 
    /* reset values */ 
    xDown = null; 
    yDown = null; 
}; 
+0

おかげでおい! – Tomasz

+1

ようこそ、嬉しいです! –

1

左/右のチェック対あなたのトップ/ボトムの両方のためのif句を見。

if (Math.abs(touchEndCoords.x) >= minDistanceXAxis && Math.abs(touchEndCoords.y) <= maxDistanceYAxis){ 
    // Process left/right case... 
} 
... 
if (Math.abs(touchEndCoords.x) >= minDistanceXAxis && Math.abs(touchEndCoords.y) <= maxDistanceYAxis){ 
    // Process top/bottom case... 
} 

これは、イベントが左/右であると検出されると、常に上/下として検出されることを意味します。

は、必要に応じて、xとyの値を交換し、トップ/ボトムif句を更新:

if (Math.abs(touchEndCoords.x) >= minDistanceXAxis && Math.abs(touchEndCoords.y) < maxDistanceYAxis){ 
    // Process left/right case... 
} 
... 
if (Math.abs(touchEndCoords.y) >= minDistanceYAxis && Math.abs(touchEndCoords.x) < maxDistanceXAxis){ 
    // Process top/bottom case... 
} 
+0

私はそれらを分けるとき:https://jsfiddle.net/egacn5j2/ 彼らは前のように同じことをします。 – Tomasz

+0

いいえ、それらを分離する必要はありません...あなたは同じ条件チェックを2回行っていますが、相互に排他的な結果を期待しています(不可能)。これをもっと明確にするために答えを編集します... – Ironcache

関連する問題