2014-05-08 22 views
0

在學校的項目上工作,我試圖用加速度計滾動網頁。使用iPhone的加速度計滾動網頁

我希望能夠滾動到頁面的頂部和底部。這通過傾斜iPhone。

我使用Cordova作爲libirarie來訪問加速度計。

http://cordova.apache.org/docs/en/3.3.0/cordova_accelerometer_accelerometer.md.html#accelerometer.watchAcceleration

但我卡住了。

我得到所有的時間uncaught類型錯誤,所以我沒有得到的加速度計的值。

而另一個問題是,我不知道如何使用加速度計的值並將其用於在網頁上滾動。

<header class="meta"> 
     <h1><i class="fa fa-flask"></i> Use a sensor</h1> 
    </header> 

    <main> 
     <div class="container"> 
      <section data-route="some-section" class="front-panel"> 

       <article> 
        <header> 
         <h1>Instructions</h1> 
        </header> 
        <div id="accelerometer">Waiting for accelerometer...</div>      
       </article> 
      </section> 

      <section data-route="some-other-section" class="back-panel"> 
       <figure> 
        <img src="media/some-other-picture.jpg" alt="a picture"> 
       </figure> 

       <header> 
        <h1>Some other section</h1> 
       </header> 

       <article> 
        <header> 
         <h1> 
          Some other article 
         </h1> 
        </header> 
       </article> 
      </section> 
     </div> 
    </main> 

    <footer class="meta"> 
     <nav> 
      <ul> 
       <li><a href="#/some-section"><i class="fa fa-hand-o-left"></i></a></li> 
       <li><a href="#/some-other-section"><i class="fa fa-hand-o-right"></i></a></li> 
      </ul> 
     </nav> 
    </footer> 
    <script src="cordova.js"></script> 
    <script src="static/js/vendor/routie.min.js"></script> 
    <script src="static/js/script.js"></script> 

//self-invoking anonymous function 

(函數(){ '使用嚴格';

var scrollTop = document.body.scrollTop, 
    el   = document.body, 
    els   = document.querySelectorAll('.meta'), 
    position = el.scrollTop, 
    oHeight  = el.offsetHeight, 
    wHeight  = window.innerHeight, 
    $, $$; 

//initialize app with an controller object literal 
var app = { 
    //init method, Cordova is ready to be used 
    init: function() { 
     this.router(); 

     accelerometer.begin(); 

     document.addEventListener('gesturechange', this, false); 
     document.addEventListener('scroll', this, false);    

    }, 

    router: function() { 
     routie({ 
      '/some-section': function() { 
       section.toggle('some-section'); 
      }, 
      '/some-other-section': function(route) { 
       section.toggle('some-other-section'); 
      } 
     }); 
    }, 

    handleEvent: function(e) { 
     var scroll = el.scrollTop, 
      i = 0, 
      l = els.length; 

     if (scroll > position && (scroll + wHeight) < oHeight && position > 0) { 
      // scrolling Down 
      for (;i < l;i++) { 
       els[i].classList.add('shrink'); 
      }; 
     } else { 
      // scrolling Up 
      for (;i < l;i++) { 
       els[i].classList.remove('shrink'); 
      }; 
     } 
     position = scroll; 
    }, 

}; 

var section = { 
    toggle: function(route) { 
     var panel = $('[data-route='+ route +']'), 
      front = /front-panel/.test(panel.className); 

     this.fp = $('.front-panel'); 
     this.bp = $('.back-panel'); 

     this.bp.addEventListener('webkitTransitionEnd',this,false) 

     if(!(panel == this.fp)){ 
      this.fp.classList.add('out'); 
      this.bp.classList.remove('back-panel'); 
      this.bp.classList.add('front-panel'); 
     } else { 
      // to do: active navigation 
     } 
    }, 

    handleEvent: function() { 
     this.fp.classList.remove('out','front-panel'); 
     this.fp.classList.add('back-panel'); 
    } 
}; 

// utilities object for common thingies 
var utils = { 
    init: function() { 
     // Shorthand selectors 
     $ = this.selectElement, 
     $$ = this.selectElements; 
    }, 
    selectElement: function(el) { 
     return document.querySelector(el); 
    }, 
    selectElements: function(el) { 
     return document.querySelectorAll(el); 
    } 
}; 

var accelerometer = { 
    begin: function() { 
     //update accleration every 100 of a second 
     var options = { 
      frequency: 100 
     }; 

     watchID = navigator.accelerometer.watchAcceleration(this.success, debug.fail, options); 
    }, 
    // Stop watching the acceleration 
    stop: function() { 
     if (watchID) { 
      navigator.accelerometer.clearWatch(watchID); 
      watchID = null; 
     } 
    }, 
    // onSuccess: Get a snapshot of the current acceleration 
    success: function (acceleration) { 
     var element = document.getElementById('accelerometer'); 
     element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />'+ 
          'Acceleration Y: ' + acceleration.y + '<br />'+ 
          'Acceleration Z: ' + acceleration.z + '<br />'+ 
          'Timestamp: '  + acceleration.timestamp + '<br />'; 
    } 
}; 

var debug = { 
    fail: function() { 
     alert('onError!'); 
    } 
};  

utils.init(); 
app.init(); 

})();

任何幫助將會很棒!

回答

-1

我不知道加速度計。但是我想讓內容可滾動地使用平滑使用的isroll插件,並檢測單個和多個手指的輕掃,捏和並回落到桌面上的鼠標拖動。

iscroll

TouchSwipe

TouchSwipe demo

+0

甚至沒有開始接近OP的問題。 –

0

花了我3天,但我得到它通過我自己的工作,真棒!

//self-invoking anonymous function 

(函數(){ '使用嚴格';

var scrollTop = document.body.scrollTop, 
    el   = document.body, 
    els   = document.querySelectorAll('.meta'), 
    position = el.scrollTop, 
    oHeight  = el.offsetHeight, 
    wHeight  = window.innerHeight, 
    $, $$; 

var watchID = null; 

//initialize app with an controller object literal 
var app = { 
    //initialize method, Cordova is ready to be used 
    init: function() { 
     this.router(); 

     document.addEventListener('gesturechange', this, false); 
     document.addEventListener('scroll', this, false); 
     document.addEventListener("deviceready", accelerometer.ready, false); 
    }, 

    router: function() { 
     routie({ 
      '/some-section': function() { 
       section.toggle('some-section'); 
      }, 
      '/some-other-section': function(route) { 
       section.toggle('some-other-section'); 
      } 
     }); 
    }, 

    handleEvent: function(e) { 
     var scroll = el.scrollTop, 
      i = 0, 
      l = els.length; 

     if (scroll > position && (scroll + wHeight) < oHeight && position > 0) { 
      // scrolling Down 
      for (;i < l;i++) { 
       els[i].classList.add('shrink'); 
      }; 
     } else { 
      // scrolling Up 
      for (;i < l;i++) { 
       els[i].classList.remove('shrink'); 
      }; 
     } 
     position = scroll; 
    }, 

}; 

var section = { 
    toggle: function(route) { 
     var panel = $('[data-route='+ route +']'), 
      front = /front-panel/.test(panel.className); 

     this.fp = $('.front-panel'); 
     this.bp = $('.back-panel'); 

     this.bp.addEventListener('webkitTransitionEnd',this,false) 

     if(!(panel == this.fp)){ 
      this.fp.classList.add('out'); 
      this.bp.classList.remove('back-panel'); 
      this.bp.classList.add('front-panel'); 
     } else { 
      // to do: active navigation 
     } 
    }, 

    handleEvent: function() { 
     this.fp.classList.remove('out','front-panel'); 
     this.fp.classList.add('back-panel'); 
    } 
}; 

// utilities object for common thingies 
var utils = { 
    init: function() { 
     // Shorthand selectors 
     $ = this.selectElement, 
     $$ = this.selectElements; 
    }, 
    selectElement: function(el) { 
     return document.querySelector(el); 
    }, 
    selectElements: function(el) { 
     return document.querySelectorAll(el); 
    } 
}; 

var accelerometer = { 
    ready: function() { 
     accelerometer.begin(); 
    }, 

    begin: function() { 

     //update accleration every 100 of a second 
     var options = { 
      frequency: 100 
     }; 

     watchID = navigator.accelerometer.watchAcceleration(this.success, debug.fail, options); 
    },   
    // Stop watching the acceleration 
    stop: function() { 
     if (watchID) { 
      navigator.accelerometer.clearWatch(watchID); 
      watchID = null; 
     } 
    }, 
    // onSuccess: Get a snapshot of the current acceleration 
    success: function (acceleration) { 
     var element = document.getElementById('accelerometer'); 
     element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />'+ 
          'Acceleration Y: ' + acceleration.y + '<br />'+ 
          'Acceleration Z: ' + acceleration.z + '<br />'+ 
          'Timestamp: '  + acceleration.timestamp + '<br />'; 

     //starts scrolling up when the acceleration.y is bigger then 7 and 
     //scrolls down when the acceleration.y lower is then 2 AWESOME!!! 
     if (acceleration.y > 7.0) { 
      window.scrollBy(0, -5); 
     } else if (acceleration.y < 2.0) { 
      window.scrollBy(0, 5); 
     } 
    } 
}; 

var debug = { 
    fail: function() { 
     alert('onError!'); 
    } 
};  

utils.init(); 
app.init(); 

})();

1

看起來有人已經實現了我的想法。好。但實際上你的代碼對我來說看起來並不複雜。事實上,我在我的黑莓Z3設備上實現了這個想法。它效果很好。 我使用了BB WebWorks HTML5。 這是代碼。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 

<!-- Including cordova library --> 
<script src="cordova.js"></script> 

<script> 

//attaching devieready event to the document 
document.addEventListener("deviceready", status, false); 

//evaluating function when devide is ready 
function status() 
{ 
function accelerometerCallback(data) 
{ 
    //displaying actual accelerometer data 
    var dy = data.y; 
    document.getElementById("ydata").innerHTML = dy; 

    //defining a scrolly variable and assigned the accelerometer data 
    var scrolly = dy; 


    if(dy < -1) //if the phone is tilted upward than normal, scroll the page all the  way to top with great speed. 
    { 
     window.scrollBy(0,scrolly*100); 
    } 


    if(dy > 9) // if the phone is tilted downward (almost vertically), scroll the page all the way down with great speed. 
    { 
     window.scrollBy(0,scrolly*20); 
    } 

    //scroll the page with human-readable speed (almost slow) 
    window.scrollBy(0,scrolly-5); 
} 

//attaching accelerometer data event to the document 
document.addEventListener("deviceaccelerometer", accelerometerCallback); 
} 
</script> 

</head> 

<body> 
<!-- Displaying the actual accelerometer "y" direction data --> 
<h1 id="ydata"></h1> 

<!--Add some dummy data to the test how the gravity scrolling works --> 
<div style="font-size:36px"> 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 


Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. 

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. 


Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). 


Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. 

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. 
</div> 
</body> 
</html>