最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - basic touch gesture not working, using hammer.js - Stack Overflow

programmeradmin0浏览0评论

Im just beginning to learn javascript so I can implement touch gestures, I am using the hammer.js library, but even a simple piece of code doesnt seem to work, does anyone know if Im missing something obvious?

I am including hammer.js correctly (have tested it by putting in the path in the browser) And I have included this code in the head...

<script type="text/javascript">
    var element = document.getElementById('tapdiv');
    var hammertime = Hammer(element).on("tap", function(event) {
    alert('hello!');
    });
</script>

and my html...

<div id="tapdiv">
 <img src="files/tabdiv.jpg" />
</div>

EDIT: IM also receiving this error in the console for hammer.js ...

TypeError: el is null [Break On This Error]

if(!css_props || !el.style) {

Im just beginning to learn javascript so I can implement touch gestures, I am using the hammer.js library, but even a simple piece of code doesnt seem to work, does anyone know if Im missing something obvious?

I am including hammer.js correctly (have tested it by putting in the path in the browser) And I have included this code in the head...

<script type="text/javascript">
    var element = document.getElementById('tapdiv');
    var hammertime = Hammer(element).on("tap", function(event) {
    alert('hello!');
    });
</script>

and my html...

<div id="tapdiv">
 <img src="files/tabdiv.jpg" />
</div>

EDIT: IM also receiving this error in the console for hammer.js ...

TypeError: el is null [Break On This Error]

if(!css_props || !el.style) {

Share Improve this question edited Dec 20, 2018 at 23:32 isherwood 61.1k16 gold badges121 silver badges169 bronze badges asked Mar 1, 2013 at 16:50 AdrianAdrian 2,0125 gold badges46 silver badges109 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

It appears that your javascript is attempting to access an object in the DOM that doesn't exist yet -- make sure you execute your javascript after the DOM is ready:

<script type="text/javascript">
window.addEventListener('load', function() { 
    var element = document.getElementById('tapdiv');
    var hammertime = Hammer(element).on("tap", function(event) {
        alert('hello!');
    })

}, false);
</script>
发布评论

评论列表(0)

  1. 暂无评论