百度地图在移动端下click无效的解决方案

这是由于百度地图在移动端屏蔽了click事件,在网上找到一种方法,利用touchClick方法来模拟click事件,代码如下(需要JQ插件):

 1 //给jquery添加touchClick方法
 2 (function () {
 3     var defaults = {
 4         start: function (self, event) { },
 5         move: function (self, event) { },
 6         end: function (self, event) { }
 7     }
 8     $.fn.touchClick = function (opts) {
 9         if (typeof opts == "function") {
10             opts = $.extend({}, defaults, { end: opts });
11         } else {
12             opts = $.extend({}, defaults, opts);
13         }
14         this.each(function () {
15             var obj = $(this);
16             obj.bind("touchstart", function (event) {
17                 obj.data("move", false);
18                 opts.start.call(this, event);
19             }).bind("touchmove", function (event) {
20                 console.log(event.originalEvent.targetTouches.length);
21                 obj.data("move", true);
22                 opts.move.call(this, event);
23             }).bind("touchend", function (event) {
24                 if (obj.data("move")) {
25                     return;
26                 } else {
27                     opts.end.call(this, event);
28                 }
29                 obj.data("move", false);
30             });
31         });
32     };
33 })(jQuery);

 

posted @ 2019-04-17 09:39  1024记忆  阅读(1334)  评论(0编辑  收藏  举报