如何在javascript / jquery中使用/调用此函数?

时间:2012-02-17 09:00:59

标签: javascript jquery jquery-mobile

我对jquery / javascript非常新。所以这里,我发现这个js代码用于swipeown和swipeup。但不知道如何使用它或称之为。

js代码:

   (function() {
 // initializes touch and scroll events
    var supportTouch = $.support.touch,
            scrollEvent = "touchmove scroll",
            touchStartEvent = supportTouch ? "touchstart" : "mousedown",
            touchStopEvent = supportTouch ? "touchend" : "mouseup",
            touchMoveEvent = supportTouch ? "touchmove" : "mousemove";

// handles swipeup and swipedown
    $.event.special.swipeupdown = {
        setup: function() {
            var thisObject = this;
            var $this = $(thisObject);

            $this.bind(touchStartEvent, function(event) {
                var data = event.originalEvent.touches ?
                        event.originalEvent.touches[ 0 ] :
                        event,
                        start = {
                            time: (new Date).getTime(),
                            coords: [ data.pageX, data.pageY ],
                            origin: $(event.target)
                        },
                        stop;

                function moveHandler(event) {
                    if (!start) {
                        return;
                    }

                    var data = event.originalEvent.touches ?
                            event.originalEvent.touches[ 0 ] :
                            event;
                    stop = {
                        time: (new Date).getTime(),
                        coords: [ data.pageX, data.pageY ]
                    };

                    // prevent scrolling
                    if (Math.abs(start.coords[1] - stop.coords[1]) > 10) {
                        event.preventDefault();
                    }
                }

                $this
                        .bind(touchMoveEvent, moveHandler)
                        .one(touchStopEvent, function(event) {
                    $this.unbind(touchMoveEvent, moveHandler);
                    if (start && stop) {
                        if (stop.time - start.time < 1000 &&
                                Math.abs(start.coords[1] - stop.coords[1]) > 30 &&
                                Math.abs(start.coords[0] - stop.coords[0]) < 75) {
                            start.origin
                                    .trigger("swipeupdown")
                                    .trigger(start.coords[1] > stop.coords[1] ? "swipeup" : "swipedown");
                        }
                    }
                    start = stop = undefined;
                });
            });
        }
    };

//Adds the events to the jQuery events special collection
    $.each({
        swipedown: "swipeupdown",
        swipeup: "swipeupdown"
    }, function(event, sourceEvent){
        $.event.special[event] = {
            setup: function(){
                $(this).bind(sourceEvent, $.noop);
            }
        };
    });

})();

这是我想使用上面的代码

触发的锚元素
<a href="<?php echo base_url();?>home/drop_down" id="swipedown"></a>

尝试调用这样的函数:

$('#swipedown').live('swipedown', function(event, data){
                    alert('swipes')
            });

但它没有提示我警报。 &GT;&LT ;. 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

last()在创建后调用该函数。该函数为jquery增加了新的可能性。所以你可以称之为:

$('#swipedown').bind('swipedown', function(e){
    alert('test');
});

答案 1 :(得分:0)

您的代码将被添加(如文档所述)到jQuery events special collection这意味着您可以像任何其他jQuery事件一样调用swipeupswipedown

示例:

$("div p").live('swipedown',function() {
  alert("swiped down");
});

这将在发生在p元素上的闪烁动作时触发,如:

<body>
 <div>
  <h1>hello world</h1>
  <p>pull me down</p>
 </div>
</body>